mirror of https://github.com/Nofated095/Q2TG.git
feat: 从零号实例创建新的实例
This commit is contained in:
parent
3240b7fe9c
commit
570e67eb12
|
@ -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;
|
||||
}
|
||||
};
|
||||
}
|
|
@ -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();
|
||||
|
|
11
src/index.ts
11
src/index.ts
|
@ -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);
|
||||
}
|
||||
}
|
||||
})();
|
||||
|
|
|
@ -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({
|
||||
|
|
|
@ -52,7 +52,6 @@ export default class DeleteMessageService {
|
|||
}
|
||||
}
|
||||
catch (e) {
|
||||
this.log.error('处理 Telegram 消息删除失败', e);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue