调用大模型实现微信自动回复新年祝福类信息
一、实现功能
- 通过uiautomation实现自动读取微信(电脑版)未读消息
- 根据关键字判断是否是新春祝福类信息,如果是,调用智谱AI模型,根据接收到的消息,利用模型生成回复内容
- 自动将回复内容发送出去
二、实现效果


三、实现代码
from uiautomation import WindowControl,MenuControl
from zhipuai import ZhipuAI
# 绑定微信主窗口
weixin = WindowControl(Name='微信',)
print('微信主窗口信息:---',weixin,'---')
# 切换窗口
weixin.SwitchToThisWindow()
# 寻找聊天框绑定
wx_chat = weixin.ListControl(Name='会话')
print("聊天框窗口信息:--- ",wx_chat,' ---')
# 调用智谱AI大模型根据接收信息获取自动回复的内容
def get_response(last_msg):
client = ZhipuAI(api_key="***************************")
response = client.chat.completions.create(
model="chatglm_std", # 填写需要调用的模型名称,根据效果选择不同的模型
messages=[
{"role": "user", "content": last_msg}
],
)
return response.choices[0].message.content
# 接收并回复消息
while True:
unreadmsg = wx_chat.TextControl(searchDepth=4)
# 死循环,没有超时报错
while not unreadmsg.Exists(0):
pass
print('查询到的未读消息:--- ',unreadmsg,' ----')
if unreadmsg.Name:
# 点击未读消息
unreadmsg.Click(simulateMove=False)
# 读取最后一条
last_msg = weixin.ListControl(Name='消息').GetChildren()[-1].Name
print('读取到的最后一条消息:--- ',last_msg,' ---')
keyword = ['拜年','龙年','新年','新春','新的一年','佳节','阖家幸福','祝您','健康','快乐','如意']
for i in keyword:
if i in last_msg:
# weixin.SendKeys(ar[0].replace('{br}', '{Shift}{Enter}'), waitTime=0)
response_msg = get_response(last_msg)
print('自动回复的消息 --- ',response_msg)
response_msg = response_msg + '【自动回复测试】'
weixin.SendKeys( response_msg, waitTime=0)
weixin.SendKeys('{Enter}', waitTime=3)
break
#如果一直循环,这里不要break
break