文章目录 一、wxauto简介 二、wxauto的紧张功能 三、wxauto的安装与利用 1. wxauto的安装 2. wxauto的大略利用 3. wxauto的工具 四、wxauto结合大模型实现大略的谈天机器人 三、完全代码
一、wxauto简介
wxauto 是我在2020年开拓的一个基于 UIAutomation 的开源 Python 微信自动化库。Python 初学者也可以大略上手自动化微信操作。目前已实现很多日常的微信操作的自动化,如自动发送、自动添加好友、自动回答、自动获取谈天记录、图片、文件等功能,后续还会根据反馈更新更多功能。
wxauto的github链接 : https://github.com/cluic/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 = 39;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 参数解释:
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 WeChatwx = 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 WeChatwx = WeChat()friend_infos = wx.GetAllFriends() # 获取好友信息print(friend_infos)
3. wxauto的工具
这个很主要,下面结合大模型时会用到以下的工具。 好友 支持属性 :
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}')
自己的 支持属性 :
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 requestsimport jsonfrom wxauto import WeChatdef 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_credentialsclient_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)