Python結(jié)合wxauto實現(xiàn)智能微信聊天機器人
??一、wxauto簡介
wxauto 是我在2020年開發(fā)的一個基于 UIAutomation 的開源 Python 微信自動化庫。Python 初學(xué)者也可以簡單上手自動化微信操作。目前已實現(xiàn)很多日常的微信操作的自動化,如自動發(fā)送消息、自動添加好友、自動回復(fù)、自動獲取聊天記錄、圖片、文件等功能,后續(xù)還會根據(jù)反饋更新更多功能。
wxauto的github鏈接:https://github.com/cluic/wxauto【點擊跳轉(zhuǎn)】
??二、wxauto的主要功能
- 消息發(fā)送:支持發(fā)送文字、圖片、文件、@群好友、引用消息等功能
- 聊天記錄:可獲取好友的聊天記錄內(nèi)容
- 監(jiān)聽消息:實時獲取指定監(jiān)聽好友(群)的新消息
- 其他定制功能:根據(jù)需求定制自動化流程,滿足各種特殊需求。
??三、wxauto的安裝與使用
1. wxauto的安裝
安裝 wxauto 非常簡單,和其他第三方庫一樣在命令行輸入以下命令即可:
pip install wxauto pip install wxauto -i https://pypi.tuna.tsinghua.edu.cn/simple (換源安裝)
注意: 目前wxauto只支持 Windows 10|11|Server2016+
系統(tǒng),蘋果等電腦的系統(tǒng)并不支持,Python環(huán)境要求 Python:3.7+(不支持3.7.6和3.8.1)
,注意?。?! Python版本不支持3.7.6和3.8.1
,微信版本默認分支為微信3.9.11.17版本,使用前請先檢查自己電腦微信是否為該版本,版本不同可能由于UI問題導(dǎo)致某些功能無法正常調(diào)用。
注意: 如果你的微信版本可以用的話,也不需要過多糾結(jié)這個。
2. wxauto的簡單使用
注意: 在運行代碼前一定要確保PC微信客戶端已經(jīng)登陸。
【示例1】:基于wxauto發(fā)送消息
使用場景:可以重復(fù)發(fā)送一樣的內(nèi)容達到消息轟炸
from wxauto import * wx = WeChat() content = 'hello world' who = '文件傳輸助手' for i in range(15): wx.SendMsg(msg=content, who=who)
附帶@好友的消息
from wxauto import * wx = WeChat() content = 'hello world' who = '文件傳輸助手' name = '文件傳輸助手' wx.SendMsg(msg=content, who=who, at=name) # 要 @ 的人
發(fā)送圖片/視頻/文件消息SendFiles
參數(shù)說明:
參數(shù)名 | 類型 | 默認值 | 說明 |
---|---|---|---|
filepath | str 或 list | / | 指定文件路徑,單個文件str,多個文件list |
who | str | None | 要發(fā)送給誰,默認則發(fā)送給當(dāng)前打開的頁面 |
from wxauto import * wx = WeChat() content = 'hello world' who = '文件傳輸助手' file = [ r'D:\軟件\圖片\荒.png', r'D:\C語言學(xué)習(xí)資料\高質(zhì)量的C-C++編程.pdf' ] wx.SendFiles(filepath=file, who=who)
聊天窗口消息獲取
默認為當(dāng)前聊天窗口
from wxauto import * wx = WeChat() # 獲取當(dāng)前聊天窗口消息 msgs = wx.GetAllMessage() # 輸出消息內(nèi)容 for msg in msgs: if msg.type == 'sys': print(f'【系統(tǒng)消息】{msg.content}') elif msg.type == 'friend': sender = msg.sender_remark # 獲取備注名 print(f'{sender.rjust(20)}:{msg.content}') elif msg.type == 'self': print(f'{msg.sender.ljust(20)}:{msg.content}') elif msg.type == 'time': print(f'\n【時間消息】{msg.time}') elif msg.type == 'recall': print(f'【撤回消息】{msg.content}')
另外LoadMoreMessage
方法用于加載更多歷史消息,配合GetAllMessage
方法使用,實現(xiàn)獲取更多歷史消息。
注意:LoadMoreMessage
方法加載更多歷史消息時,需要保證當(dāng)前聊天窗口有歷史消息,否則沒有效果,即觸發(fā)一次“查看更多消息”。
from wxauto import WeChat wx = WeChat() # 加載更多歷史消息 wx.LoadMoreMessage() # 獲取當(dāng)前聊天窗口消息 msgs = wx.GetAllMessage() # 消息處理邏輯代碼。。。
微信添加好友AddNewFriend
方法用于發(fā)起好友申請。
注意:微信有一定的限制,如果頻繁添加好友,可能會被限制添加好友的權(quán)限,請謹慎使用,切勿濫用!??!
from wxauto import * wx = WeChat() keywords = 's15576806087' # 對方的微信號、手機號、QQ號 addmsg = '你好,我是xxxx' # 添加好友的消息 remark = '備注名字' # 備注名,沒有則不用設(shè)置 tags = ['朋友', '同事'] # 標(biāo)簽列表 # 發(fā)起好友申請 wx.AddNewFriend(keywords, addmsg=addmsg, remark=remark, tags=tags)
獲取好友信息
from wxauto import WeChat wx = WeChat() friend_infos = wx.GetAllFriends() # 獲取好友信息 print(friend_infos)
3. wxauto的消息對象
這個很重要,下面結(jié)合大模型時會用到以下的消息對象。
好友消息
支持屬性:
屬性名 | 類型 | 說明 |
---|---|---|
type | str | 消息類型,固定為friend |
content | str | 消息內(nèi)容 |
sender | str | 發(fā)送者 |
sender_remark | str | 發(fā)送者備注名 |
info | list | 原始消息信息,包含了消息的所有信息 |
control | uiautomation.Control | 該消息的uiautomation控件 |
id | str | 消息id |
msgs = wx.GetAllMessage() for msg in msgs: if msg.type == 'friend': # 消息類型 sender = msg.sender_remark # 獲取備注名 print(f'{sender}:{msg.content}')
自己的消息
支持屬性:
屬性名 | 類型 | 說明 |
---|---|---|
type | str | 消息類型,固定為self |
content | str | 消息內(nèi)容 |
sender | str | 發(fā)送者 |
sender_remark | str | 發(fā)送者備注名 |
info | list | 原始消息信息,包含了消息的所有信息 |
control | uiautomation.Control | 該消息的uiautomation控件 |
id | str | 消息id |
msgs = wx.GetAllMessage() for msg in msgs: if msg.type == 'self': # 消息類型 print(f'{msg.sender}:{msg.content}')
??四、wxauto結(jié)合大模型實現(xiàn)簡單的聊天機器人
這里選用的是百度的千帆大模型,首先登陸進去之后點擊模型廣場,隨便選一個免費的就行。
選擇好模型之后,點進去,點擊那個開通付費【免費的,不要錢,放心點擊】,提交訂單就開通成功啦。
返回到主頁,點擊應(yīng)用接入,記住這里的API Key
和 Secret Key
,點擊創(chuàng)建應(yīng)用。
填寫好應(yīng)用名稱和應(yīng)用描述(隨便填一下就好了),點擊確定。
返回主頁,點擊模型廣場,點擊你之前選擇的模型,點擊API文檔。
往下翻找到對應(yīng)的請求示例的Python代碼,復(fù)制那段代碼。
復(fù)制好代碼后,將你對應(yīng)的API Key
和 Secret Key
給添加上去。
運行一下代碼可以看到,result
就是大模型根據(jù)我們的問題給出的結(jié)果,現(xiàn)在我們只需要將content改成微信中好友發(fā)送過來的消息作為問題給大模型,然后將大模型給出的結(jié)果中的result
提取出來作為內(nèi)容發(fā)送給好友,這樣,一個簡單的微信聊天機器人就完成了。
五、完整代碼
import requests import json from wxauto import WeChat def get_access_token(): """ 使用 API Key,Secret Key 獲取access_token,替換下列示例中的應(yīng)用API Key、應(yīng)用Secret Key """ url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[應(yīng)用API Key]&client_secret=[應(yīng)用Secret Key]" payload = json.dumps("") headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) return response.json().get("access_token") def main(wx1, msg1): url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/ernie-speed-128k?access_token=" + get_access_token() payload = json.dumps({ "messages": [ { "role": "user", "content": msg1 } ] }) headers = { 'Content-Type': 'application/json' } response = requests.request("POST", url, headers=headers, data=payload) json_result = json.loads(response.text) print(json_result) # print(response.text) wx.SendMsg(msg=json_result['result'] + "--此內(nèi)容為AI生成", who="你要發(fā)送的人") if __name__ == '__main__': wx = WeChat() while True: msgs = wx.GetAllMessage() if msgs: if msgs[-1].type == "friend": main(wx, msgs[-1].content)
到此這篇關(guān)于結(jié)合wxauto實現(xiàn)智能微信聊天機器人的文章就介紹到這了,更多相關(guān)wxauto微信聊天機器人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于python制作簡易版學(xué)生信息管理系統(tǒng)
這篇文章主要介紹了基于python制作簡易版學(xué)生信息管理系統(tǒng),文中有非常詳細的代碼示例,對正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下2021-04-04PyCharm遠程調(diào)試代碼配置以及運行參數(shù)設(shè)置方式
這篇文章主要介紹了PyCharm遠程調(diào)試代碼配置以及運行參數(shù)設(shè)置方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01