利用AI大模型与wxauto搭建智能微信对话机器人
文章目录
-
-
- 🤔一、wxauto概览
- 🎯二、wxauto的核心功能
- 📦三、wxauto的安装与运用
-
- 1. wxauto的安装
- 2. wxauto的基础运用
- 3. wxauto的消息对象
- 💻四、wxauto结合大模型打造简易聊天机器人
- 🎧五、完整代码
-
🤔一、wxauto概览
wxauto是一款基于UIAutomation的开源Python微信自动化库。Python新手也能轻松上手进行微信自动化操作。目前已实现诸多日常微信操作的自动化,像自动发送消息、自动添加好友、自动回复、自动获取聊天记录、图片、文件等功能,后续会依据反馈持续更新更多功能。
wxauto的github链接:https://github.com/cluic/wxauto【点击跳转】
🎯二、wxauto的核心功能
- 消息发送:支持发送文字、图片、文件、@群好友、引用消息等操作
- 聊天记录:可获取好友的聊天记录内容
- 消息监听:能实时获取指定监听好友(群)的新消息
- 其他定制功能:可根据需求定制自动化流程,满足各类特殊需求。
📦三、wxauto的安装与运用
1. wxauto的安装
安装wxauto十分简便,和安装其他第三方库一样,在命令行输入下述命令即可:
pip install wxauto
pip install wxauto -i https://pypi.tuna.tsinghua.edu.cn/simple
(换源安装 )
注意:当下wxauto仅支持 Windows 10|11|Server2016+
系统,苹果等电脑系统不支持,Python环境要求 Python:3.7+(不支持3.7.6和3.8.1)
,注意!!!Python版本不支持3.7.6和3.8.1
,微信版本默认分支为微信3.9.11.17版本 ,使用前请先检查自身电脑微信是否为该版本,版本不同可能因UI问题导致某些功能无法正常调用。
注意:若你的微信版本可用,也无需过度纠结此点。
2. wxauto的基础运用
注意:在运行代码前务必保证PC微信客户端已登陆。
【示例1】:基于wxauto发送消息
应用场景:可重复发送相同内容以达成消息轰炸效果
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) # 要 @ 的人
发送图片/视频/文件消息SendFiles
参数说明:
参数名 | 类型 | 默认值 | 说明 |
---|---|---|---|
filepath | str 或 list | / | 指定文件路径,单个文件用str,多个文件用list |
who | str | None | 要发送给谁,默认则发送给当前打开的页面 |
from wxauto import *
wx = WeChat()
content = 'hello world'
who = '文件传输助手'
file = [
r'D:\软件\图片\荒.png',
r'D:\C语言学习资料\高质量的C-C++编程.pdf'
]
wx.SendFiles(filepath=file, who=who)
聊天窗口消息获取
默认是当前聊天窗口
from wxauto import *
wx = WeChat()
# 获取当前聊天窗口消息
msgs = wx.GetAllMessage()
# 输出消息内容
for msg in msgs:
if msg.type == 'sys':
print(f'【系统消息】{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
方法使用,以实现获取更多历史消息。
注意:LoadMoreMessage
方法加载更多历史消息时,需保证当前聊天窗口有历史消息,否则无效果,即需触发一次“查看更多消息”。
from wxauto import WeChat
wx = WeChat()
# 加载更多历史消息
wx.LoadMoreMessage()
# 获取当前聊天窗口消息
msgs = wx.GetAllMessage()
# 消息处理逻辑代码。。。
微信添加好友AddNewFriend
方法用于发起好友申请。
注意:微信有一定限制,若频繁添加好友,可能会被限制添加好友权限,请谨慎使用,切勿滥用!!!
from wxauto import *
wx = WeChat()
keywords = 's15576806087' # 对方的微信号、手机号、QQ号
addmsg = '你好,我是xxxx' # 添加好友的消息
remark = '备注名字' # 备注名,没有则不用设置
tags = ['朋友', '同事'] # 标签列表
# 发起好友申请
wx.AddNewFriend(keywords, addmsg=addmsg, remark=remark, tags=tags)
获取好友信息
from wxauto import WeChat
wx = WeChat()
friend_infos = wx.GetAllFriends() # 获取好友信息
print(friend_infos)
3. wxauto的消息对象
这十分关键,结合大模型时会用到下述消息对象。
好友消息
支持属性 :
属性名 | 类型 | 说明 |
---|---|---|
type | str | 消息类型,固定为friend |
content |
str | 消息内容 |
sender | str | 发送者 |
sender_remark | str | 发送者备注名 |
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 | 消息内容 |
sender | str | 发送者 |
sender_remark | str | 发送者备注名 |
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结合大模型打造简易聊天机器人
此处选用百度的千帆大模型,首先登录进去后点击模型广场,随意选取一个免费的模型即可。
选好模型后,点击进去,点击那个开通付费(免费的,无需担忧费用,放心点击),提交订单就开通成功了。
返回至主页,点击应用接入,记住这里的API Key
和 Secret Key
,点击创建应用。
填写好应用名称和应用描述(随意填写就行),点击确定。
返回主页,点击模型广场,点击之前选取的模型,点击API文档。
向下滚动找到对应的请求示例的Python代码,复制那段代码。
复制好代码后,将自身对应的API Key
和 Secret Key
添加进去。
运行一下代码能够看到,result
便是大模型依据我们的问题给出的结果,当下只需将content替换成微信中好友发送过来的消息作为问题传递给大模型,然后将大模型给出的结果中的result
提取出来作为内容发送给好友,如此,一个简易的微信聊天机器人便搭建完成了。
🎧五、完整代码
import requests
import json
from wxauto import WeChat
def get_access_token():
"""
使用 API Key,Secret Key 获取access_token,替换下列示例中的应用API Key、应用Secret Key
"""
url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=[应用API Key]&client_secret=[应用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'] + "--此内容为AI生成", who="你要发送的人")
if __name__ == '__main__':
wx = WeChat()
while True:
msgs = wx.GetAllMessage()
if msgs:
if msgs[-1].type == "friend":
main(wx, msgs[-1].content)
文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/13626.html