Q2TG/src/services/SetupService.ts

105 lines
3.8 KiB
TypeScript
Raw Permalink Normal View History

2022-02-23 09:11:04 +00:00
import Telegram from '../client/Telegram';
2022-03-07 10:14:45 +00:00
import { getLogger, Logger } from 'log4js';
2022-02-17 13:09:12 +00:00
import { BigInteger } from 'big-integer';
2023-06-30 11:26:45 +00:00
import { Platform } from 'icqq';
2022-02-18 06:22:29 +00:00
import { MarkupLike } from 'telegram/define';
2022-02-23 06:09:54 +00:00
import OicqClient from '../client/OicqClient';
2022-02-18 06:22:29 +00:00
import { Button } from 'telegram/tl/custom/button';
import { CustomFile } from 'telegram/client/uploads';
import { WorkMode } from '../types/definitions';
2022-02-23 09:11:04 +00:00
import TelegramChat from '../client/TelegramChat';
import Instance from '../models/Instance';
import db from '../models/db';
2022-02-17 13:09:12 +00:00
export default class SetupService {
private owner: TelegramChat;
2022-03-07 10:14:45 +00:00
private readonly log: Logger;
2022-02-17 13:09:12 +00:00
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram) {
this.log = getLogger(`SetupService - ${instance.id}`);
2022-02-17 13:09:12 +00:00
}
public setWorkMode(mode: WorkMode) {
this.instance.workMode = mode;
2022-02-19 04:06:43 +00:00
}
2022-02-17 13:09:12 +00:00
/**
* start bot bot
* @param userId ID
* @return {boolean} false
*/
public async claimOwner(userId: number | BigInteger) {
userId = Number(userId);
if (!this.owner) {
this.instance.owner = userId;
2022-02-17 13:09:12 +00:00
await this.setupOwner();
this.log.info(`用户 ID: ${userId} 成为了 Bot 主人`);
return true;
}
return false;
}
private async setupOwner() {
if (!this.owner && this.instance.owner) {
this.owner = await this.tgBot.getChat(this.instance.owner);
2022-02-17 13:09:12 +00:00
}
}
public async informOwner(message: string, buttons?: MarkupLike) {
2022-02-17 13:09:12 +00:00
if (!this.owner) {
throw new Error('应该不会运行到这里');
}
2022-02-25 16:15:53 +00:00
return await this.owner.sendMessage({ message, buttons: buttons || Button.clear(), linkPreview: false });
2022-02-17 13:09:12 +00:00
}
2022-02-25 16:15:53 +00:00
public async waitForOwnerInput(message?: string, buttons?: MarkupLike, remove = false) {
2022-02-17 13:09:12 +00:00
if (!this.owner) {
throw new Error('应该不会运行到这里');
}
message && await this.informOwner(message, buttons);
2022-02-25 16:15:53 +00:00
const reply = await this.owner.waitForInput();
remove && await reply.delete({ revoke: true });
return reply.message;
2022-02-17 13:09:12 +00:00
}
public async createUserBot(phoneNumber: string) {
if (!this.owner) {
throw new Error('应该不会运行到这里');
}
2022-02-17 13:40:09 +00:00
return await Telegram.create({
2022-02-17 13:09:12 +00:00
phoneNumber,
password: async (hint?: string) => {
2022-02-25 16:15:53 +00:00
return await this.waitForOwnerInput(
`请输入你的二步验证密码${hint ? '\n密码提示' + hint : ''}`, undefined, true);
2022-02-17 13:09:12 +00:00
},
phoneCode: async (isCodeViaApp?: boolean) => {
await this.informOwner(`请输入你${isCodeViaApp ? ' Telegram APP 中' : '手机上'}收到的验证码\n` +
'👇请使用下面的按钮输入,不要在文本框输入,<b>否则验证码会发不出去并立即失效</b>',
Button.text('👆请使用上面的按钮输入', true, true));
return await this.owner.inlineDigitInput(5);
2022-02-17 13:09:12 +00:00
},
onError: (err) => this.log.error(err),
});
2022-02-17 13:40:09 +00:00
}
2023-07-29 17:48:06 +00:00
public async createOicq(uin: number, password: string, platform: Platform, signApi: string, signVer: string) {
const dbQQBot = await db.qqBot.create({ data: { uin, password, platform, signApi, signVer } });
2022-02-23 06:09:54 +00:00
return await OicqClient.create({
id: dbQQBot.id,
uin, password, platform, signApi, signVer,
2022-02-18 06:22:29 +00:00
onVerifyDevice: async (phone) => {
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
},
onVerifySlider: async (url) => {
return await this.waitForOwnerInput(`收到滑块验证码 <code>${url}</code>\n` +
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket');
2022-02-18 06:22:29 +00:00
},
});
}
2022-02-17 15:26:48 +00:00
public async finishConfig() {
this.instance.isSetup = true;
2022-02-17 13:09:12 +00:00
}
}