Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法
眾所周知QQ上面是可以設(shè)置自動(dòng)回復(fù)的,但是微信上面并不可以。最近在學(xué)習(xí)Python,發(fā)現(xiàn)Python的適用范圍是真的很廣,這里使用itchat組件實(shí)現(xiàn)微信的自動(dòng)回復(fù)
1:安裝itchat
pip install itchat
2:簡(jiǎn)單實(shí)例:
(1):發(fā)送信息
import itchatitchat.auto_login()name = itchat.search_friends(name=u’XX’) #XX表示昵稱(chēng)或用戶(hù)名userName = name[0]['UserName']print(userName )itchat.send_msg(’。。。’, toUserName=userName)
(2):回復(fù)發(fā)給自己的文本消息
import itchat@itchat.msg_register(itchat.content.TEXT)def text_reply(msg): return msg.textitchat.auto_login()itchat.run()
3:實(shí)現(xiàn)微信自動(dòng)回復(fù)
這里使用到了圖靈機(jī)器人 http://www.tuling123.com/
注冊(cè)一個(gè)賬號(hào)添加一個(gè)機(jī)器人然后根據(jù)api文檔使用接口即可獲得機(jī)器人返回值
#獲取圖靈機(jī)器人回復(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 ’’#正常消息自動(dòng)回復(fù)@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING])def text_reply(msg):print(msg.type)#設(shè)置默認(rèn)回復(fù)defaultmsg = ’你好’#獲取圖靈機(jī)器人的回復(fù)信息reply = get_msg(msg[’Text’])#如果圖靈機(jī)器人回復(fù)信息有誤則使用默認(rèn)回復(fù)replymsg = reply or defaultmsgreturn replymsg#音頻,圖片自動(dòng)回復(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)#好友請(qǐng)求,自動(dòng)添加并打招呼@itchat.msg_register(FRIENDS)def add_friend(msg): msg.user.verify() msg.user.send(’Nice to meet you!’)#群消息自動(dòng)回復(fù)@itchat.msg_register(TEXT, isGroupChat=True)def text_reply(msg):#設(shè)置默認(rèn)回復(fù)defaultmsg = ’你好’#獲取圖靈機(jī)器人的回復(fù)信息reply = get_msg(msg[’Text’])#如果圖靈機(jī)器人回復(fù)信息有誤則使用默認(rèn)回復(fù)replymsg = reply or defaultmsgreturn replymsgitchat.auto_login(hotReload=True)itchat.run(True)
以上就是Python 實(shí)現(xiàn)微信自動(dòng)回復(fù)的方法的詳細(xì)內(nèi)容,更多關(guān)于python 微信自動(dòng)回復(fù)的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單2. 博客日志摘要暨RSS技術(shù)3. JSP的Cookie在登錄中的使用4. ASP腳本組件實(shí)現(xiàn)服務(wù)器重啟5. ASP常用日期格式化函數(shù) FormatDate()6. JSP之表單提交get和post的區(qū)別詳解及實(shí)例7. SSM框架整合JSP中集成easyui前端ui項(xiàng)目開(kāi)發(fā)示例詳解8. 解析原生JS getComputedStyle9. 使用XSL將XML文檔中的CDATA注釋輸出為HTML文本10. XML解析錯(cuò)誤:未組織好 的解決辦法
