perf: 完善初始化流程,修复一些 bug

This commit is contained in:
凌莞 2022-02-19 15:39:40 +08:00
parent c77e088044
commit 10f4963131
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
5 changed files with 41 additions and 9 deletions

View File

@ -35,6 +35,7 @@ export class Telegram {
public static async create(startArgs: UserAuthParams | BotAuthParams, stringSession = '') {
const bot = new this(stringSession);
await bot.client.start(startArgs);
bot.client.setParseMode('html');
bot.waitForMessageHelper = new WaitForMessageHelper(bot);
bot.client.addEventHandler(bot.onMessage, new NewMessage({}));
return bot;
@ -82,6 +83,16 @@ export class Telegram {
// 上游定义不好好写
return this.client.session.save() as any as string;
}
public async setCommands(commands: Api.BotCommand[], scope: Api.TypeBotCommandScope) {
return await this.client.invoke(
new Api.bots.SetBotCommands({
commands,
langCode: 'zh',
scope,
}),
);
}
}
export class TelegramChat {

View File

@ -0,0 +1,8 @@
import { Api } from 'telegram';
export default {
preSetupCommands: [new Api.BotCommand({
command: 'setup',
description: '执行初始化配置',
})],
};

View File

@ -5,12 +5,13 @@ import { getLogger } from 'log4js';
import { Button } from 'telegram/tl/custom/button';
import setupHelper from '../helpers/setupHelper';
import { Client as OicqClient } from 'oicq';
import commands from '../constants/commands';
export default class SetupController {
private readonly setupService: SetupService;
private log = getLogger('SetupController');
private isInProgress = false;
private waitForFinishCallbacks: Array<(ret: { tgUser: Telegram }) => unknown> = [];
private waitForFinishCallbacks: Array<(ret: { tgUser: Telegram, oicq: OicqClient }) => unknown> = [];
// 创建的 UserBot
private tgUser: Telegram;
private oicq: OicqClient;
@ -18,6 +19,7 @@ export default class SetupController {
constructor(private readonly tgBot: Telegram) {
this.setupService = new SetupService(tgBot);
tgBot.addNewMessageEventHandler(this.handleMessage);
tgBot.setCommands(commands.preSetupCommands, new Api.BotCommandScopeUsers());
}
private handleMessage = async (message: Api.Message) => {
@ -50,7 +52,7 @@ export default class SetupController {
try {
while (!workMode) {
const workModeText = await this.setupService.waitForOwnerInput('欢迎使用 Q2TG v2\n' +
'请选择工作模式,关于工作模式的区别请查看[这里](https://github.com)', [
'请选择工作模式,关于工作模式的区别请查看<a href="https://github.com">这里</a>', [
[Button.text('个人模式', true, true)],
[Button.text('群组模式', true, true)],
]);
@ -132,11 +134,12 @@ export default class SetupController {
await this.setupService.finishConfig();
this.waitForFinishCallbacks.forEach(e => e({
tgUser: this.tgUser,
oicq: this.oicq,
}));
}
public waitForFinish() {
return new Promise<{ tgUser: Telegram }>(resolve => {
return new Promise<{ tgUser: Telegram, oicq: OicqClient }>(resolve => {
this.waitForFinishCallbacks.push(resolve);
});
}

View File

@ -2,6 +2,8 @@ import { Telegram } from './client/Telegram';
import { config } from './providers/userConfig';
import { getLogger, configure } from 'log4js';
import SetupController from './controllers/SetupController';
import { Client as OicqClient } from 'oicq';
import createOicq from './client/oicq';
(async () => {
configure({
@ -17,14 +19,22 @@ import SetupController from './controllers/SetupController';
const tgBot = await Telegram.create({
botAuthToken: process.env.TG_BOT_TOKEN,
});
let tgUser: Telegram;
let tgUser: Telegram, oicq: OicqClient;
log.debug('TG Bot 登录完成');
if (!config.isSetup) {
log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
const setupController = new SetupController(tgBot);
({ tgUser } = await setupController.waitForFinish());
({ tgUser, oicq } = await setupController.waitForFinish());
}
else {
tgUser = await Telegram.connect(config.userBotSession);
config.userBotSession && (tgUser = await Telegram.connect(config.userBotSession));
oicq = await createOicq({
uin: config.qqUin,
password: config.qqPassword,
platform: config.qqPlatform,
onVerifyDevice: () => null,
onVerifySlider: () => null,
onQrCode: () => null,
});
}
})();

View File

@ -52,7 +52,7 @@ export default class SetupService {
if (!this.owner) {
throw new Error('应该不会运行到这里');
}
message && await this.owner.sendMessage({ message, buttons: buttons || Button.clear(), parseMode: 'md' });
message && await this.owner.sendMessage({ message, buttons: buttons || Button.clear(), linkPreview: false });
const { message: reply } = await this.owner.waitForInput();
return reply;
}
@ -92,8 +92,8 @@ export default class SetupService {
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
},
onVerifySlider: async (url) => {
const res = await this.waitForOwnerInput(`收到滑块验证码 \`${url}\`\n` +
'请使用[此软件](https://github.com/mzdluo123/TxCaptchaHelper/releases)验证并输入 Ticket\n' +
const res = await this.waitForOwnerInput(`收到滑块验证码 <code>${url}</code>\n` +
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket\n' +
'或者点击下方的按钮切换到扫码登录', [
Button.text('切换到扫码登录', true, true),
]);