Python使用itchat模組實現群聊轉發,自動回覆功能示例
阿新 • • 發佈:2020-01-09
本文例項講述了Python使用itchat模組實現群聊轉發,自動回覆功能。分享給大家供大家參考,具體如下:
1.itchat自動把好友發來的訊息,回覆給他
僅能實現自動回覆 原文給 好友發來的文字訊息、圖片表情訊息。
#!/usr/bin/python #coding=utf-8 import itchat from itchat.content import * @itchat.msg_register([PICTURE,TEXT]) def simple_reply(msg): if msg['Type'] == TEXT: ReplyContent = 'I received message: '+msg['Content'] if msg['Type'] == PICTURE: ReplyContent = 'I received picture: '+msg['FileName'] itchat.send_msg(ReplyContent,msg['FromUserName']) itchat.auto_login() itchat.run()
這裡註冊了兩個訊息型別,文字和圖片(表情),當微信接收到這兩個訊息時就會進入註冊的函式simple_reply,msg是一個字典型別裡面包含了訊息資料包,有傳送者、接收者、訊息型別、訊息內容等超多的資訊
itchat要註冊訊息型別,比如註冊了TEXT(itchat.content.text),就會接收文字訊息,其他訊息不會觸發函式。訊息型別見庫中的content.py檔案
訊息型別判斷,msg['Type']
訊息發起者,msg['FromUserName']
訊息接收者,msg['ToUserName']
文字訊息,msg['Content']
檔名字,msg['FileName']
2.自動轉發指定的群聊訊息給指定的好友。
應用場景:每天會在微信群內收集訂餐的小夥伴名單,訂餐的回覆+1,
由於時間跨度,群訊息太多,手工上下翻 +1 的訊息難免遺漏,所以這段指令碼正好滿足此需求。
轉發的內容是:群內暱稱:+1
#!/usr/bin/python #coding=UTF-8 import itchat from itchat.content import * @itchat.msg_register([PICTURE,TEXT],isGroupChat=True) def simple_reply(msg): users = itchat.search_friends(name=u'測試23')#通訊錄中好友備註名 userName = users[0]['UserName'] if msg['Content'] == "+1": itchat.send(u'%s\u2005: %s '%(msg['ActualNickName'],msg['Content']),toUserName=userName) itchat.auto_login()#enableCmdQR=True 可以在命令列顯示二維碼 itchat.run()
更多關於Python相關內容感興趣的讀者可檢視本站專題:《Python程序與執行緒操作技巧總結》、《Python Socket程式設計技巧總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總》
希望本文所述對大家Python程式設計有所幫助。