itchat群机器人的自动画实现

总体功能或思想
1、根据 itchat文档 实现简单的聊天回复功能
2、添加敏感词汇过滤、添加一些日常对话的回答
3、获取群成员信息,收到消息后,如果是女同胞发言,则给出提示
4、接入图灵机器人,提升日常会话能力

软件安装:
pip install itchat
pip install itchat_uos

import itchat
import requests
import json
from itchat.content import *

tuling = 1
daily_count = 32
flag = 0

roomslist = []
ChatRoom = {
    
    }

# 你要管理的群的列表
groupList = [
"tee22",
"ARM-Trustzone-TEE-ATF",
"None123"
]

def auto_chat(text):
    data = {
    
    
        "perception": {
    
    
            "inputText": {
    
    
                "text": text
            }
        },
        "userInfo": {
    
    
            "apiKey": "xxxxxxxxxxxxxxx",  ## 你的图灵机器人KEY
            "userId": '123',
        }
    }
    req = json.dumps(data).encode('utf8')
    r = requests.post('http://openapi.tuling123.com/openapi/api/v2', data=req).text
    print(r)
    r=json.loads(r)['results']
    for resutl in r:
        return resutl['values']['text']

def lc():
    global ChatRoom
    print('机器人HH在线')
    
    group  = itchat.get_chatrooms(update=True)
    for g in group:
        print(g['NickName'])
        if g['NickName'] in groupList:
            itchat.send("[机器人HH上线] -- Hello,我是进化版HH,你有什么问题,可以直接问我, 但记得要艾特我哦!", g['UserName'])
    
    for i in getchatrooms():
        roomslist.append(i['NickName'])
    
    for n in roomslist:
        if(n=="ARM-Trustzone-TEE-ATF"):#填你想要分析的群名字
            ChatRoom = itchat.update_chatroom(getroom_message(n), detailedMember=True)
            break
            
def ec():
    print('机器人HH在线退出')

def getroom_message(n):
  #获取群的username,对群成员进行分析需要用到
  itchat.dump_login_status() # 显示所有的群聊信息,默认是返回保存到通讯录中的群聊
  RoomList = itchat.search_chatrooms(name=n)
  if RoomList is None:
    print("%s group is not found!" % (name))
  else:
    return RoomList[0]['UserName']
 
def getchatrooms():
  #获取群聊列表
  roomslist = itchat.get_chatrooms()
  return roomslist

def check_sensitive(message):
    global flag
    replay = ""
    flag = 1
    if u'逼' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'卧槽' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif 'SB' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif 'sb' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'傻' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'草' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'妈' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'MD' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'辣鸡' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'垃圾' in message:
        replay = u'含有敏感词汇,请注意言辞'
    else:
        flag = 0
    
    return replay

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING, PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True)
def group_reply_text(msg):
    global flag
    global ChatRoom
    global tuling
    global daily_count
    print("group_reply_text enter")
    message = msg['Text']    

    for i in ChatRoom['MemberList']:
        if msg['ActualNickName'] == i['NickName']:
            if i['Sex'] == 2:
                print("There's a girl")
                itchat.send("Hint : There is a girl appearing", msg['FromUserName'])
    
    replay = check_sensitive(message)
    if flag == 1:
        itchat.send(replay, msg['FromUserName'])
        return
    
    if 'HH_rebot' not in message:
        return

    if tuling == 1:
        replay = auto_chat(msg['Content'])# 用api得到回复内容
        itchat.send(replay, msg['FromUserName'])
        
        daily_count = daily_count +1
        if daily_count >= 99:
            itchat.send("[进化版HH 下线了] -- Sorry, 我今天聊的太多了,要去休息了!", g['UserName'])
            itchat.send("[机器人HH上线] -- Hello,进化版HH哥哥去睡觉了,接下来你有什么问题,我可以帮着回答, 但记得要艾特我哦!!", g['UserName'])
            tuling = 0;
        return
    
    flag = 0
    if u'干什么' in message:
        replay = u'在忙呢'
    elif u'生气' in message:
        replay = u'生气对身体不好'
    elif u'?' in  message:
        replay = u'哈哈,我也不知道'
    elif u'不理你' in message:
        replay = u'乖,忙完给你买糖吃'
    elif u'涛' in message:
        replay = u'他是我主人'
    elif u'厉害' in message:
        replay = u'不不不,辣鸡一个'
    elif u'你好' in message:
        replay = u'你好哇'
    elif u'好吧' in message:
        replay = u'再见'
    elif u'博客' in message:
        replay = u'您是想看群主博客吗,可参考链接:\n https://blog.csdn.net/weixin_42135087/article/details/107037145'
    elif u'视频' in message:
        replay = u'您是想看群主哔哩哔哩吗,可参考链接:\n https://space.bilibili.com/482877928'
    elif u'你是谁' in message:
        replay = u'我是我们群的机器人啊'
    elif u'名字' in message:
        replay = u'你不会点开我的头像查看吗'
    elif u'叫什么' in message:
        replay = u'我叫HH_rebot'
    elif u'岁' in message:
        replay = u'年龄是我的隐私,不要调戏人家好不'
    else:
        flag = 1
    
    if flag == 1:
        replay = u'对不起,我还不能理解您说的什么。正在进化中....'
    
    itchat.send(replay, msg['FromUserName'])


@itchat.msg_register('Text')
def text_reply(msg):
    message = msg['Text']

    print("11111111111111111111111:" + msg['FromUserName'])
    replay = u'[机器人自动回复]呵呵,您继续吹!'

    if u'干什么' in message:
        replay = u'在忙呢'
    elif u'逼' in message:
        replay = u'含有敏感词汇,请注意言辞'
    elif u'生气' in message:
        replay = u'生气对身体不好'
    elif u'?' in  message:
        replay = u'哈哈,我也不知道'
    elif u'不理你' in message:
        replay = u'乖,忙完给你买糖吃'
    elif u'涛' in message:
        replay = u'他是我主人'
    elif u'厉害' in message:
        replay = u'不不不,辣鸡一个'
    elif u'你好' in message:
        replay = u'你好哇'
    elif u'好吧' in message:
        replay = u'再见'
    #itchat.send("test",toUserName=msg['FromUserName'])
    return replay

itchat.auto_login(loginCallback=lc, exitCallback=ec)
itchat.run()

在这里插入图片描述

参考
1、使用图灵机器人和itchat实现微信自动回复

猜你喜欢

转载自blog.csdn.net/weixin_42135087/article/details/121585183