python實(shí)現(xiàn)微信小程序自動(dòng)回復(fù)
本文是使用Python的itchat模塊進(jìn)行微信私聊消息以及群消息的自動(dòng)回復(fù)功能,必須在自己的微信中添加微信號(hào)xiaoice-ms(微軟的微信機(jī)器人)才能實(shí)現(xiàn),直接復(fù)制代碼運(yùn)行之后掃一掃二維碼即可,經(jīng)過(guò)測(cè)試,該程序能夠保持幾小時(shí)的時(shí)間。
實(shí)現(xiàn)原理,將別人發(fā)送給你的消息轉(zhuǎn)發(fā)給AI小冰,然后再將AI的回復(fù)轉(zhuǎn)回給那個(gè)人。
群消息也是如此,此外還添加了新年問(wèn)候語(yǔ),是否與他人進(jìn)行AI聊天等功能。不過(guò)只能實(shí)現(xiàn)文本消息以及微信自帶表情的轉(zhuǎn)發(fā),不能轉(zhuǎn)發(fā)表情、語(yǔ)音等。
# -*-coding:utf-8-*-
import itchat
import itchat.content as itcontent
# 登錄網(wǎng)頁(yè)微信,hotReload=True 能讓登錄時(shí)間加長(zhǎng)
itchat.auto_login(hotReload=True)
# 記錄公眾號(hào)機(jī)器人小冰的UserName
mps = itchat.search_mps(name='小冰')
AI = mps[0]['UserName']
# print(AI)
# 記錄自己的UserName,不然發(fā)送消息會(huì)發(fā)兩遍
username = itchat.get_friends()
user = username[0]['UserName']
# print(username)
# 記錄好友列表里好友的 UserName
friendsname = [friend['UserName'] for friend in username if friend['UserName'] != user]
# print(friendsname)
groupname = itchat.get_chatrooms()
groups = [group['UserName'] for group in groupname]
# 這個(gè)說(shuō)來(lái)話長(zhǎng)~~,有興趣的可以去上網(wǎng)查查
@itchat.msg_register(itcontent.TEXT, isFriendChat=True, isMpChat=True, isGroupChat=True)
def simple_reply(msg, FriendList=[]):
Fromuser = msg['FromUserName']
# 如果是AI而且列表不為空,就將AI發(fā)給自己的消息轉(zhuǎn)發(fā)給發(fā)送消息者
if msg['FromUserName'] == AI and FriendList:
# print(msg['FromUserName'])
itchat.send(msg['Text'], toUserName=FriendList[-1])
elif Fromuser in friendsname:
if '新年' in msg['Text']:
return '新年快樂(lè),祝您身體健康,萬(wàn)事勝意。'
# 記錄發(fā)送消息者入FriendList中
elif Fromuser not in FriendList and msg['Text'] == '小小冰真漂亮':
FriendList.append(Fromuser)
return '通信建立成功'
# 第一次發(fā)送消息過(guò)來(lái),回復(fù)以下內(nèi)容
elif Fromuser in FriendList:
if msg['Text'] in ['小小冰再見(jiàn)', '小小冰晚安', '小小冰下次聊']:
FriendList.remove(Fromuser)
return '再見(jiàn),和您聊天十分開(kāi)心,希望您今天過(guò)得愉快!'
else:
FriendList.append(Fromuser)
itchat.send(msg['Text'], toUserName=AI)
else:
text = '''Mr.D先生現(xiàn)在不在,我是助手AI,有要事請(qǐng)撥打號(hào)碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說(shuō)"小小冰真漂亮
(回復(fù)‘小小冰再見(jiàn)/小小冰晚安/小小冰下次聊'可結(jié)束此次聊天。)"'''
return text
elif Fromuser in groups:
if msg.isAt:
if '新年' in msg['Content']:
return '新年快樂(lè),祝您身體健康,萬(wàn)事勝意。'
elif Fromuser not in FriendList and msg['Content'] == '小小冰真漂亮':
FriendList.append(Fromuser)
return '通信建立成功'
elif Fromuser in FriendList:
if msg['Content'] in ['小小冰再見(jiàn)', '小小冰晚安', '小小冰下次聊']:
FriendList.remove(Fromuser)
return '再見(jiàn),和您聊天十分開(kāi)心,希望您今天過(guò)得愉快!'
else:
FriendList.append(Fromuser)
itchat.send(msg['Content'], toUserName=AI)
else:
text = '''Mr.D先生現(xiàn)在不在,我是助手AI,有要事請(qǐng)撥打號(hào)碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說(shuō)"小小冰真漂亮
(回復(fù)‘小小冰再見(jiàn)/小小冰晚安/小小冰下次聊'可結(jié)束此次聊天。)"'''
return text
elif msg['Text'] == '小小冰真漂亮':
FriendList.append(Fromuser)
return '通信建立成功'
elif Fromuser in FriendList:
if msg['Text'] in ['小小冰再見(jiàn)', '小小冰晚安', '小小冰下次聊']:
FriendList.clear()
return '再見(jiàn),和您聊天十分開(kāi)心,希望您今天過(guò)得愉快!'
elif '新年' in msg['Text']:
return '新年快樂(lè),祝您身體健康,萬(wàn)事勝意。'
else:
FriendList.append(Fromuser)
itchat.send(msg['Text'], toUserName=AI)
# 如果是自己發(fā)送消息,則清空列表
elif Fromuser == user:
FriendList.clear()
# 其他公眾號(hào)信息,就通知一聲給微信文件助手
else:
itchat.send('公眾號(hào)信息', toUserName='filehelper')
@itchat.msg_register([itcontent.PICTURE, itcontent.RECORDING, itcontent.VIDEO, itcontent.MAP], isFriendChat=True,
isGroupChat=True, isMpChat=True)
def return_text(msg):
text = '我不具備識(shí)別語(yǔ)音與圖片等功能,請(qǐng)說(shuō)普通話。'
return text
itchat.run()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 10分鐘教你用Python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
- python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
- python itchat實(shí)現(xiàn)微信自動(dòng)回復(fù)的示例代碼
- 利用python微信庫(kù)itchat實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
- python微信公眾號(hào)之關(guān)鍵詞自動(dòng)回復(fù)
- python實(shí)現(xiàn)微信機(jī)器人: 登錄微信、消息接收、自動(dòng)回復(fù)功能
- Python中re.compile函數(shù)的使用方法
- 關(guān)于Python中compile() 函數(shù)簡(jiǎn)單實(shí)用示例詳解
- Python正則表達(dá)式re.compile()和re.findall()詳解
- Python 正則 re.compile 真的必需嗎
- Python中請(qǐng)不要再用re.compile了
- python內(nèi)置函數(shù)compile(),complex()的使用
相關(guān)文章
Python使用pyinstaller打包含有g(shù)ettext?locales語(yǔ)言環(huán)境的項(xiàng)目(推薦)
最近在用 pyhton 做一個(gè)圖片處理的小工具,順便接觸了gettext,用來(lái)實(shí)現(xiàn)本地化化中英文轉(zhuǎn)換,本文通過(guò)一個(gè)項(xiàng)目給大家詳細(xì)介紹下,感興趣的朋友跟隨小編一起看看吧2022-01-01
xadmin使用formfield_for_dbfield函數(shù)過(guò)濾下拉表單實(shí)例
這篇文章主要介紹了xadmin使用formfield_for_dbfield函數(shù)過(guò)濾下拉表單實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04
在keras里面實(shí)現(xiàn)計(jì)算f1-score的代碼
這篇文章主要介紹了在keras里面實(shí)現(xiàn)計(jì)算f1-score的代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06
Python設(shè)計(jì)模式之門(mén)面模式簡(jiǎn)單示例
這篇文章主要介紹了Python設(shè)計(jì)模式之門(mén)面模式,簡(jiǎn)單描述了門(mén)面模式的概念、原理,并結(jié)合實(shí)例形式給出了Python定義與使用門(mén)面模式的具體操作技巧,需要的朋友可以參考下2018-01-01
Mac更新python3.12?解決pip3安裝報(bào)錯(cuò)問(wèn)題小結(jié)
Mac使用homebrew更新了python3.12,刪除了以前的版本和pip3安裝軟件時(shí)候報(bào)錯(cuò),下面小編給大家分享Mac更新python3.12?解決pip3安裝報(bào)錯(cuò)問(wèn)題,感興趣的朋友跟隨小編一起看看吧2024-05-05

