溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用python實現(xiàn)微信自動回復(fù)機器人功能

發(fā)布時間:2021-04-07 09:48:08 來源:億速云 閱讀:715 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹了使用python實現(xiàn)微信自動回復(fù)機器人功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

一 簡單介紹

wxpy基于itchat,使用了 Web 微信的通訊協(xié)議,,通過大量接口優(yōu)化提升了模塊的易用性,并進行豐富的功能擴展。實現(xiàn)了微信登錄、收發(fā)消息、搜索好友、數(shù)據(jù)統(tǒng)計等功能。

總而言之,可用來實現(xiàn)各種微信個人號的自動化操作。

安裝: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

運行上面的程序,會彈出二維碼,用手機微信掃一掃即可實現(xiàn)登錄。

但上面的程序有一個缺點,每次運行都要掃二維碼。不過wxpy非常貼心地提供了緩存的選項,用于將登錄信息保存下來,就不用每次都掃二維碼,如下

bot = Bot(cache_path=True) # 必須先登錄過一次以后才可以使用緩存

三 微信好友男女比例占比展示

from wxpy import *
from pyecharts import Pie
import webbrowser
bot=Bot(cache_path=True) #注意手機確認(rèn)登錄
friends=bot.friends()
#拿到所有朋友對象,放到列表里
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,屬性所對應(yīng)的值list,is_label_show是否現(xiàn)在標(biāo)簽
pie.render('sex.html')#生成html頁面
# 打開瀏覽器
webbrowser.open("sex.html")

使用python實現(xiàn)微信自動回復(fù)機器人功能

四 微信好友地域分布

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

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

特別注明,中國地圖在 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={}#定義一個字典,用來存放省市以及省市人數(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")

使用python實現(xiàn)微信自動回復(fù)機器人功能

五 微信聊天機器人

1、為微信傳輸助手傳送消息

這里的file_helper就是微信的文件傳輸助手,我們給文件傳輸助手發(fā)送一條消息,可以在手機端的文件傳輸助手中收到括號內(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 '自動回復(fù):%s' %recv_msg.text
# 進入Python命令行,讓程序保持運行
embed()

3、自動給老婆回復(fù)信息

當(dāng)你在網(wǎng)吧吃著雞,操作騷出天際時,你老婆打電話讓你回家吃飯,此時你怎么辦。。

from wxpy import *
bot=Bot(cache_path=True)
girl_friend=bot.search('劉劉劉')[0]
print(girl_friend)
@bot.register() # 接收從指定好友發(fā)來的消息,發(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='老婆最美麗,我對老婆的愛如滔滔江水,連綿不絕'
  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ā)來的消息,發(fā)送者即recv_msg.sender為組
def recv_send_msg(recv_msg):
 print('收到的消息:',recv_msg.text)
 if recv_msg.member == boss:
  #這里不用recv_msg.render 因為render是群的名字
  recv_msg.forward(bot.file_helper,prefix='老板發(fā)言: ')
  return '老板說的好有道理,深受啟發(fā)'
embed()

5、聊天機器人

給所有人自動回復(fù)

import json
import requests
from wxpy import *
bot = Bot(cache_path=True)
# 調(diào)用圖靈機器人API,發(fā)送消息并獲得機器人的回復(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 "[來自智能機器人] " + 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)用圖靈機器人API,發(fā)送消息并獲得機器人的回復(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 "[來自智能機器人] " + 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)用圖靈機器人API,發(fā)送消息并獲得機器人的回復(fù)
def auto_reply(text):
 url = "http://www.tuling123.com/openapi/api"
 api_key = "申請圖靈機器人獲取key值放到這里"
 payload = {
  "key": api_key,
  "info": text,
 }
 r = requests.post(url, data=json.dumps(payload))
 result = json.loads(r.content)
 return "[微信測試,請忽略] " + result["text"]
@bot.register()
def forward_message(msg):
 if msg.sender == girl_friend:
  return auto_reply(msg.text)
embed()

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“使用python實現(xiàn)微信自動回復(fù)機器人功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI