欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python itchat微信机器人自动回复

程序员文章站 2022-07-13 08:52:11
...

**

Python itchat微信机器人自动回复

**

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

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

环境:WIN7,Python 3.5
软件:PYcharm
使用库:itchat,request

    import itchat
    from  itchat.content import *
    import requests
    
        # 二维码登录 使用缓存登录
    itchat.auto_login(hotReload=True)
    # itchat.auto_login()
    
    # 获取用户自己的ID
    myUserName = itchat.get_friends(update=True)[0]['UserName']
    # print(myUserName)
    
    # 自动回复及使用机器人回复
    auto_reply = False
    robot_reply = False
    
    # 为避免机器人使用频繁设置机器人回复名单,可自动添加
    noreply_list = []
    
    # 加载图灵机器人回复模块
    def tuling_reply(msg):
        api_url='http://www.tuling123.com/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_msg
        print(msg['Content'] )
        
        if msg['FromUserName'] == myUserName and msg['Content'] == u'开启自动回复':
            auto_reply = True
            itchat.send('自动回复已经开启',toUserName='filehelper')
            
        if  msg['FromUserName']== myUserName and msg['Content'] == u'关闭自动回复':
            auto_reply = False
            itchat.send('自动回复已经关闭',toUserName='filehelper')
            
        if msg['FromUserName'] == myUserName and msg['Content'] == u'开启机器人':
            robot_reply = True
            auto_reply = True
            itchat.send('机器人已经开启',toUserName='filehelper')
            
        if  msg['FromUserName']== myUserName and msg['Content'] == u'关闭机器人':
            robot_reply = False
            itchat.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_list
        if  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()