python實(shí)現(xiàn)微信自動(dòng)回復(fù)機(jī)器人功能
一 簡(jiǎn)單介紹
wxpy基于itchat,使用了 Web 微信的通訊協(xié)議,,通過大量接口優(yōu)化提升了模塊的易用性,并進(jìn)行豐富的功能擴(kuò)展。實(shí)現(xiàn)了微信登錄、收發(fā)消息、搜索好友、數(shù)據(jù)統(tǒng)計(jì)等功能。
總而言之,可用來(lái)實(shí)現(xiàn)各種微信個(gè)人號(hào)的自動(dòng)化操作。(http://wxpy.readthedocs.io/zh/latest/bot.html)
安裝:wxpy 支持 Python 3.4-3.6,以及 2.7 版本
pip3 install -U wxpy
安裝 pillow模塊
pip3 install pillow
安裝 pyecharts模塊
pip3 install pyecharts
二 登錄微信
1 、 掃碼登錄微信
from wxpy import * bot = Bot()
2、cache_path=True
運(yùn)行上面的程序,會(huì)彈出二維碼,用手機(jī)微信掃一掃即可實(shí)現(xiàn)登錄。
但上面的程序有一個(gè)缺點(diǎn),每次運(yùn)行都要掃二維碼。不過wxpy非常貼心地提供了緩存的選項(xiàng),用于將登錄信息保存下來(lái),就不用每次都掃二維碼,如下
bot = Bot(cache_path=True) # 必須先登錄過一次以后才可以使用緩存
三 微信好友男女比例占比展示
from wxpy import * from pyecharts import Pie import webbrowser bot=Bot(cache_path=True) #注意手機(jī)確認(rèn)登錄 friends=bot.friends() #拿到所有朋友對(duì)象,放到列表里 attr=['男朋友','女朋友','未知性別'] value=[0,0,0] for friend in friends: if friend.sex == 1: # 等于1代表男性 value[0]+=1 elif friend.sex == 2: #等于2代表女性 value[1]+=1 else: value[2]+=1 pie = Pie("朋友男女比例") pie.add("", attr, value, is_label_show=True) #圖表名稱str,屬性名稱list,屬性所對(duì)應(yīng)的值list,is_label_show是否現(xiàn)在標(biāo)簽 pie.render('sex.html')#生成html頁(yè)面 # 打開瀏覽器 webbrowser.open("sex.html")
四 微信好友地域分布
顯示中國(guó)地圖,需要裝中國(guó)地圖模塊:
全球國(guó)家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個(gè)國(guó)家,包括中國(guó)地圖
中國(guó)省級(jí)地圖: echarts-china-provinces-pypkg (730KB):23 個(gè)省,5 個(gè)自治區(qū)
中國(guó)市級(jí)地圖: echarts-china-cities-pypkg (3.8MB):370 個(gè)中國(guó)城市
中國(guó)縣區(qū)級(jí)地圖: echarts-china-counties-pypkg (4.1MB):2882 個(gè)中國(guó)縣·區(qū)
中國(guó)區(qū)域地圖: echarts-china-misc-pypkg (148KB):11 個(gè)中國(guó)區(qū)域地圖,比如華南、華北。
特別注明,中國(guó)地圖在 echarts-countries-pypkg 里。需要這些地圖的朋友,可以裝 pip 命令行:
pip3installecharts−countries−pypkg pip3installecharts−countries−pypkg pip3 install echarts-china-provinces-pypkg
pip3installecharts−china−cities−pypkg pip3installecharts−china−cities−pypkg pip3 install echarts-china-counties-pypkg
$ pip3 install echarts-china-misc-pypkg
from wxpy import * from pyecharts import Map import webbrowser bot=Bot(cache_path=True) friends=bot.friends() area_dic={}#定義一個(gè)字典,用來(lái)存放省市以及省市人數(shù) for friend in friends: if friend.province not in area_dic: area_dic[friend.province]=1 else: area_dic[friend.province]+=1 attr = area_dic.keys() value = area_dic.values() map = Map("好朋友們的地域分布", width=1200, height=600) map.add( "好友地域分布", attr, value, maptype='china', is_visualmap=True, #結(jié)合體VisualMap ) #is_visualmap -> bool 是否使用視覺映射組件 # map.render('area.html') webbrowser.open("area.html")
五 微信聊天機(jī)器人
1、為微信傳輸助手傳送消息
這里的file_helper就是微信的文件傳輸助手,我們給文件傳輸助手發(fā)送一條消息,可以在手機(jī)端的文件傳輸助手中收到括號(hào)內(nèi)的消息
bot.file_helper.send('lqz say hello')
2、收發(fā)消息@bot.register()
from wxpy import * bot=Bot(cache_path=True) @bot.register() def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) # recv_msg.text取得文本 return '自動(dòng)回復(fù):%s' %recv_msg.text # 進(jìn)入Python命令行,讓程序保持運(yùn)行 embed()
3、自動(dòng)給老婆回復(fù)信息
當(dāng)你在網(wǎng)吧吃著雞,操作騷出天際時(shí),你老婆打電話讓你回家吃飯,此時(shí)你怎么辦。。
from wxpy import * bot=Bot(cache_path=True) girl_friend=bot.search('劉劉劉')[0] print(girl_friend) @bot.register() # 接收從指定好友發(fā)來(lái)的消息,發(fā)送者即recv_msg.sender為指定好友girl_friend def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) # recv_msg.text取得文本 if recv_msg.sender == girl_friend: recv_msg.forward(bot.file_helper,prefix='老婆留言: ') #在文件傳輸助手里留一份,方便自己忙完了回頭查看 ms='老婆最美麗,我對(duì)老婆的愛如滔滔江水,連綿不絕' print('>>>給老婆回復(fù)的:', ms) return ms#給老婆回一份 embed()
4、從微信群里定位好友之拍老板馬屁
from wxpy import * bot=Bot(cache_path=True) company_group=bot.groups().search('群名字')[0] boss=company_group.search('老板名字')[0] @bot.register(chats=company_group) #接收從指定群發(fā)來(lái)的消息,發(fā)送者即recv_msg.sender為組 def recv_send_msg(recv_msg): print('收到的消息:',recv_msg.text) if recv_msg.member == boss: #這里不用recv_msg.render 因?yàn)閞ender是群的名字 recv_msg.forward(bot.file_helper,prefix='老板發(fā)言: ') return '老板說的好有道理,深受啟發(fā)' embed()
5、聊天機(jī)器人
給所有人自動(dòng)回復(fù)
import json import requests from wxpy import * bot = Bot(cache_path=True) # 調(diào)用圖靈機(jī)器人API,發(fā)送消息并獲得機(jī)器人的回復(fù) def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "9df516a74fc443769b233b01e8536a42" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[來(lái)自智能機(jī)器人] " + result["text"] @bot.register() def forward_message(msg): return auto_reply(msg.text) embed()
給指定的群回復(fù)
import json import requests from wxpy import * bot = Bot(cache_path=False) group=bot.groups().search('群名字')[0] print(group) # 調(diào)用圖靈機(jī)器人API,發(fā)送消息并獲得機(jī)器人的回復(fù) def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "9d602fe417464cd18beb2083d064bee6" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[來(lái)自智能機(jī)器人] " + result["text"] @bot.register(chats=group) def forward_message(msg): return auto_reply(msg.text) embed()
給指定的人回復(fù)
import requests from wxpy import * bot = Bot( cache_path=True) girl_friend=bot.search('名字r')[0] # 調(diào)用圖靈機(jī)器人API,發(fā)送消息并獲得機(jī)器人的回復(fù) def auto_reply(text): url = "http://www.tuling123.com/openapi/api" api_key = "申請(qǐng)圖靈機(jī)器人獲取key值放到這里" payload = { "key": api_key, "info": text, } r = requests.post(url, data=json.dumps(payload)) result = json.loads(r.content) return "[微信測(cè)試,請(qǐng)忽略] " + result["text"] @bot.register() def forward_message(msg): if msg.sender == girl_friend: return auto_reply(msg.text) embed()
總結(jié)
以上所述是小編給大家介紹的python實(shí)現(xiàn)微信自動(dòng)回復(fù)機(jī)器人功能 ,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
使用tensorflow框架在Colab上跑通貓狗識(shí)別代碼
這篇文章主要介紹了使用tensorflow框架在Colab上跑通貓狗識(shí)別代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04python實(shí)現(xiàn)AES算法及AES-CFB8加解密源碼
這篇文章主要為大家介紹了python實(shí)現(xiàn)AES算法及AES-CFB8加解密的源碼示例,有需要朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02Python 靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入的實(shí)現(xiàn)示例
Python靜態(tài)導(dǎo)入和動(dòng)態(tài)導(dǎo)入是指導(dǎo)入模塊或模塊內(nèi)部函數(shù)的兩種方式,本文主要介紹了Python 靜態(tài)導(dǎo)入與動(dòng)態(tài)導(dǎo)入的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05使用Pytorch實(shí)現(xiàn)two-head(多輸出)模型的操作
這篇文章主要介紹了使用Pytorch實(shí)現(xiàn)two-head(多輸出)模型的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05