https://zhuanlan.zhihu.com/p/308999073html
今天是鄙人的生日,欢luo事后想写点关于itchat的文章~python
(不当心暴露年龄了,是的,我已经16岁了~~)react
言归正传,这里说的自动回复包括了两种:一个是相似QQ的“【自动回复】”,就是一句本身设置的固定的话,别人以给你发消息你就自动回复这句话;二是相似Siri的智能回复,就是找了一个机器人代替你聊天,哈哈,虽然贱贱的可是我真的很讨厌微信聊天。json
1、运行环境和看懂下文须要的知识windows
一、Python基本语法。这个若是不清楚的话,先学习下吧,几天就看完了:api
Python基本知识服务器
二、Linux系统。固然这个不是必须的,若是你只是想试验一下的话彻底能够在windows下,Python能够跨平台的,不过毕竟我想24h跑这个脚本,那就最好用服务器,服务器大部分是Linux的,我用的是阿里云ECS CentOS release 6.5。微信
2、基本原理app
一、针对第一种类QQ的自动回复,这个很简单,就是收到消息就向发送者发送一条固定字符串就好了。ide
二、针对第二种类Siri的智能回复,这个须要调用图灵机器人(Turing Robort),就是将收到的消息发送给Turing,再把Turing回复的消息发送给发送者。
三、有一些问题须要解决
3、代码实现
一、申请Turing机器人的对外接口key
添加一个机器人,设置机器人跟你的名字同样,其余信息也同样,由于有时候这个傻子会以第三人称来回复消息,好比“帅帅以为这个很不错!”,设置好,不容易露馅!
最关键的是拿到APIkey,而后替换掉下面代码中的“replaceKeyByYourKey”。
二、python源代码
#coding=utf8 import itchat from itchat.content import TEXT from itchat.content import * import sys import time import re import requests, json import aiml import os # When recieve the following msg types, trigger the auto replying. @itchat.msg_register([TEXT, PICTURE, FRIENDS, CARD, MAP, SHARING, RECORDING, ATTACHMENT, VIDEO],isFriendChat=True, isMpChat=True) def text_reply(msg): global auto_reply, robort_reply, peer_list # The command signal of "[自动回复]" if msg['FromUserName'] == myUserName and msg['Content'] == u"开启自动回复": auto_reply = True itchat.send_msg(u"[自动回复]已经打开。\n", msg['FromUserName']) elif msg['FromUserName'] == myUserName and msg['Content'] == u"关闭自动回复": auto_reply = False itchat.send_msg(u"[自动回复]已经关闭。\n", msg['FromUserName']) # elif not msg['FromUserName'] == myUserName: else: if auto_reply == True: itchat.send_msg(u"[自动回复]您好,我如今有事不在,一会再和您联系。\n", msg['FromUserName']) else: ''' For none-filehelper message, if recieve '= =', start robort replying. if recieve 'x x', stop robort replying. ''' if msg['Content'] == u"= =": robort_reply = True peer_list.append(msg['ToUserName']) return elif msg['Content'] == u"x x": robort_reply = False peer_list.remove(msg['ToUserName']) return # Let Turing reply the msg. if robort_reply == True and msg['FromUserName'] in peer_list: # Sleep 1 second is not necessary. Just cheat human. time.sleep(1) cont = requests.get('http://www.tuling123.com/openapi/api?key=replacekeyByYourKey&info=%s' % msg['Content']).content m = json.loads