微信機器人(實現自動回復,數據統計) + 數據可視化


微信機器人的使用

安裝:wxpy模塊、pillow模塊、pyecharts數據可視化模塊(https://pyecharts.org # 官網)

 

顯示中國地圖,需要裝中國地圖模塊:

全球國家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個國家,包括中國地圖
中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個省,5 個自治區
中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 個中國城市
中國縣區級地圖: echarts-china-counties-pypkg (4.1MB):2882 個中國縣·區
中國區域地圖: echarts-china-misc-pypkg (148KB):11 個中國區域地圖,比如華南、華北。

特別注明,中國地圖在 echarts-countries-pypkg 里。需要這些地圖的朋友,可以裝 pip 命令行:

$ pip3 install echarts-countries-pypkg
$ pip3 install echarts-china-provinces-pypkg
$ pip3 install echarts-china-cities-pypkg
$ pip3 install echarts-china-counties-pypkg
$ pip3 install echarts-china-misc-pypkg

 

用pkl模塊保存的朋友數據,刪除可以重掃 



生成餅圖統計好友性別
from wxpy import * # 導入微信機器人模塊所有
from pyecharts import Pie,Map # Pie是餅圖,map是地圖
import webbrowser

'''實例化微信機器人'''
bot = Bot(cache_path=True) # 參數True表示微信登陸后緩存好友信息

friends = bot.friends() # 拿到所有好友對象

'''生成餅圖數據'''
attr = ['男性','女性','未知',]
value=[0,0,0]
for friend in friends:
if friend.sex == 1:
value[0] += 1
elif friend.sex == 2:
value[1] += 1
else:
value[2] += 1

pie = Pie('男女朋友比例') # 實例化對象
pie.add('',attr,value,is_label_show=True) # is_label_show=True 讓效果好看點
pie.render('sex.html')
webbrowser.open('sex.html') # 讓瀏覽器打開一個地址

  

生成地圖統計微信好友分布
from wxpy import * # 導入微信機器人模塊所有
from pyecharts import Map # Pie是餅圖,map是地圖
import webbrowser

bot = Bot(cache_path=True) # 參數True表示微信登陸后緩存好友信息
bro = webbrowser.Chrome
friends = bot.friends() # 拿到所有好友對象


area_dic = {} # 用來統計
for friend in friends:
# province 就是對象里的省份字段
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() # 人數

maps = Map("好友地區分布",width=1200,height=600) # 生成對象
maps.add( # 調用add方法傳入參數
"好友地區分布",
attr,
value,
maptype='china',
is_visualmap=True,) # 傳入兩個參數列表,地區china

maps.render('area.html')

webbrowser.open('area.html')


自動回復消息的簡單應用
from wxpy import *
bot=Bot(cache_path=True)

@bot.register() # 裝飾器裝飾
def recv_send_msg(recv_msg): # 對象傳入參數
print('收到的消息:',recv_msg.text) # 收到的消息
return '自動回復:%s' %recv_msg.text # 回復消息直接return

# 進入Python命令行,讓程序保持運行
embed() # 這個不寫就直接結束程序了


收到指定的人的消息自動回復
from wxpy import *
bot=Bot(cache_path=True)

girl_friend=bot.search('劉劉劉')[0] # 指定接收消息自動回復的好友,模糊匹配放入列表
print(girl_friend)

@bot.register() # 接收從指定好友發來的消息,發送者即recv_msg.sender為指定好友girl_friend
def recv_send_msg(recv_msg):
print('收到的消息:',recv_msg.text) # recv_msg.text取得文本
# sender參數就是誰發給你的消息
if recv_msg.sender == girl_friend: # 當發來的消息對象等於我上面指定的對象
'''下面的幾行就是回復代碼'''
recv_msg.forward(bot.file_helper,prefix='老婆留言: ') # 這個是在文件傳輸助手里留一份消息,方便忙完回頭查看
ms='老婆最美麗,我對老婆的愛如滔滔江水,連綿不絕' # 定義回復的文字
print('>>>給老婆回復的:', ms)
return ms # 直接返回

embed()


給群里指定的人自動回復消息
from wxpy import *
bot=Bot(cache_path=True)

company_group=bot.groups().search('群名字')[0] # 模糊匹配到的群名字是在列表里所以取第0個

boss=company_group.search('老板名字')[0] # 從群對象里模糊匹配指定人的名字,名字是在列表里所以取第0個


@bot.register(chats=company_group) # 這個裝飾器要傳入群對象,就是只有這個群的聊天才能走到后面的程序
def recv_send_msg(recv_msg):
print('收到的消息:',recv_msg.text)
# .member出來的是個人,.sender出來的是群對象,而不是個人
if recv_msg.member == boss: # 當群里有消息進入,就匹配個人是不是自己指定的人,是的舊后面自動回復
#這里不用recv_msg.render 因為render是群的名字
recv_msg.forward(bot.file_helper,prefix='老板發言: ') # 這個是在文件傳輸助手里留一份消息,方便忙完回頭查看
return '老板說的好有道理,深受啟發'

embed()


調用圖靈機器人實現自動聊天
import json
import requests
from wxpy import *
bot = Bot(cache_path=True)

laopo = bot.search('XXX')[0] # 指定自動回復的人

@bot.register()
def forward_message(msg):
if msg.sender == laopo:
return auto_reply(msg.text)



# 調用圖靈機器人API,發送消息並獲得機器人的回復
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)) # 向接口發送post請求,攜帶數據
result = json.loads(r.content)
print()
return result["text"]

embed()

if __name__ == '__main__':
forward_message()



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM