1 Star 3 Fork 0

JC/wechat-bot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sendMessage.js 2.71 KB
一键复制 编辑 原始数据 按行查看 历史
JC 提交于 2024-05-27 15:31 . initial commit
import {
botName,
roomWhiteList,
aliasWhiteList
} from '../../config.js';
import {
getServe
} from './serve.js';
/**
* 默认消息发送
* @param msg
* @param bot
* @param ServiceType 服务类型 'GPT' | 'Kimi'
* @returns {Promise<void>}
*/
export async function defaultMessage(msg, bot, ServiceType = 'GPT') {
const getReply = getServe(ServiceType)
const contact = msg.talker() // 发消息人
const receiver = msg.to() // 消息接收人
const content = msg.text() // 消息内容
const room = msg.room() // 是否是群消息
const roomName = (await room?.topic()) || null // 群名称
const alias = (await contact.alias()) || (await contact.name()) // 发消息人昵称
const remarkName = await contact.alias() // 备注名称
const name = await contact.name() // 微信名称
const isText = msg.type() === bot.Message.Type.Text; // 消息类型是否为文本
const isImage = msg.type() == bot.Message.Type.Image;
const isRoom = roomWhiteList.includes(roomName) && content.includes(`${botName}`) // 是否在群聊白名单内并且艾特了机器人
const isAlias = aliasWhiteList === '*' ? true : (aliasWhiteList.includes(remarkName) || aliasWhiteList.includes(name)) // 发消息的人是否在联系人白名单内
const isBotSelf = botName === remarkName || botName === name // 是否是机器人自己
// TODO 你们可以根据自己的需求修改这里的逻辑
// if (isBotSelf || !isText) return // 如果是机器人自己发送的消息或者消息类型不是文本则不处理
if (isBotSelf) return // 如果是机器人自己发送的消息或者消息类型不是文本则不处理
const options = {
msg,
bot,
type: ({
[bot.Message.Type.Text]: 'text',
// 豆包大模型接图片
...(ServiceType === 'Doubao' ? {
[bot.Message.Type.Image]: 'image',
[bot.Message.Type.Video]: 'video',
[bot.Message.Type.Audio]: 'audio'
} : {})
})[msg.type()],
};
if (!options.type) {
return;
}
try {
// 区分群聊和私聊
if (isRoom && room) {
const question = await msg.mentionText() || content.replace(`${botName}`, ''); // 去掉艾特的消息主体
console.log('🌸🌸🌸 / question: ', question);
const response = await getReply(question, options);
console.log('🚀🚀🚀 / reply', response);
await room.say(response);
}
// 私人聊天,白名单内的直接发送
if (isAlias && !room) {
console.log('🌸🌸🌸 / content: ', content);
const response = await getReply(content, options);
console.log('🚀🚀🚀 / reply', response);
await contact.say(response);
}
} catch (e) {
console.error('defaultMessage in error: ', e);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/jin-ec/wechat-bot.git
git@gitee.com:jin-ec/wechat-bot.git
jin-ec
wechat-bot
wechat-bot
master

搜索帮助

Cb406eda 1850385 E526c682 1850385