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. IDEA 2020.1.2 安裝教程附破解教程詳解2. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算3. Java利用TCP協(xié)議實(shí)現(xiàn)客戶(hù)端與服務(wù)器通信(附通信源碼)4. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶(hù)登錄的步驟5. JS圖片懶加載庫(kù)VueLazyLoad詳解6. Java實(shí)現(xiàn)的迷宮游戲7. Python 解決火狐瀏覽器不彈出下載框直接下載的問(wèn)題8. django queryset相加和篩選教程9. Java PreparedStatement用法詳解10. Spring如何集成ibatis項(xiàng)目并實(shí)現(xiàn)dao層基類(lèi)封裝

網(wǎng)公網(wǎng)安備