生活随笔
收集整理的這篇文章主要介紹了
itchat群机器人的自动画实现
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總體功能或思想
1、根據(jù) itchat文檔 實(shí)現(xiàn)簡(jiǎn)單的聊天回復(fù)功能
2、添加敏感詞匯過濾、添加一些日常對(duì)話的回答
3、獲取群成員信息,收到消息后,如果是女同胞發(fā)言,則給出提示
4、接入圖靈機(jī)器人,提升日常會(huì)話能力
…
軟件安裝:
pip install itchat
pip install itchat_uos
import itchat
import requests
import json
from itchat
.content import
*tuling
= 1
daily_count
= 32
flag
= 0roomslist
= []
ChatRoom
= {}# 你要管理的群的列表
groupList
= [
"tee22",
"ARM-Trustzone-TEE-ATF",
"None123"
]def
auto_chat(text
):data
= {"perception": {"inputText": {"text": text
}},"userInfo": {"apiKey": "xxxxxxxxxxxxxxx", ## 你的圖靈機(jī)器人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('機(jī)器人HH在線')group
= itchat
.get_chatrooms(update
=True
)for g in group
:print(g
['NickName'])if g
['NickName'] in groupList
:itchat
.send("[機(jī)器人HH上線] -- Hello,我是進(jìn)化版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
)breakdef
ec():print('機(jī)器人HH在線退出')def
getroom_message(n
):#獲取群的username,對(duì)群成員進(jìn)行分析需要用到itchat
.dump_login_status() # 顯示所有的群聊信息,默認(rèn)是返回保存到通訊錄中的群聊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 roomslistdef
check_sensitive(message
):global flagreplay
= ""flag
= 1if u
'逼' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'臥槽' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif
'SB' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif
'sb' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'傻' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'草' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'媽' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'MD' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'辣雞' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'垃圾' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'else:flag
= 0return replay@itchat
.msg_register([TEXT
, MAP
, CARD
, NOTE
, SHARING
, PICTURE
, RECORDING
, ATTACHMENT
, VIDEO
], isGroupChat
=True
)
def
group_reply_text(msg
):global flagglobal ChatRoomglobal tulingglobal 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'])returnif 'HH_rebot' not in message
:returnif tuling
== 1:replay
= auto_chat(msg
['Content'])# 用api得到回復(fù)內(nèi)容itchat
.send(replay
, msg
['FromUserName'])daily_count
= daily_count
+1if daily_count
>= 99:itchat
.send("[進(jìn)化版HH 下線了] -- Sorry, 我今天聊的太多了,要去休息了!", g
['UserName'])itchat
.send("[機(jī)器人HH上線] -- Hello,進(jìn)化版HH哥哥去睡覺了,接下來你有什么問題,我可以幫著回答, 但記得要艾特我哦!!", g
['UserName'])tuling
= 0;returnflag
= 0if u
'干什么' in message
:replay
= u
'在忙呢'elif u
'生氣' in message
:replay
= u
'生氣對(duì)身體不好'elif u
'?' in message
:replay
= u
'哈哈,我也不知道'elif u
'不理你' in message
:replay
= u
'乖,忙完給你買糖吃'elif u
'濤' in message
:replay
= u
'他是我主人'elif u
'厲害' in message
:replay
= u
'不不不,辣雞一個(gè)'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
'我是我們?nèi)旱臋C(jī)器人啊'elif u
'名字' in message
:replay
= u
'你不會(huì)點(diǎn)開我的頭像查看嗎'elif u
'叫什么' in message
:replay
= u
'我叫HH_rebot'elif u
'歲' in message
:replay
= u
'年齡是我的隱私,不要調(diào)戲人家好不'else:flag
= 1if flag
== 1:replay
= u
'對(duì)不起,我還不能理解您說的什么。正在進(jìn)化中....'itchat
.send(replay
, msg
['FromUserName'])@itchat
.msg_register('Text')
def
text_reply(msg
):message
= msg
['Text']print("11111111111111111111111:" + msg
['FromUserName'])replay
= u
'[機(jī)器人自動(dòng)回復(fù)]呵呵,您繼續(xù)吹!'if u
'干什么' in message
:replay
= u
'在忙呢'elif u
'逼' in message
:replay
= u
'含有敏感詞匯,請(qǐng)注意言辭'elif u
'生氣' in message
:replay
= u
'生氣對(duì)身體不好'elif u
'?' in message
:replay
= u
'哈哈,我也不知道'elif u
'不理你' in message
:replay
= u
'乖,忙完給你買糖吃'elif u
'濤' in message
:replay
= u
'他是我主人'elif u
'厲害' in message
:replay
= u
'不不不,辣雞一個(gè)'elif u
'你好' in message
:replay
= u
'你好哇'elif u
'好吧' in message
:replay
= u
'再見'#itchat.send("test",toUserName=msg['FromUserName'])return replayitchat
.auto_login(loginCallback
=lc
, exitCallback
=ec
)
itchat
.run()
參考
1、使用圖靈機(jī)器人和itchat實(shí)現(xiàn)微信自動(dòng)回復(fù)
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的itchat群机器人的自动画实现的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。