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)過測(cè)試,該程序能夠保持幾小時(shí)的時(shí)間。
實(shí)現(xiàn)原理,將別人發(fā)送給你的消息轉(zhuǎn)發(fā)給AI小冰,然后再將AI的回復(fù)轉(zhuǎn)回給那個(gè)人。
群消息也是如此,此外還添加了新年問候語,是否與他人進(jìn)行AI聊天等功能。不過只能實(shí)現(xiàn)文本消息以及微信自帶表情的轉(zhuǎn)發(fā),不能轉(zhuǎn)發(fā)表情、語音等。
# -*-coding:utf-8-*- import itchat import itchat.content as itcontent # 登錄網(wǎng)頁微信,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è)說來話長(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 '新年快樂,祝您身體健康,萬事勝意。' # 記錄發(fā)送消息者入FriendList中 elif Fromuser not in FriendList and msg['Text'] == '小小冰真漂亮': FriendList.append(Fromuser) return '通信建立成功' # 第一次發(fā)送消息過來,回復(fù)以下內(nèi)容 elif Fromuser in FriendList: if msg['Text'] in ['小小冰再見', '小小冰晚安', '小小冰下次聊']: FriendList.remove(Fromuser) return '再見,和您聊天十分開心,希望您今天過得愉快!' else: FriendList.append(Fromuser) itchat.send(msg['Text'], toUserName=AI) else: text = '''Mr.D先生現(xiàn)在不在,我是助手AI,有要事請(qǐng)撥打號(hào)碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說"小小冰真漂亮 (回復(fù)‘小小冰再見/小小冰晚安/小小冰下次聊'可結(jié)束此次聊天。)"''' return text elif Fromuser in groups: if msg.isAt: if '新年' in msg['Content']: return '新年快樂,祝您身體健康,萬事勝意。' elif Fromuser not in FriendList and msg['Content'] == '小小冰真漂亮': FriendList.append(Fromuser) return '通信建立成功' elif Fromuser in FriendList: if msg['Content'] in ['小小冰再見', '小小冰晚安', '小小冰下次聊']: FriendList.remove(Fromuser) return '再見,和您聊天十分開心,希望您今天過得愉快!' else: FriendList.append(Fromuser) itchat.send(msg['Content'], toUserName=AI) else: text = '''Mr.D先生現(xiàn)在不在,我是助手AI,有要事請(qǐng)撥打號(hào)碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說"小小冰真漂亮 (回復(fù)‘小小冰再見/小小冰晚安/小小冰下次聊'可結(jié)束此次聊天。)"''' return text elif msg['Text'] == '小小冰真漂亮': FriendList.append(Fromuser) return '通信建立成功' elif Fromuser in FriendList: if msg['Text'] in ['小小冰再見', '小小冰晚安', '小小冰下次聊']: FriendList.clear() return '再見,和您聊天十分開心,希望您今天過得愉快!' elif '新年' in msg['Text']: return '新年快樂,祝您身體健康,萬事勝意。' 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í)別語音與圖片等功能,請(qǐng)說普通話。' 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微信庫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)文章
python3的UnicodeDecodeError解決方法
這篇文章主要介紹了python3的UnicodeDecodeError解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12python內(nèi)置函數(shù)breakpoint()與bytearray()示例詳解
本文給大家分享的是python內(nèi)置函數(shù)breakpoint()與bytearray()的相關(guān)資料,并給大家附上了詳細(xì)代碼,有需要的小伙伴可以參考下2017-04-04淺談python中copy和deepcopy中的區(qū)別
Python學(xué)習(xí)過程中會(huì)遇到許多問題,最近對(duì)copy和deepcopy略感困惑,下面對(duì)其進(jìn)行解答,需要的朋友可以參考。2017-10-10讓你的Python代碼實(shí)現(xiàn)類型提示功能
今天小編就為大家分享一篇讓你的Python代碼實(shí)現(xiàn)類型提示功能,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11深入理解Python虛擬機(jī)中的Code?obejct
在本篇文章當(dāng)中主要給大家深入介紹在?cpython?當(dāng)中非常重要的一個(gè)數(shù)據(jù)結(jié)構(gòu)?code?object!?我們簡(jiǎn)單介紹了一下在?code?object?當(dāng)中有哪些字段以及這些字段的簡(jiǎn)單含義,在本篇文章當(dāng)中將會(huì)舉一些例子以便更加深入理解這些字段2023-04-04python?tkinter自定義實(shí)現(xiàn)Expander控件
和其他成熟的GUI庫相比,tkinter的組件并不是太多,但在自定義組件這一點(diǎn)上,并不遜色于其他框架,下面小編就教大家如何自定義一個(gè)Expander控件吧2023-08-08