1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python itchat微信机器人自动回复

Python itchat微信机器人自动回复

时间:2021-03-31 16:33:20

相关推荐

Python itchat微信机器人自动回复

**

Python itchat微信机器人自动回复

**

第一次写博客,以后会继续更新;

以下是使用Pythobn写的自动微信回复功能,目前支持设置离开状态及机器人回复状态,适合新手学习;

环境:WIN7,Python 3.5

软件:PYcharm

使用库:itchat,request

import itchatfrom itchat.content import *import requests# 二维码登录 使用缓存登录itchat.auto_login(hotReload=True)# itchat.auto_login()# 获取用户自己的IDmyUserName = itchat.get_friends(update=True)[0]['UserName']# print(myUserName)# 自动回复及使用机器人回复auto_reply = Falserobot_reply = False# 为避免机器人使用频繁设置机器人回复名单,可自动添加noreply_list = []# 加载图灵机器人回复模块def tuling_reply(msg):api_url='/openapi/api'data={'key':'01735be58f374e1b828b68c4aed5c230','info':msg,'userid':'Kevin'}# ,verify=False 关闭SSL证书验证reply_msg=requests.post(api_url,verify=False,data=data).json()print(reply_msg)return reply_msg# 获取收到的微信消息并回复@itchat.msg_register(TEXT,isFriendChat=True,isMpChat=True)def reply(msg):global auto_reply,robot_reply,noreply_list,reply_msgprint(msg['Content'] )if msg['FromUserName'] == myUserName and msg['Content'] == u'开启自动回复':auto_reply = Trueitchat.send('自动回复已经开启',toUserName='filehelper')if msg['FromUserName']== myUserName and msg['Content'] == u'关闭自动回复':auto_reply = Falseitchat.send('自动回复已经关闭',toUserName='filehelper')if msg['FromUserName'] == myUserName and msg['Content'] == u'开启机器人':robot_reply = Trueauto_reply = Trueitchat.send('机器人已经开启',toUserName='filehelper')if msg['FromUserName']== myUserName and msg['Content'] == u'关闭机器人':robot_reply = Falseitchat.send('机器人已经关闭',toUserName='filehelper')if msg['FromUserName']== myUserName and msg['Content'] == u'查询状态':itchat.send('自动回复状态:%s , 机器人状态: %s'%(auto_reply,robot_reply), toUserName='filehelper')if msg['FromUserName'] ==myUserName and msg['Content'] =='help':itchat.send('开启自动回复:KQHF \n关闭自动回复:GBHF\n开启机器人:KQBOT\n关闭机器人:GBBOT', toUserName='filehelper')# print(robot_reply)# print(noreply_list)#设置机器人回复白名单 noreply_listif msg['FromUserName'] == myUserName and msg['Content'] == u'bot status change':if msg['ToUserName'] not in noreply_list:noreply_list.append(msg['ToUserName'])itchat.send('关闭%s 机器人回复功能'%(msg['ToUserName']),toUserName='filehelper')elif msg['ToUserName'] in noreply_list:noreply_list.remove(msg['ToUserName'])itchat.send('开启%s 机器人回复功能' % (msg['ToUserName']), toUserName='filehelper')#自动回复功能if auto_reply ==True :if robot_reply==True:if msg['FromUserName'] not in noreply_list:reply_msg=tuling_reply(msg['Content'])itchat.send(reply_msg['text'],toUserName=msg['FromUserName'])else:itchat.send('你好,我现在有事离开,', toUserName=msg['FromUserName'])itchat.run()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。