Skip to content

Commit

Permalink
Merge pull request #378 from Giftia/dev
Browse files Browse the repository at this point in the history
v3.7.0
  • Loading branch information
Giftia authored Nov 16, 2022
2 parents 8656aa4 + 3b7157a commit fed908c
Show file tree
Hide file tree
Showing 6 changed files with 503 additions and 27 deletions.
6 changes: 6 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ System:
#接入QQ频道开关:
CONNECT_QQ_GUILD_SWITCH: false

#接入Telegram开关:
CONNECT_TELEGRAM_SWITCH: false

#web端使用的本机端口,如果有冲突的话请尝试更改:
WEB_PORT: 80

Expand All @@ -40,6 +43,9 @@ ApiKey:
#QQ频道机器人令牌,申请地址 https://q.qq.com/,获取地址 https://q.qq.com/bot/#/developer/developer-setting
QQ_GUILD_TOKEN: ""

#Telegram Bot Token,申请地址 https://t.me/BotFather
TELEGRAM_BOT_TOKEN: ""

#qqBot配置
qqBot:
#qqBot小夜的管理员列表,管理员有权执行管理员指令,且第一位管理员将会处理用户邀请小夜加群的请求:
Expand Down
83 changes: 70 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ const voicePlayer = require("play-sound")({
}); // mp3静默播放工具,用于直播时播放语音
const ipTranslator = require("lib-qqwry")(true); // lib-qqwry是一个高效纯真IP库(qqwry.dat)引擎,传参 true 是将IP库文件读入内存中以提升效率
const { createOpenAPI, createWebsocket } = require("qq-guild-bot"); // QQ频道SDK
const semverDiff = require("semver-diff");
const semverDiff = require("semver-diff"); // 版本比较
const TelegramBot = require("node-telegram-bot-api"); // Telegram机器人SDK

/**
* 中文分词器
Expand Down Expand Up @@ -148,7 +149,9 @@ var onlineUsers = 0, // 预定义
c1c_count = 0,
CONNECT_QQ_GUILD_SWITCH,
QQ_GUILD_APP_ID,
QQ_GUILD_TOKEN;
QQ_GUILD_TOKEN,
CONNECT_TELEGRAM_SWITCH,
TELEGRAM_BOT_TOKEN;

/**
* 声明结束,开始初始化
Expand All @@ -170,11 +173,11 @@ logger.info("插件加载完毕√\n".log);
InitConfig();

/**
* 下面是三大核心功能: web端、qq端、直播间端
* 下面是各种功能实现
*/

/**
* web端,前端使用layim框架
* web端消息处理,前端使用layim框架
*/
io.on("connection", async (socket) => {
socket.emit("getCookie");
Expand Down Expand Up @@ -304,7 +307,7 @@ io.on("connection", async (socket) => {
});

/**
* 小夜核心代码,对接go-cqhttp
* qq端消息处理,对接go-cqhttp
*/
async function StartQQBot() {
/**
Expand Down Expand Up @@ -592,7 +595,7 @@ async function StartQQBot() {
if (Constants.is_qq_reg.test(who)) {
// 如果是自己要被闭菊,那么闭菊
if (event.self_id == who) {
logger.error(
console.log(
`群 ${event.group_id} 停止了小夜服务`.error,
);
await utils.DisableGroupService(event.group_id);
Expand All @@ -607,7 +610,7 @@ async function StartQQBot() {
}
// 没指定小夜
} else if (event.message === "闭菊") {
logger.error(
console.log(
`群 ${event.group_id} 停止了小夜服务`.error
);
await utils.DisableGroupService(event.group_id);
Expand Down Expand Up @@ -1259,7 +1262,7 @@ async function StartQQBot() {
}

/**
* qq内嵌的频道的消息处理,并非独立的qq频道
* qq内嵌频道的消息处理,并非独立的qq频道
*/
async function ProcessGuildMessage(event) {
// qq内嵌频道插件应答器
Expand All @@ -1283,9 +1286,9 @@ async function ProcessGuildMessage(event) {
}

/**
* 虚拟主播星野夜蝶核心代码,星野夜蝶上线!
* b站直播端消息处理,虚拟主播星野夜蝶上线!
*/
function StartLive() {
async function StartLive() {
const live = new KeepLiveTCP(BILIBILI_LIVE_ROOM_ID);
live.on("open", () => logger.info(`哔哩哔哩直播间 ${BILIBILI_LIVE_ROOM_ID} 连接成功`.log));

Expand Down Expand Up @@ -1356,9 +1359,9 @@ function StartLive() {
}

/**
* 接入QQ频道
* qq频道消息处理,需要注册独立的qq频道bot号
*/
function StartQQGuild() {
async function StartQQGuild() {
const testConfig = {
appID: QQ_GUILD_APP_ID,
token: QQ_GUILD_TOKEN,
Expand Down Expand Up @@ -1455,7 +1458,51 @@ function StartQQGuild() {

}
});
}

/**
* Telegram端消息处理
*/
async function StartTelegram() {
const telegramClient = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true });
telegramClient.on("message", async (data) => {
const chatId = data.chat.id;
const content = data.text;
const userName = data.from.username;
logger.info(`[Telegram] 收到用户 ${userName} 的消息: ${content}`);

// Telegram插件应答器
const pluginsReply = await ProcessExecute(
content,
data.from.id,
userName,
chatId,
"",
{
type: "telegram",
}
);

if (pluginsReply) {
const replyToTelegram = utils.PluginAnswerToTelegramStyle(pluginsReply);
if (pluginsReply.type == "text") {
telegramClient.sendMessage(chatId, replyToTelegram.text);
}
else if (pluginsReply.type == "picture" || pluginsReply.type == "directPicture") {
telegramClient.sendPhoto(chatId, replyToTelegram.image, {}, {
contentType: "image/jpeg",
});
}
else if (pluginsReply.type == "audio") {
telegramClient.sendAudio(chatId, replyToTelegram.audio, {
title: replyToTelegram.text,
duration: replyToTelegram.duration,
}, {
contentType: "audio/mpeg",
});
}
}
});
}

/**
Expand Down Expand Up @@ -1534,13 +1581,16 @@ async function InitConfig() {
CONNECT_GO_CQHTTP_SWITCH = config.System.CONNECT_GO_CQHTTP_SWITCH ?? false;
CONNECT_BILIBILI_LIVE_SWITCH = config.System.CONNECT_BILIBILI_LIVE_SWITCH ?? false;
CONNECT_QQ_GUILD_SWITCH = config.System.CONNECT_QQ_GUILD_SWITCH ?? false;
CONNECT_TELEGRAM_SWITCH = config.System.CONNECT_TELEGRAM_SWITCH ?? false;
WEB_PORT = config.System.WEB_PORT ?? 80;
GO_CQHTTP_SERVICE_ANTI_POST_API = config.System.GO_CQHTTP_SERVICE_ANTI_POST_API ?? "/bot";
GO_CQHTTP_SERVICE_API_URL = config.System.GO_CQHTTP_SERVICE_API_URL ?? "127.0.0.1:5700";

QQ_GUILD_APP_ID = config.ApiKey.QQ_GUILD_APP_ID ?? "";
QQ_GUILD_TOKEN = config.ApiKey.QQ_GUILD_TOKEN ?? "";

TELEGRAM_BOT_TOKEN = config.ApiKey.TELEGRAM_BOT_TOKEN ?? "";

QQBOT_ADMIN_LIST = config.qqBot.QQBOT_ADMIN_LIST; // 小夜的管理员列表
QQ_GROUP_WELCOME_MESSAGE = config.qqBot.QQ_GROUP_WELCOME_MESSAGE; // qq入群欢迎语
QQ_GROUP_POKE_REPLY_MESSAGE = config.qqBot.QQ_GROUP_POKE_REPLY_MESSAGE; // 戳一戳的文案
Expand Down Expand Up @@ -1579,7 +1629,7 @@ async function InitConfig() {
logger.error(`go-cqhttp启动失败,错误原因: ${error}`.error);
return;
}
logger.error("go-cqhttp窗口意外退出,小夜将无法正常使用,请尝试重新启动".error);
logger.error("go-cqhttp窗口意外退出,qq小夜将无法正常使用,请在右下角托盘区右键小夜头像,选择 重启go-cqhttp".error);
return;
});

Expand Down Expand Up @@ -1617,6 +1667,13 @@ async function InitConfig() {
logger.info("小夜QQ频道接入关闭\n".off);
}

if (CONNECT_TELEGRAM_SWITCH) {
logger.info("小夜Telegram接入开启\n".on);
StartTelegram();
} else {
logger.info("小夜Telegram接入关闭\n".off);
}

StartHttpServer();

CheckUpdate();
Expand Down
Loading

0 comments on commit fed908c

Please sign in to comment.