'''Created on 2018年4月26日@author: cvv54'''#引入itchat包获取微信消息import itchatfrom itchat.content import *import hashlibimport requests#注册帐号http://www.tuling123.com/#获得自己的机器人,并记录下apikey#传入消息给机器人并获取回复def get_response(msg,FromUserName): api_url='http://www.tuling123.com/openapi/api' #填自己的apikey apikey='*************************************' hash=hashlib.md5() userid=hash.update(FromUserName.encode('utf-8')) data = { 'key': apikey, 'info': msg, 'userid': userid } try: #给机器人的api接口转发信息 req = requests.post(api_url, data=data).json() #将机器人的回复返回 return req.get('text') except: return#生成微信登录的二维码图片,用户手机扫码登录微信itchat.auto_login()#只对单独给自己发微信消息的好友自动答复,对群消息不自动答复@itchat.msg_register([PICTURE,TEXT])def simple_reply(msg): ''' if msg['Type']==TEXT: ReplyContent='I received message: '+msg['Content'] if msg['Type']==PICTURE: ReplyContent = 'I received picture: '+msg['FileName'] itchat.send_msg('[auto-reply]'+ReplyContent,msg['FromUserName']) ''' #调用函数,发送信息给机器人并将回复返回给respones respones = get_response(msg['Content'], msg['FromUserName']) #将机器人的回复转发给消息发送人,并在前面加上“我是图灵机器人:”区分自动回复与人工回复 itchat.send("我是图灵机器人:"+respones, msg['FromUserName'])itchat.run()
posted on 2018-04-27 09:42 阅读( ...) 评论( ...)