python實現(xiàn)微信小程序自動回復
本文是使用Python的itchat模塊進行微信私聊消息以及群消息的自動回復功能,必須在自己的微信中添加微信號xiaoice-ms(微軟的微信機器人)才能實現(xiàn),直接復制代碼運行之后掃一掃二維碼即可,經(jīng)過測試,該程序能夠保持幾小時的時間。
實現(xiàn)原理,將別人發(fā)送給你的消息轉(zhuǎn)發(fā)給AI小冰,然后再將AI的回復轉(zhuǎn)回給那個人。
群消息也是如此,此外還添加了新年問候語,是否與他人進行AI聊天等功能。不過只能實現(xiàn)文本消息以及微信自帶表情的轉(zhuǎn)發(fā),不能轉(zhuǎn)發(fā)表情、語音等。
# -*-coding:utf-8-*- import itchat import itchat.content as itcontent # 登錄網(wǎng)頁微信,hotReload=True 能讓登錄時間加長 itchat.auto_login(hotReload=True) # 記錄公眾號機器人小冰的UserName mps = itchat.search_mps(name='小冰') AI = mps[0]['UserName'] # print(AI) # 記錄自己的UserName,不然發(fā)送消息會發(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] # 這個說來話長~~,有興趣的可以去上網(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ā)送消息過來,回復以下內(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,有要事請撥打號碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說"小小冰真漂亮 (回復‘小小冰再見/小小冰晚安/小小冰下次聊'可結(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,有要事請撥打號碼:xxxxxxxxxxx。如果想和我聊天,那就大聲地說"小小冰真漂亮 (回復‘小小冰再見/小小冰晚安/小小冰下次聊'可結(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() # 其他公眾號信息,就通知一聲給微信文件助手 else: itchat.send('公眾號信息', toUserName='filehelper') @itchat.msg_register([itcontent.PICTURE, itcontent.RECORDING, itcontent.VIDEO, itcontent.MAP], isFriendChat=True, isGroupChat=True, isMpChat=True) def return_text(msg): text = '我不具備識別語音與圖片等功能,請說普通話。' return text itchat.run()
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- 10分鐘教你用Python實現(xiàn)微信自動回復功能
- python實現(xiàn)微信自動回復功能
- python itchat實現(xiàn)微信自動回復的示例代碼
- 利用python微信庫itchat實現(xiàn)微信自動回復功能
- python微信公眾號之關鍵詞自動回復
- python實現(xiàn)微信機器人: 登錄微信、消息接收、自動回復功能
- Python中re.compile函數(shù)的使用方法
- 關于Python中compile() 函數(shù)簡單實用示例詳解
- Python正則表達式re.compile()和re.findall()詳解
- Python 正則 re.compile 真的必需嗎
- Python中請不要再用re.compile了
- python內(nèi)置函數(shù)compile(),complex()的使用
相關文章
python3的UnicodeDecodeError解決方法
這篇文章主要介紹了python3的UnicodeDecodeError解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12python內(nèi)置函數(shù)breakpoint()與bytearray()示例詳解
本文給大家分享的是python內(nèi)置函數(shù)breakpoint()與bytearray()的相關資料,并給大家附上了詳細代碼,有需要的小伙伴可以參考下2017-04-04淺談python中copy和deepcopy中的區(qū)別
Python學習過程中會遇到許多問題,最近對copy和deepcopy略感困惑,下面對其進行解答,需要的朋友可以參考。2017-10-10python?tkinter自定義實現(xiàn)Expander控件
和其他成熟的GUI庫相比,tkinter的組件并不是太多,但在自定義組件這一點上,并不遜色于其他框架,下面小編就教大家如何自定義一個Expander控件吧2023-08-08