mirror of https://github.com/Nofated095/Q2TG.git
perf: 完善初始化流程,修复一些 bug
This commit is contained in:
parent
c77e088044
commit
10f4963131
|
@ -35,6 +35,7 @@ export class Telegram {
|
||||||
public static async create(startArgs: UserAuthParams | BotAuthParams, stringSession = '') {
|
public static async create(startArgs: UserAuthParams | BotAuthParams, stringSession = '') {
|
||||||
const bot = new this(stringSession);
|
const bot = new this(stringSession);
|
||||||
await bot.client.start(startArgs);
|
await bot.client.start(startArgs);
|
||||||
|
bot.client.setParseMode('html');
|
||||||
bot.waitForMessageHelper = new WaitForMessageHelper(bot);
|
bot.waitForMessageHelper = new WaitForMessageHelper(bot);
|
||||||
bot.client.addEventHandler(bot.onMessage, new NewMessage({}));
|
bot.client.addEventHandler(bot.onMessage, new NewMessage({}));
|
||||||
return bot;
|
return bot;
|
||||||
|
@ -82,6 +83,16 @@ export class Telegram {
|
||||||
// 上游定义不好好写
|
// 上游定义不好好写
|
||||||
return this.client.session.save() as any as string;
|
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 {
|
export class TelegramChat {
|
||||||
|
|
|
@ -0,0 +1,8 @@
|
||||||
|
import { Api } from 'telegram';
|
||||||
|
|
||||||
|
export default {
|
||||||
|
preSetupCommands: [new Api.BotCommand({
|
||||||
|
command: 'setup',
|
||||||
|
description: '执行初始化配置',
|
||||||
|
})],
|
||||||
|
};
|
|
@ -5,12 +5,13 @@ import { getLogger } from 'log4js';
|
||||||
import { Button } from 'telegram/tl/custom/button';
|
import { Button } from 'telegram/tl/custom/button';
|
||||||
import setupHelper from '../helpers/setupHelper';
|
import setupHelper from '../helpers/setupHelper';
|
||||||
import { Client as OicqClient } from 'oicq';
|
import { Client as OicqClient } from 'oicq';
|
||||||
|
import commands from '../constants/commands';
|
||||||
|
|
||||||
export default class SetupController {
|
export default class SetupController {
|
||||||
private readonly setupService: SetupService;
|
private readonly setupService: SetupService;
|
||||||
private log = getLogger('SetupController');
|
private log = getLogger('SetupController');
|
||||||
private isInProgress = false;
|
private isInProgress = false;
|
||||||
private waitForFinishCallbacks: Array<(ret: { tgUser: Telegram }) => unknown> = [];
|
private waitForFinishCallbacks: Array<(ret: { tgUser: Telegram, oicq: OicqClient }) => unknown> = [];
|
||||||
// 创建的 UserBot
|
// 创建的 UserBot
|
||||||
private tgUser: Telegram;
|
private tgUser: Telegram;
|
||||||
private oicq: OicqClient;
|
private oicq: OicqClient;
|
||||||
|
@ -18,6 +19,7 @@ export default class SetupController {
|
||||||
constructor(private readonly tgBot: Telegram) {
|
constructor(private readonly tgBot: Telegram) {
|
||||||
this.setupService = new SetupService(tgBot);
|
this.setupService = new SetupService(tgBot);
|
||||||
tgBot.addNewMessageEventHandler(this.handleMessage);
|
tgBot.addNewMessageEventHandler(this.handleMessage);
|
||||||
|
tgBot.setCommands(commands.preSetupCommands, new Api.BotCommandScopeUsers());
|
||||||
}
|
}
|
||||||
|
|
||||||
private handleMessage = async (message: Api.Message) => {
|
private handleMessage = async (message: Api.Message) => {
|
||||||
|
@ -50,7 +52,7 @@ export default class SetupController {
|
||||||
try {
|
try {
|
||||||
while (!workMode) {
|
while (!workMode) {
|
||||||
const workModeText = await this.setupService.waitForOwnerInput('欢迎使用 Q2TG v2\n' +
|
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)],
|
||||||
[Button.text('群组模式', true, true)],
|
[Button.text('群组模式', true, true)],
|
||||||
]);
|
]);
|
||||||
|
@ -132,11 +134,12 @@ export default class SetupController {
|
||||||
await this.setupService.finishConfig();
|
await this.setupService.finishConfig();
|
||||||
this.waitForFinishCallbacks.forEach(e => e({
|
this.waitForFinishCallbacks.forEach(e => e({
|
||||||
tgUser: this.tgUser,
|
tgUser: this.tgUser,
|
||||||
|
oicq: this.oicq,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
public waitForFinish() {
|
public waitForFinish() {
|
||||||
return new Promise<{ tgUser: Telegram }>(resolve => {
|
return new Promise<{ tgUser: Telegram, oicq: OicqClient }>(resolve => {
|
||||||
this.waitForFinishCallbacks.push(resolve);
|
this.waitForFinishCallbacks.push(resolve);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
16
src/index.ts
16
src/index.ts
|
@ -2,6 +2,8 @@ import { Telegram } from './client/Telegram';
|
||||||
import { config } from './providers/userConfig';
|
import { config } from './providers/userConfig';
|
||||||
import { getLogger, configure } from 'log4js';
|
import { getLogger, configure } from 'log4js';
|
||||||
import SetupController from './controllers/SetupController';
|
import SetupController from './controllers/SetupController';
|
||||||
|
import { Client as OicqClient } from 'oicq';
|
||||||
|
import createOicq from './client/oicq';
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
configure({
|
configure({
|
||||||
|
@ -17,14 +19,22 @@ import SetupController from './controllers/SetupController';
|
||||||
const tgBot = await Telegram.create({
|
const tgBot = await Telegram.create({
|
||||||
botAuthToken: process.env.TG_BOT_TOKEN,
|
botAuthToken: process.env.TG_BOT_TOKEN,
|
||||||
});
|
});
|
||||||
let tgUser: Telegram;
|
let tgUser: Telegram, oicq: OicqClient;
|
||||||
log.debug('TG Bot 登录完成');
|
log.debug('TG Bot 登录完成');
|
||||||
if (!config.isSetup) {
|
if (!config.isSetup) {
|
||||||
log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
|
log.info('当前服务器未配置,请向 Bot 发送 /setup 来设置');
|
||||||
const setupController = new SetupController(tgBot);
|
const setupController = new SetupController(tgBot);
|
||||||
({ tgUser } = await setupController.waitForFinish());
|
({ tgUser, oicq } = await setupController.waitForFinish());
|
||||||
}
|
}
|
||||||
else {
|
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,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
|
|
|
@ -52,7 +52,7 @@ export default class SetupService {
|
||||||
if (!this.owner) {
|
if (!this.owner) {
|
||||||
throw new Error('应该不会运行到这里');
|
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();
|
const { message: reply } = await this.owner.waitForInput();
|
||||||
return reply;
|
return reply;
|
||||||
}
|
}
|
||||||
|
@ -92,8 +92,8 @@ export default class SetupService {
|
||||||
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
|
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
|
||||||
},
|
},
|
||||||
onVerifySlider: async (url) => {
|
onVerifySlider: async (url) => {
|
||||||
const res = await this.waitForOwnerInput(`收到滑块验证码 \`${url}\`\n` +
|
const res = await this.waitForOwnerInput(`收到滑块验证码 <code>${url}</code>\n` +
|
||||||
'请使用[此软件](https://github.com/mzdluo123/TxCaptchaHelper/releases)验证并输入 Ticket\n' +
|
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket\n' +
|
||||||
'或者点击下方的按钮切换到扫码登录', [
|
'或者点击下方的按钮切换到扫码登录', [
|
||||||
Button.text('切换到扫码登录', true, true),
|
Button.text('切换到扫码登录', true, true),
|
||||||
]);
|
]);
|
||||||
|
|
Loading…
Reference in New Issue