随笔 - 27,  文章 - 0,  评论 - 1,  阅读 - 18063

微信群

image.png

群二维码失效时添加好友拉群

image.png

文章博客地址,查看源码

前置条件

  • 你需要有openai的账号
  • 创建账号时很多国家的手机号是不允许注册的,可以再一些网站购买允许注册的手机号,可以再这个网站,购买(需要充值 2 美元)
  • 有了账号之后你需要在 openAI 创建 token(这里 token 基本上过期时间只有一天,所以每天都需要创建新的 token 后粘贴到代码上重启项目)
  • 还需要有一台登录了微信的手机(并且这个微信是绑定过手机号,用过一段时间的,否则代码登录可能发不出消息,在代码登录之后手机的微信不能退出,就像 PC 登录了微信,手机退出时电脑也会自动退出)

安装依赖

npm i wechaty
npm i axios

登录微信

输出的链接浏览器打开,微信扫码登录即可。

import { WechatyBuilder } from "wechaty"

const apiKey = "你的openAI key"

// 调用 ChatGPT4 模型
const model = "gpt-3.5-turbo"

let username = ""
const wechaty = WechatyBuilder.build()
wechaty
  .on("scan", (qrcode, status) =>
    console.log(
      `二维码${status}: https://wechaty.js.org/qrcode/${encodeURIComponent(
        qrcode
      )}`
    )
  )
  .on("login", user =>
    console.log(`用户名 ${(username = user.name() || "")} 登录成功`)
  )

群聊自动@回复

以下实现群@当前登录的用户是,@回复对方

.on("message", async (message) => {
    // 如果是群聊消息
    if (message.room()) {
      // const type = message.type();
      const msg = message.text().replace(`@${username}`, "");
      const room = await message.room();
      const isMentioned = await message.mentionSelf();
      const contact = message.talker();
      const topic = await room.topic();
      // const includes = [""]

      if (isMentioned) {
        try {
          const res = await axios({
            method: "post",
            url: "https://api.openai.com/v1/chat/completions",
            headers: {
              Authorization: "Bearer " + apiKey,
              "Content-Type": "application/json",
            },
            data: JSON.stringify({
              model,
              messages: [{ role: "user", content: msg }],
            }),
            timeout: 0,
          });
          console.log(
            `群号: ${topic}, 用户名: ${contact.name()}, 消息: ${msg},回答: ${
              res.data.choices[0].message.content
            }`
          );
          room.say(`@${contact.name()} ${res.data.choices[0].message.content}`);
        } catch (e) {
          console.log("报错: ", e.message);
          room.say(`@${contact.name()} 报错了...`);
        }
      }
    }
  });

// 事件监听完成后开始启动
wechaty.start();
posted on   tomiaa  阅读(212)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
历史上的今天:
2022-06-21 npm publish 403 发布失败可能的原因
2022-06-21 JavaScript rem.js(rem 自适应屏幕大小)
2022-06-21 前端 json 转换为 excel 文件保存 xlsx
2022-06-21 javascript 防抖与节流
2022-06-21 axios 防抖取消多次请求及封装

< 2025年3月 >
23 24 25 26 27 28 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 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示