抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

通过 wechatFerry 实现微信机器人,并接入 AI 实现智能客服。

一、事前准备

1.1 特殊微信客户端安装

下载安装 https://github.com/lich0821/WeChatFerry/releases 最新版客户端,例如 WeChatSetup-3.9.12.17.exe

1.2 wechatFerry 注入

下载安装 https://github.com/dr-forget/wcferry-node/releases 最新版工具,例如 Wcf-Tool-Windows-1.0.8-Setup.exe

1.3 启动 wcf

运行 wcf 工具,一般会自动下载 dll 压缩包并解压

若下载失败,也可手动到 https://github.com/lich0821/WeChatFerry/releases 下载并放在例如 C:\文档\WCFApp\v39.4.5.zip

二、消息监听

2.1 初始化项目

1
pnpm init

2.2 安装依赖

1
pnpm i @zippybee/wechatcore

2.3 消息监听

根目录下创建 index.js 文件并编写

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import { Wcferry } from "@zippybee/wechatcore";

const client = new Wcferry({
host: "192.168.2.137", // 默认127.0.0.1,若运行微信的电脑非本机,请改为其ip
});
client.start();

const isLogin = client.isLogin();
const userinfo = client.getUserInfo();

console.log(isLogin, userinfo);

client.listening((msg) => {
console.log("收到消息:", msg.sender, msg.isGroup, msg.roomId, msg.content);
});

2.4 运行

运行后即可看到日志会打印出微信登录账号信息,以及实时收到的消息

1
node index.js

三、实现AI智能客服

3.1 安装 AI 依赖

为了便捷此处使用 @seepine/ai 这个 node 库,也可改为任意 ai 库,例如原生的 openai

1
pnpm i @seepine/ai

3.2 AI 对话并答复

此处只简单调用了大模型的对话返回,实际场景还可以给大模型接入知识库或MCP,打造答复更精准的智能客服

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
const off = client.listening((msg) => {
console.log("收到消息:", msg.sender, msg.isGroup, msg.roomId, msg.content);

// 此处判断当是群消息,且消息内容@ai开头才响应,否则消息一多就炸了
if (msg.isGroup && msg.content.startsWith("@ai ") && msg.content.length > 4) {

const question = msg.content.replace("@ai ", "");
console.log("开始执行", question);

const agent = new Agent({
baseURL: process.env["OPENAI_BASE_URL"],
apiKey: process.env["OPENAI_API_KEY"],
model: process.env["OPENAI_MODEL"],
});

agent
.chatSync(question)
.then((res) => {
// 拿到ai返回结果后,发送到群里
console.log("执行结果:", res);
client.sendTxt(msg.content + "\n\n" + res, msg.roomId);
})
.catch((e) => {
client.sendTxt(e.message, msg.roomId);
});
}
});

评论