feat: 从零号实例创建新的实例

This commit is contained in:
Clansty 2022-03-07 20:50:05 +08:00
parent 3240b7fe9c
commit 570e67eb12
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
6 changed files with 97 additions and 36 deletions

View File

@ -0,0 +1,38 @@
import { getLogger } from 'log4js';
import Telegram from '../client/Telegram';
import { Api } from 'telegram';
import Instance from '../models/Instance';
import { Button } from 'telegram/tl/custom/button';
export default class InstanceManageController {
private readonly log = getLogger('InstanceManageController');
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram) {
tgBot.addNewMessageEventHandler(this.onTelegramMessage);
}
private onTelegramMessage = async (message: Api.Message) => {
if (!(message.chat.id.eq(this.instance.owner) && message.message)) return;
const messageSplit = message.message.split(' ');
if (messageSplit[0] !== '/newinstance') return;
if (messageSplit.length === 1) {
await message.reply({
message: '通过 <code>/newinstance 新的 Bot API Token</code> 创建一个新的转发机器人实例',
});
return true;
}
else {
await message.reply({
message: `正在创建,请稍候`,
});
const newInstance = await Instance.createNew(messageSplit[1]);
this.log.info(`已创建新的实例 实例 ID: ${newInstance.id} Bot Token: ${messageSplit[1]}`);
await message.reply({
message: `已创建新的实例\n实例 ID: ${newInstance.id}`,
buttons: Button.url('去配置', `https://t.me/${newInstance.botMe.username}?start=setup`),
});
return true;
}
};
}

View File

@ -33,7 +33,7 @@ export default class SetupController {
return false;
}
if (message.text === '/setup') {
if (message.text === '/setup' || message.text === '/start setup') {
this.isInProgress = true;
await this.doSetup(Number(message.sender.id));
await this.finishSetup();

View File

@ -1,5 +1,6 @@
import { configure, getLogger } from 'log4js';
import Instance from './models/Instance';
import db from './models/db';
(async () => {
configure({
@ -14,5 +15,13 @@ import Instance from './models/Instance';
process.on('unhandledRejection', error => {
log.error('UnhandledException: ', error);
});
await Instance.start(0);
const instanceEntries = await db.instance.findMany();
if (!instanceEntries.length) {
await Instance.start(0);
}
else {
for (const instanceEntry of instanceEntries) {
await Instance.start(instanceEntry.id);
}
}
})();

View File

@ -10,6 +10,7 @@ import Telegram from '../client/Telegram';
import OicqClient from '../client/OicqClient';
import { getLogger, Logger } from 'log4js';
import ForwardPairs from './ForwardPairs';
import InstanceManageController from '../controllers/InstanceManageController';
export default class Instance {
private _owner = 0;
@ -22,15 +23,20 @@ export default class Instance {
private readonly log: Logger;
private tgBot: Telegram;
private tgUser: Telegram;
private oicq: OicqClient;
public forwardPairs: ForwardPairs;
private setupController: SetupController;
private instanceManageController: InstanceManageController;
private configController: ConfigController;
private deleteMessageController: DeleteMessageController;
private forwardController: ForwardController;
private fileAndFlashPhotoController: FileAndFlashPhotoController;
private constructor(public readonly id: number) {
this.log = getLogger(`Instance ${this.id}`);
this.log = getLogger(`Instance - ${this.id}`);
}
private async load() {
@ -61,37 +67,42 @@ export default class Instance {
private async init() {
this.log.debug('正在登录 TG Bot');
const tgBot = await Telegram.create({
this.tgBot = await Telegram.create({
botAuthToken: this.botToken,
}, 'bot');
let tgUser: Telegram, oicq: OicqClient;
this.log.debug('TG Bot 登录完成');
if (!this.isSetup) {
this.log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
this.setupController = new SetupController(this, tgBot);
({ tgUser, oicq } = await this.setupController.waitForFinish());
}
else {
this.log.debug('正在登录 TG UserBot');
tgUser = await Telegram.connect('user');
this.log.debug('TG UserBot 登录完成');
this.log.debug('正在登录 OICQ');
oicq = await OicqClient.create({
uin: this.qqUin,
password: this.qqPassword,
platform: this.qqPlatform,
onVerifyDevice: () => null,
onVerifySlider: () => null,
onQrCode: () => null,
});
this.log.debug('OICQ 登录完成');
}
this.forwardPairs = await ForwardPairs.load(this.id, oicq, tgBot);
this.configController = new ConfigController(this, tgBot, tgUser, oicq);
this.deleteMessageController = new DeleteMessageController(this, tgBot, tgUser, oicq);
this.forwardController = new ForwardController(this, tgBot, tgUser, oicq);
this.fileAndFlashPhotoController = new FileAndFlashPhotoController(this, tgBot, oicq);
}, `bot:${this.id}`);
this.log.info('TG Bot 登录完成');
(async () => {
if (!this.isSetup) {
this.log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
this.setupController = new SetupController(this, this.tgBot);
// 这会一直卡在这里,所以要新开一个异步来做,提前返回掉上面的
({ tgUser: this.tgUser, oicq: this.oicq } = await this.setupController.waitForFinish());
}
else {
this.log.debug('正在登录 TG UserBot');
this.tgUser = await Telegram.connect(`user:${this.id}`);
this.log.info('TG UserBot 登录完成');
this.log.debug('正在登录 OICQ');
this.oicq = await OicqClient.create({
uin: this.qqUin,
password: this.qqPassword,
platform: this.qqPlatform,
onVerifyDevice: () => null,
onVerifySlider: () => null,
onQrCode: () => null,
});
this.log.info('OICQ 登录完成');
}
this.forwardPairs = await ForwardPairs.load(this.id, this.oicq, this.tgBot);
if (this.id === 0) {
this.instanceManageController = new InstanceManageController(this, this.tgBot);
}
this.configController = new ConfigController(this, this.tgBot, this.tgUser, this.oicq);
this.deleteMessageController = new DeleteMessageController(this, this.tgBot, this.tgUser, this.oicq);
this.forwardController = new ForwardController(this, this.tgBot, this.tgUser, this.oicq);
this.fileAndFlashPhotoController = new FileAndFlashPhotoController(this, this.tgBot, this.oicq);
})()
.then(() => this.log.info('初始化已完成'));
}
public static async start(instanceId: number) {
@ -136,6 +147,10 @@ export default class Instance {
return this.id === 0 ? process.env.TG_BOT_TOKEN : this._botToken;
}
get botMe() {
return this.tgBot.me;
}
set owner(owner: number) {
this._owner = owner;
db.instance.update({

View File

@ -52,7 +52,6 @@ export default class DeleteMessageService {
}
}
catch (e) {
this.log.error('处理 Telegram 消息删除失败', e);
}
}

View File

@ -16,7 +16,7 @@ export default class SetupService {
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram) {
this.log = getLogger(`SetupService - ${instance.id}`)
this.log = getLogger(`SetupService - ${instance.id}`);
}
public setWorkMode(mode: WorkMode) {
@ -79,7 +79,7 @@ export default class SetupService {
return await this.owner.inlineDigitInput(5);
},
onError: (err) => this.log.error(err),
}, 'user');
}, `user:${this.instance.id}`);
}
public async createOicq(uin: number, password: string, platform: Platform) {