python 微信机器人,微信自动回复

使用python现成的模块 itchat,可以实现,微信机器人的自动回复

其内部原理,是模拟了web版微信的登录,然后进行消息的接收发送,并不是只能用python实现,其他后端语言都可以做到

下面是使用例子

#coding=utf8
import itchat, time
from itchat.content import *

import requests

KEY = '46653ff2f959407da57e9ada472ce4f0' 
ME = '@2df4bb846987e4e401e7da93e4fa09131e4ffc38cdedbb951ae18b74721f769e'  #自己
def get_response(msg):
    # 构造了要发送给服务器的数据
    apiUrl = 'http://www.tuling123.com/openapi/api'
    data = {
        'key'    : KEY,
        'info'   : msg,
        'userid' : 'wechat-robot',
    }
    try:
        r = requests.post(apiUrl, data=data).json()
        # 字典的get方法在字典没有'text'值的时候会返回None而不会抛出异常
        return  "机器人回复:" + r.get('text')
    # 为了防止服务器没有正常响应导致程序异常退出,这里用try-except捕获了异常
    # 如果服务器没能正常交互(返回非json或无法连接),那么就会进入下面的return
    except:
        # 将会返回一个None
        return


@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])
def text_reply(msg):
    #打印一下你发出的消息
    print(msg['Text'])
    # 为了保证在图灵Key出现问题的时候仍旧可以回复,这里设置一个默认回复
    defaultReply = 'I received: ' + msg['Text']
    # 如果图灵Key出现问题,那么reply将会是None
    reply = get_response(msg['Text'])
    # a or b的意思是,如果a有内容,那么返回a,否则返回b
    # 有内容一般就是指非空或者非None,你可以用`if a: print('True')`来测试
    replymsg = reply or defaultReply
    print(replymsg)
    return replymsg

#普通好有回复
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
def download_files(msg):
    msg.download(msg.fileName)
    typeSymbol = {
        PICTURE: 'img',
        VIDEO: 'vid', }.get(msg.type, 'fil')
    return '@%s@%s' % (typeSymbol, msg.fileName)

@itchat.msg_register(FRIENDS)
def add_friend(msg):
# 该操作会自动将新好友的消息录入,不需要重载通讯录
    itchat.add_friend(**msg['Text']) 
    # 加完好友后,给好友打个招呼
    itchat.send('Nice to meet you!', msg['RecommendInfo']['UserName'])

#群消息回复
@itchat.msg_register(TEXT, isGroupChat=True)
def text_reply(msg):
    print(msg['Text'])
    print("to  ",msg['ToUserName'])
    print("from",msg['FromUserName'])
    # 为了保证在图灵Key出现问题的时候仍旧可以回复,这里设置一个默认回复
    defaultReply = 'I received: ' + msg['Text']
    # 如果图灵Key出现问题,那么reply将会是None
    reply = get_response(msg['Text'])
    # a or b的意思是,如果a有内容,那么返回a,否则返回b
    # 有内容一般就是指非空或者非None,你可以用`if a: print('True')`来测试
    replymsg = reply or defaultReply
    print(replymsg)
    return replymsg

itchat.auto_login(hotReload=True)
itchat.run(True)

 
原文地址:https://www.cnblogs.com/muamaker/p/9869482.html