python:itchat操作微信自动回复

该程序实现对特定好友的自动回复,指定一位

安装

pip install itchat

注册一个图灵官网账号:http://www.tuling123.com/,免费的

注册完成后需要用到你申请机器人的key

源代码:

代码执行时会跳出二维码界面,通过手机微信扫码进行登录

import itchat
import requests

# 登录微信
itchat.auto_login(hotReload=True)
# 获取好友发的消息
apiUrl="http://www.tuling123.com/openapi/api"
# 根据发的消息回复
def get_info(message):
    data={
        # 这是注册机器人时的key
        'key':'a40e72ec77e247a99ff79169b8d1e26e',
        'info':message,
        'userid':'wechat-robot'
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        info=r['text']
        # 打印机器人回复的信息
        print("robot reply:%s"%info)
        return info
    except:
        return

# 回复给微信好友
@itchat.msg_register(itchat.content.TEXT)
def auto_reply(msg):
    defaultReply='我知道了'
    # 搜索微信好友
    readFriend=itchat.search_friends(name='微信好友的备注')
    readFriendsName=readFriend[0]['UserName']
    # 打印好友回复的信息
    print("message:%s"%msg['Text'])
    reply=get_info(msg['Text'])
    if msg['FromUserName']==readFriendsName:
        itchat.send(reply,toUserName=readFriendsName)

itchat.run()

猜你喜欢

转载自blog.csdn.net/LinRuiC/article/details/82990486