微信群
群二维码失效时添加好友拉群
前置条件
- 你需要有openai的账号
- 创建账号时很多国家的手机号是不允许注册的,可以再一些网站购买允许注册的手机号,可以再这个网站,购买(需要充值 2 美元)
- 有了账号之后你需要在 openAI 创建 token(这里 token 基本上过期时间只有一天,所以每天都需要创建新的 token 后粘贴到代码上重启项目)
- 还需要有一台登录了微信的手机(并且这个微信是绑定过手机号,用过一段时间的,否则代码登录可能发不出消息,在代码登录之后手机的微信不能退出,就像 PC 登录了微信,手机退出时电脑也会自动退出)
安装依赖
npm i wechaty
npm i axios登录微信
输出的链接浏览器打开,微信扫码登录即可。
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() || "")} 登录成功`)
)" aria-label="复制" data-bs-original-title="复制"></button>
</div>
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(
二维码<span class="hljs-subst">${status}</span>: https://wechaty.js.org/qrcode/<span class="hljs-subst">${<span class="hljs-built_in">encodeURIComponent</span>( qrcode )}</span>
)
)
.on("login", user =>
console.log(用户名 <span class="hljs-subst">${(username = user.name() || <span class="hljs-string">""</span>)}</span> 登录成功)
)
群聊自动@回复
以下实现群@当前登录的用户是,@回复对方
<button type="button" class="btn btn-dark far fa-copy rounded-0 sflex-center copyCode" data-toggle="tooltip" data-placement="top" data-clipboard-text=".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();" aria-label="复制" data-bs-original-title="复制"></button>
</div>
</div><pre class="ts hljs language-typescript">.<span class="hljs-title function_">on</span>(<span class="hljs-string">"message"</span>, <span class="hljs-keyword">async</span> (message) => {
<span class="hljs-comment">// 如果是群聊消息</span>
<span class="hljs-keyword">if</span> (message.<span class="hljs-title function_">room</span>()) {
<span class="hljs-comment">// const type = message.type();</span>
<span class="hljs-keyword">const</span> msg = message.<span class="hljs-title function_">text</span>().<span class="hljs-title function_">replace</span>(<span class="hljs-string">`@<span class="hljs-subst">${username}</span>`</span>, <span class="hljs-string">""</span>);
<span class="hljs-keyword">const</span> room = <span class="hljs-keyword">await</span> message.<span class="hljs-title function_">room</span>();
<span class="hljs-keyword">const</span> isMentioned = <span class="hljs-keyword">await</span> message.<span class="hljs-title function_">mentionSelf</span>();
<span class="hljs-keyword">const</span> contact = message.<span class="hljs-title function_">talker</span>();
<span class="hljs-keyword">const</span> topic = <span class="hljs-keyword">await</span> room.<span class="hljs-title function_">topic</span>();
<span class="hljs-comment">// const includes = [""]</span>
<span class="hljs-keyword">if</span> (isMentioned) {
<span class="hljs-keyword">try</span> {
<span class="hljs-keyword">const</span> res = <span class="hljs-keyword">await</span> <span class="hljs-title function_">axios</span>({
<span class="hljs-attr">method</span>: <span class="hljs-string">"post"</span>,
<span class="hljs-attr">url</span>: <span class="hljs-string">"https://api.openai.com/v1/chat/completions"</span>,
<span class="hljs-attr">headers</span>: {
<span class="hljs-title class_">Authorization</span>: <span class="hljs-string">"Bearer "</span> + apiKey,
<span class="hljs-string">"Content-Type"</span>: <span class="hljs-string">"application/json"</span>,
},
<span class="hljs-attr">data</span>: <span class="hljs-title class_">JSON</span>.<span class="hljs-title function_">stringify</span>({
model,
<span class="hljs-attr">messages</span>: [{ <span class="hljs-attr">role</span>: <span class="hljs-string">"user"</span>, <span class="hljs-attr">content</span>: msg }],
}),
<span class="hljs-attr">timeout</span>: <span class="hljs-number">0</span>,
});
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(
<span class="hljs-string">`群号: <span class="hljs-subst">${topic}</span>, 用户名: <span class="hljs-subst">${contact.name()}</span>, 消息: <span class="hljs-subst">${msg}</span>,回答: <span class="hljs-subst">${
res.data.choices[<span class="hljs-number">0</span>].message.content
}</span>`</span>
);
room.<span class="hljs-title function_">say</span>(<span class="hljs-string">`@<span class="hljs-subst">${contact.name()}</span> <span class="hljs-subst">${res.data.choices[<span class="hljs-number">0</span>].message.content}</span>`</span>);
} <span class="hljs-keyword">catch</span> (e) {
<span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">"报错: "</span>, e.<span class="hljs-property">message</span>);
room.<span class="hljs-title function_">say</span>(<span class="hljs-string">`@<span class="hljs-subst">${contact.name()}</span> 报错了...`</span>);
}
}
}
});
<span class="hljs-comment">// 事件监听完成后开始启动</span>
wechaty.<span class="hljs-title function_">start</span>();</pre><p><script setup lang="ts"><br>import CommunicationGroup from "../../../layout/CommunicationGroup.vue"<br></script></p>