# wechat-intelligent-robot **Repository Path**: deng-2022/wechat-intelligent-robot ## Basic Information - **Project Name**: wechat-intelligent-robot - **Description**: 微信智能机器人 - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2024-01-07 - **Last Updated**: 2025-06-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 微信智能 AI 机器人 > 2024 年 4 月 22 日 今年年初试了下这个**微信智能 AI 机器人**开发,并记录了最终效果,只是相关文档并未完善。今天想起来了,抓紧时间完善下。 **相关阅读:** [Wechaty + 讯飞星火 GPT 打造微信机器人\_讯飞 gpt-CSDN 博客](https://blog.csdn.net/ziyunLLL/article/details/134908113) 这里建议关注**微信公众号**:**子云之风**,我就是看了他去年十二月份发表的一篇文章,才尝试了微信智能机器人的开发和使用。 > 2025 年 1 月 10 日 今天刚好在测试讯飞星火应用生成热点文章的稳定性,就顺便回顾下前两年玩过的微信智能 AI 机器人,推送下最新代码。 [wechat-intelligent-robot: 微信智能机器人 (gitee.com)](https://gitee.com/deng-2022/wechat-intelligent-robot) 这个仓库本来就有的,更新下项目文档即可。 年后考虑持学习下微信公众号内置的智能 AI 机器人,要不然没有免费的 token 这智能机器人也回复不了,有免费的当然不用花钱的。 ### Token 获取 > 2024 年 4 月 22 日 **源码获取**:[GitHub - wechaty/puppet-padlocal: Puppet PadLocal is a Pad Protocol for WeChat](https://github.com/wechaty/puppet-padlocal) 基本上所有的问题都能在这个项目的 README.md 文档中找到答案,这里就简单介绍下怎么获取 Token 来运行该项目: ![image-20240422162442048](http://blog.memory-life.icu/image-20240422162442048.png) ![image-20240422162031416](http://blog.memory-life.icu/image-20240422162031416.png) 可以看到 👆 我已经使用过一个 Token 了,就是年初尝试做这个**微信智能机器人**时申请的,已经过期了。不过可以考虑续费: ![image-20240422162624595](http://blog.memory-life.icu/image-20240422162624595.png) 妈的一个月两百块钱,你怎么不去抢。怎么永久获得这个 Token 呢,这我还得想想其他办法。之前看到鱼皮在牛客网上发的教程,使用的是**微信智能对话平台**提供的机器人,而不是**讯飞星火语言大模型**。那个方法好像是免费的,有时间再去看看。 那我就多用几个手机号拿一些 Token 玩玩了。 ### 信息配置 > 2024 年 4 月 22 日 首先当然是配置拿到的 Token 了,要不连项目都起不来,如果你已经获取到合法 Token 的话: 在 `main.ts` 下,修改如下配置: ```tsx const puppet = new PuppetPadlocal({ token: "puppet_padlocal_cee4dff75e8d451d82137dab33bf4641", }); ``` 以上就是一键配置 Token 了,如果没有正确配置,启动项目肯定会失败,报错如下: ![image-20240422163302912](http://blog.memory-life.icu/image-20240422163302912.png) 在`spark.ts`下,修改如下配置: ```tsx // 封装鉴权方法 function authenticate() { return new Promise((resolve, reject) => { var apiKey = "2523e8f0d30b56fe0ce00f7d2b8e3099"; var apiSecret = "NDk3Njk2OGIwYTFlZDZiNWVlMWM1ZjVh"; var url = "wss://spark-api.xf-yun.com/v1.1/chat"; var host = "spark-api.xf-yun.com"; var date = new Date().toUTCString(); var algorithm = "hmac-sha256"; var headers = "host date request-line"; var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v1.1/chat HTTP/1.1`; var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, apiSecret); var signature = CryptoJS.enc.Base64.stringify(signatureSha); var authorizationOrigin = `api_key="${apiKey}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`; var authorization = btoa(authorizationOrigin); url = `${url}?authorization=${authorization}&date=${date}&host=${host}`; resolve(url); }); } ``` 只需要修改其中的 apiKey 和 apiSecret 这两项参数,这就是通过接口认证连接对应讯飞星火模型。这两项参数内容需要在**讯飞开放平台获取([控制台-讯飞开放平台 (xfyun.cn)](https://console.xfyun.cn/services/bm2))**: ![image-20240422163052501](http://blog.memory-life.icu/image-20240422163052501.png) 至此,必要的开发前信息配置就已经完成了,运行以下命令即可启动项目: ```bash npm run demo ``` ### 开发过程 > 2024 年 1 月 7 日 简单展示一下一早上的成果,可算跑通 demo 代码了:**(2024/01/07 早)** ![image-20240107085819793](http://blog.memory-life.icu/image-20240107085819793.png) 再次调试成功,能够辨认群聊消息并回复固定文字,且屏蔽 AI 自己的消息: ```tsx // 群聊消息 if (message.room()) { // 不处理自己的消息 if (message.self()) { return; } // 如果 content 以 "@初心" 开头,那么这里的代码将被执行 if (content.startsWith("@初心")) { const reply = `你好,${sender}!这是来自群聊${roomName}的消息:${content}`; // 回复消息 await message.say(reply); } // 如果 content 不以 "@初心" 开头,那么这里的代码将被执行 } else { // ... } } ``` AI 智能有两个很关键的函数:**(2024/01/09 晚)** 判断消息类型(附件、音频、视频、表情、图片、URL 或小程序) ```ts export async function getMessagePayload(message: Message) { switch (message.type()) { case PUPPET.types.Message.Text: log.silly(LOGPRE, `get message text: ${message.text()}`); break; case PUPPET.types.Message.Attachment: case PUPPET.types.Message.Audio: { const attachFile = await message.toFileBox(); const dataBuffer = await attachFile.toBuffer(); log.info(LOGPRE, `get message audio or attach: ${dataBuffer.length}`); break; } case PUPPET.types.Message.Video: { const videoFile = await message.toFileBox(); const videoData = await videoFile.toBuffer(); log.info(LOGPRE, `get message video: ${videoData.length}`); break; } ................................. } } ``` 执行消息发送 ```ts export async function dingDongBot(message: Message) { if (message.to()?.self() && message.type() == PUPPET.types.Message.Text) { await sendMsg(message.text(), (data: string) => { message.talker().say(data); }); } } ``` ### 基本完善 ![image-20240109204229935](http://blog.memory-life.icu/image-20240109204229935.png) 单线对话: ``` export async function dingDongBot(message: Message) { if (message.to()?.self() && message.type() == PUPPET.types.Message.Text) { await sendMsg(message.text(), (data: string) => { message.talker().say(data); }); } } ``` 在上面这段代码中,`message.to()?.self()` 这行代码的作用是`判断是否为用户单线发起聊天`;`message.talker().say(data)` 这行代码的作用是`返回响应消息给该单线聊天用户 ` 实现群聊助手功能 ```tsx // 群聊消息 if (message.room()) { // 不处理自己的消息 if (message.self()) { return; } const mesTime = new Date(); if (mesTime >= startTime) { // 如果 content 以 "@初心" 开头,那么这里的代码将被执行 if (content.startsWith("@初心")) { // const reply = `你好,${sender}!这是来自群聊${roomName}的消息:${content}`; // await message.say(reply); await getMessagePayload(message); if (message.type() == PUPPET.types.Message.Text) { await sendMsg(message.text(), (data: string) => { message.say(data); }); } // 如果 content 不以 "@初心" 开头,那么这里的代码将被执行 } else { // ... } } } ``` 以上就是实现群聊 AI 助手的主要逻辑代码了**(2024/01/09 晚)** 再附上几张实验成果图留念一下: ![image-20240109204128728](http://blog.memory-life.icu/image-20240109204128728.png) ![image-20240109204353998](http://blog.memory-life.icu/image-20240109204353998.png) ![image-20240109204427101](http://blog.memory-life.icu/image-20240109204427101.png) 时隔半年,终于再次完善了微信智能 AI 机器人,使用了讯飞星火 AI 大模型。目前还不太完善,还需要对该 AI 的功能做进一步调试。**(2024/01/09 晚)** 之后继续尝试优化,这个服务的健壮性还有待提高,只要问题稍有违规就会停止掉整个服务。解决了这个问题,就可以尝试部署在云服务器上了。 保留原文档启动步骤在这里吧: # Quick Start with Demo ### 0. Apply Token You need PadLocal token to run this demo. [How to Apply Token](https://github.com/padlocal/wechaty-puppet-padlocal/wiki/How-to-Apply-Token) ### 1. Prepare Node Environment [Install Node](https://nodejs.org/), 16 LTS version is recommended. ``` $ node --version // >= v16.0.0 ``` ### 2. Clone the [wechaty-puppet-padlocal-demo](https://github.com/padlocal/wechaty-puppet-padlocal-demo) project. ``` $ git clone git@github.com:padlocal/wechaty-puppet-padlocal-demo.git ``` Then install Node dependencies. ``` $ cd wechaty-puppet-padlocal-demo $ npm install ``` ### 3. Set you PadLocal Token in [`main.ts`](https://github.com/padlocal/wechaty-puppet-padlocal-demo/blob/master/main.ts) https://github.com/padlocal/wechaty-puppet-padlocal-demo/blob/42d2ab8dcfeff2b2be1f6b8b2e09d0f5fc0fdc11/main.ts#L10-L12 _[Optional]_ Uncomment following code to turn on debug log https://github.com/padlocal/wechaty-puppet-padlocal-demo/blob/42d2ab8dcfeff2b2be1f6b8b2e09d0f5fc0fdc11/main.ts#L5-L8 ### 4. Run the demo ``` $ npm run demo ``` ![carbon](https://user-images.githubusercontent.com/64943823/117439626-a6cde080-af65-11eb-85a5-815aa422b5c5.png)