Python 實現(xiàn)微信自動回復(fù)的方法
眾所周知QQ上面是可以設(shè)置自動回復(fù)的,但是微信上面并不可以。最近在學(xué)習(xí)Python,發(fā)現(xiàn)Python的適用范圍是真的很廣,這里使用itchat組件實現(xiàn)微信的自動回復(fù)
1:安裝itchat
pip install itchat
2:簡單實例:
(1):發(fā)送信息
import itchatitchat.auto_login()name = itchat.search_friends(name=u’XX’) #XX表示昵稱或用戶名userName = name[0]['UserName']print(userName )itchat.send_msg(’。。。’, toUserName=userName)
(2):回復(fù)發(fā)給自己的文本消息
import [email protected]_register(itchat.content.TEXT)def text_reply(msg): return msg.textitchat.auto_login()itchat.run()
3:實現(xiàn)微信自動回復(fù)
這里使用到了圖靈機器人 http://www.tuling123.com/
注冊一個賬號添加一個機器人然后根據(jù)api文檔使用接口即可獲得機器人返回值
#獲取圖靈機器人回復(fù)信息def get_msg(msg):apiUrl = ’http://openapi.tuling123.com/openapi/api/v2’data = { 'perception': { 'inputText': { 'text': msg }, }, 'userInfo': { 'apiKey': 'cfada3289203426f842746afdc5c0806', 'userId': 'demo' }}data = json.dumps(data)try:r = requests.post(apiUrl,data = data).json()return r[’results’][0][’values’][’text’]except:return ’’#正常消息自動回復(fù)@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])def text_reply(msg):print(msg.type)#設(shè)置默認回復(fù)defaultmsg = ’你好’#獲取圖靈機器人的回復(fù)信息reply = get_msg(msg[’Text’])#如果圖靈機器人回復(fù)信息有誤則使用默認回復(fù)replymsg = reply or defaultmsgreturn replymsg#音頻,圖片自動回復(fù)@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])def download_files(msg): msg.download(msg.fileName) typeSymbol = { PICTURE: ’img’, VIDEO: ’vid’, }.get(msg.type, ’fil’) return ’@%s@%s’ % (typeSymbol, msg.fileName)#好友請求,自動添加并打招呼@itchat.msg_register(FRIENDS)def add_friend(msg): msg.user.verify() msg.user.send(’Nice to meet you!’)#群消息自動回復(fù)@itchat.msg_register(TEXT, isGroupChat=True)def text_reply(msg):#設(shè)置默認回復(fù)defaultmsg = ’你好’#獲取圖靈機器人的回復(fù)信息reply = get_msg(msg[’Text’])#如果圖靈機器人回復(fù)信息有誤則使用默認回復(fù)replymsg = reply or defaultmsgreturn replymsgitchat.auto_login(hotReload=True)itchat.run(True)
以上就是Python 實現(xiàn)微信自動回復(fù)的方法的詳細內(nèi)容,更多關(guān)于python 微信自動回復(fù)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Jsp中request的3個基礎(chǔ)實踐2. jsp EL表達式詳解3. XML入門的常見問題(一)4. Django程序的優(yōu)化技巧5. Python多線程操作之互斥鎖、遞歸鎖、信號量、事件實例詳解6. chat.asp聊天程序的編寫方法7. Django ORM實現(xiàn)按天獲取數(shù)據(jù)去重求和例子8. idea設(shè)置自動導(dǎo)入依賴的方法步驟9. IntelliJ IDEA 統(tǒng)一設(shè)置編碼為utf-8編碼的實現(xiàn)10. 怎樣才能用js生成xmldom對象,并且在firefox中也實現(xiàn)xml數(shù)據(jù)島?
