利用itchat 以及图灵机器人
在图灵机器人网站上注册即可,利用itchat登录网页版微信,当收到消息时,利用图灵机器人去回复,可以查询天气预报,讲笑话,查快递单号,百科等一些信息,比较好玩。
实现代码
如下:
#coding=utf8
import requests
import itchat
from itchat.content import *
KEY = 'd371624b5a324c7caf827956398c6451'
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()
return r.get('text')
except:
return
@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
# defaultReply = 'I received: ' + msg['Text']
# reply = U'自动回复:'+get_response(msg['Text'])
# return reply or defaultReply
return get_response(msg['Text'])
@itchat.msg_register([PICTURE,RECORDING,ATTACHMENT,VIDEO])
def download_files(msg):
msg['Text'](msg['FileName'])
return '@%s@%s'%({'Picture':'img','Video':'vid'}.get(msg['Type'],'fil'),msg['FileName'])
@itchat.msg_register(FRIENDS)
def add_friend(msg):
itchat.add_friend(**msg['Text'])# 该操作将自动将好友的消息录入,不需要重载通讯录
itchat.send_msg('Nice to meet you!',msg['RecommendInfo']['UserName'])
@itchat.msg_register(TEXT,isGroupChat=True)
def text_reply(msg):
if msg['isAt']:
itchat.send(u'@%s\u2005I received: %s '%(msg['ActualNickName'],msg['Content']),msg['FromUserName'])
itchat.auto_login(hotReload=True)
itchat.run()