前言
随着DeepSeek大模型的火热,最近各地的政务、企业业务等都在纷纷接入DeepSeek大模型,大家希望利用大模型的能力赋能自身业务的进一步提质增效,本地化部署有着高昂的成本,所以很多中小企业还是采用了api接入的方式实现。在此火热状态下,DeepSeek可以使智能客服聊天机器人能力更上一层台阶,让那些死板的自动回复话术更加灵活,本文主要记录DeepSeek在wx自动回复的过程,代码具有局限性,仅供参考学习,不可直接使用,慎重有wx强制登出的情况(为了你的账号安全,请重新登陆。),尝试的后果自负。
思路
PC端运行过程如下:启动程序 → 绑定微信窗口 → 监控消息列表 → 发现未读消息 → 点击消息 → 识别联系人 → 过滤群聊 → 生成回复 → 发送消息
其中api接口采用的是openai:调用OpenRouter(兼容OpenAI API)的官方库,uiautomation:用于控制Windows桌面应用的UI自动化库。
效果
实现
一、模块导入与初始化
import numpy as np
from uiautomation import WindowControl
import requests
import json
import time
from openai import OpenAI
其中requests用于调api,json解析数据,time控制操作间隔防止频繁请求。
二、微信窗口绑定
wx = WindowControl(Name='微信', searchDepth=1)
wx.SwitchToThisWindow()
hw = wx.ListControl(Name='会话')
定位微信主窗口并绑定消息列表控件。
三、过滤机制
是否AI自动聊天的检测逻辑
def is_ai_chat(contact_name):
# 跳过的关键字
skip_keywords = ['总','雷军', '沟通', '群']
for keyword in skip_keywords:
if keyword in contact_name:
return False
return True
通过名称关键词识别,可通过修改 skip_keywords 列表自定义跳过AI自动聊天。
四、处理主循环
1. 消息检测
while True:
we = hw.TextControl(searchDepth=4)
while not we.Exists(): pass
持续扫描会话列表中的未读消息控件(TextControl)。
2. 消息点击与识别
we.Click(simulateMove=False)
contact_name = wx.ListControl(Name='消息').GetChildren()[-1].Name
点击未读消息激活聊天窗口,通过遍历消息列表最后一个子控件获取联系人/群聊名称。
五、AI回复请求
1. API配置
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="sk-or-v1-xxxxxxxx"
)
关键参数:
base_url:指向OpenRouter的API端点
api_key:用户认证密钥(需替换为实际值)
2. 模型调用
completion = client.chat.completions.create(
model="deepseek/deepseek-r1:free",
messages=[{"role": "user", "content": last_msg}]
)
reply_content = completion.choices[0].message.content
使用 deepseek-r1:free 模型(免费版),单轮对话模式(仅传递用户消息),返回结果通过结构化数据访问。
六、消息发送机制
1. 内容格式化
formatted_reply = result.replace('\n', '{Shift}{Enter}')
将换行符转换为微信支持的换行快捷键。
2. 模拟操作
wx.SendKeys(formatted_reply, waitTime=1)
time.sleep(2)
wx.SendKeys('{Enter}', waitTime=1)
输入回复内容,等待2秒,发送回车键提交消息。
源码
完整源码如下
import numpy as np
from uiautomation import WindowControl
import requests
import json
import time
from openai import OpenAI # 新增OpenAI库导入
# 绑定微信主窗口
wx = WindowControl(
Name='微信',
searchDepth=1
)
# 切换窗口
wx.SwitchToThisWindow()
# 初始化OpenAI客户端(新增配置)
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="", # 替换为你的OpenRouter API密钥
)
# 寻找会话控件绑定
hw = wx.ListControl(Name='会话')
# 是否AI自动聊天的检测逻辑
def is_ai_chat(contact_name):
# 跳过的关键字
skip_keywords = ['总','雷军', '沟通', '群']
for keyword in skip_keywords:
if keyword in contact_name:
return False
return True
while True:
we = hw.TextControl(searchDepth=4)
while not we.Exists():
pass
if we.Name:
we.Click(simulateMove=False)
contact_name = wx.ListControl(Name='消息').GetChildren()[-1].Name
#print(f"当前会话人: {contact_name}")
if not is_ai_chat(contact_name):
print("检测到关键词,跳过自动回复")
continue
last_msg = wx.ListControl(Name='消息').GetChildren()[-1].Name
print(f"收到消息: {last_msg}")
try:
# 调用DeepSeek API获取回复(修改核心部分)
completion = client.chat.completions.create(
model="deepseek/deepseek-r1:free",
messages=[
{
"role": "user",
"content": last_msg
}
],
extra_headers={
"HTTP-Referer": "http://localhost:8080", # 可选配置
"X-Title": "WeChat Bot" # 可选配置
}
)
reply_content = completion.choices[0].message.content
result = f'AI自动回复:{reply_content}'
except Exception as e:
result = "自动回复生成失败,请稍后再试"
print(f"API调用错误: {str(e)}")
print(f"回复内容: {result}")
time.sleep(3)
# 处理换行符(根据模型返回格式调整)
formatted_reply = result.replace('\n', '{Shift}{Enter}')
wx.SendKeys(formatted_reply, waitTime=1)
time.sleep(2)
wx.SendKeys('{Enter}', waitTime=1)
wx.TextControl(SubName=result[:5]).RightClick()
time.sleep(5)