就这个微信框架,目前免费的(支持微信3.9.10.16,后期会升级微信4.0),我为它写了咱奥特曼的适配器。适配器js附件在底部:
使用前需要先注册账号,注册地址http://starbot.chat/
下载适配器插件,编辑js文件找到以下内容
const imHttpApi = "http://127.0.0.1:10001/api/processor";//默认机器人框架api接口,根据实际情况修改ip
const token = "51e344e605dd42cbab1a73814eafd3c9";//请求token,从框架拷贝过来
然后放到/plugin/adapters下面,重启奥特曼
向机器人发送set im.starbot enable true然后重启奥特曼
设置管理员的指令:set im.starbot masters xxxx
然后把奥特曼的插件接收消息地址http://autMan地址:端口/starbot/receive
写到微信框架内部,保存即可完成对接。
适配器插件下载地址:https://wwaf.lanzouv.com/ixoXb2fj80id
/*
@Tips 本机器人目前免费使用,使用前请先注册微信框架账号,注册地址https://starbot.i1314i.com/register.html?invitationCodeStatus=hidden&invitationCode=RCSK0
@Name: startBot微信框架接入autMan的适配器,nodejs版本
@Author: 泡泡大佬
@Date: 2024-11-17
@FilePath: plugin/adapters/adapter_starbot_http.js
@Description: 下面主函数的url地址为http://autMan地址:端口/starbot/adapter 微信框架端回调地址是http://autMan地址:端口/starbot/receive
@Bugs: 适用于autMan版本>=3.0.2,
@Commands:启用命令:set im.starbot enable true,重启生效
*/
const { WebSocket } = require('ws');
const axios = require('axios');
// starbot的http接口地址
const imHttpApi = "http://localhost:10001/api/processor";//api接口
const token = "51e344e605dd42cbab1a73814eafd3c9";//请求token
async function receiveAutData(websocketAut) {
websocketAut.on('message', async (message) => {
try {
console.log(`Received from StarBot: ${message}`);
const messageJson = JSON.parse(message);
if ("data" in messageJson && "event" in messageJson) {
var msg_data = messageJson.data;
if (msg_data) {
if (msg_data['messageSource'] === 1) {
console.log("自己的消息不做处理");
return;//自己的消息
}
const commsg = {
bot_id: msg_data['robotId'],
user_name: "粉丝",
im_type: "starbot",
message_id: msg_data['messageId'],
content: msg_data["message"],
raw_message: msg_data["message"]
};
if (msg_data["fromType"] === "group") {
commsg["chat_id"] = msg_data['fromWxId'];
commsg["user_id"] = msg_data['finalFromWxId'];
}
else {
commsg["user_id"] = msg_data['fromWxId'];
}
var str = JSON.stringify(commsg);
//console.log("整理好的消息内容:", str);
await websocketAut.send(str);
} else {
console.log("data is null or undefined");
}
} else if ("aut_echo" in messageJson) {
if (messageJson["aut_action"] === "reply_message" || messageJson["aut_action"] === "push_message") {
//console.log("凹凸曼的回调消息:", messageJson);
const autEcho = messageJson["aut_echo"];
var send_content = messageJson['aut_params']['content'];
//===========先处理内容,把图片和内容分开
// 正则表达式匹配图片链接
let final_text = send_content;
let images = [];
try {
const regex = /\[CQ:image,file=.*?\]/g;
const matches = send_content.match(regex);
for (let index = 0; index < matches.length; index++) {
const image = matches[index];
final_text = final_text?.replace(image, '');
var tmp = image?.replace("[CQ:image,file=", "").replace("]", "");
if (tmp) {
images.push(tmp);
}
}
} catch (error) {
console.log("提取图片出错:", error);
}
final_text = final_text?.replace(/\s*\n\s*\n/g, '\n');//去掉连续的空行
//===============发送文本=======
if (final_text?.length > 0) {
const replyMessage = {
type: "sendTextMessage",
params:
{
robotId: messageJson["aut_params"]["bot_id"],
wxId: messageJson["aut_params"]["user_id"],
message: final_text
}
};
if ("chat_id" in messageJson["aut_params"] && messageJson["aut_params"]["chat_id"] !== "") {
replyMessage.params["wxId"] = messageJson["aut_params"]["chat_id"];
}
const messageId = await postData(replyMessage);
if (messageId !== null) {
const replyMessage = {
aut_echo: autEcho,
aut_params: `${messageId}`,
};
await websocketAut.send(JSON.stringify(replyMessage));
}
}
//===============发送图片=======
if(images?.length>0)
{
for (let index = 0; index < images?.length; index++) {
const image = images[index];
const replyMessage = {
type: "sendImageMessage",
params:
{
robotId: messageJson["aut_params"]["bot_id"],
wxId: messageJson["aut_params"]["user_id"],
path: image
}
};
if ("chat_id" in messageJson["aut_params"] && messageJson["aut_params"]["chat_id"] !== "") {
replyMessage.params["wxId"] = messageJson["aut_params"]["chat_id"];
}
const messageId = await postData(replyMessage);
if (messageId !== null) {
const replyMessage = {
aut_echo: autEcho,
aut_params: `${messageId}`,
};
await websocketAut.send(JSON.stringify(replyMessage));
}
}
}
} else if (messageJson["aut_action"] === "delete_message" && "aut_params" in messageJson && "message_id" in messageJson["aut_params"]) {
const replyMessage = {
type: "delete_msg",
params: {
message_id: messageJson["aut_params"]["message_id"]
},
};
await postData(replyMessage);
}
}
} catch (e) {
console.log(`JSON解析错误: ${message}`);
}
});
}
async function postData(data) {
try {
const config = {
headers: {
'Authorization': token,
'Content-Type': 'application/json'
}
};
const response = await axios.post(`${imHttpApi}`, data, config);
if (response?.data?.code === 200) {
const messageId = response.data?.data?.instanceId;
//console.log(`Messageid: ${messageId}`);
return null;//这个微信暂时没有返回消息id,直接给null
} else {
console.log(`POST ${imHttpApi} failed with code ${response?.data?.code}${response?.data?.description}`);
return null;
}
} catch (error) {
console.log(`Error: Unexpected response format for ${imHttpApi}${error}`);
return null;
}
}
async function main() {
const uriAut = "ws://127.0.0.1:8080/starbot/adapter";
const websocketAut = new WebSocket(uriAut);
await receiveAutData(websocketAut);
}
main();