2022-02-23 09:11:04 +00:00
|
|
|
|
import Telegram from '../client/Telegram';
|
2022-02-17 13:09:12 +00:00
|
|
|
|
import { config, saveConfig } from '../providers/userConfig';
|
|
|
|
|
import { getLogger } from 'log4js';
|
|
|
|
|
import { BigInteger } from 'big-integer';
|
2022-02-18 04:48:28 +00:00
|
|
|
|
import { Platform } from 'oicq';
|
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';
|
2022-02-18 06:36:17 +00:00
|
|
|
|
import { CustomFile } from 'telegram/client/uploads';
|
2022-02-22 11:17:17 +00:00
|
|
|
|
import { WorkMode } from '../types/definitions';
|
2022-02-23 09:11:04 +00:00
|
|
|
|
import TelegramChat from '../client/TelegramChat';
|
2022-02-17 13:09:12 +00:00
|
|
|
|
|
|
|
|
|
export default class SetupService {
|
|
|
|
|
private owner: TelegramChat;
|
|
|
|
|
private log = getLogger('SetupService');
|
|
|
|
|
|
|
|
|
|
constructor(private readonly tgBot: Telegram) {
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-22 11:17:17 +00:00
|
|
|
|
public setWorkMode(mode: WorkMode) {
|
2022-02-19 04:06:43 +00:00
|
|
|
|
config.workMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
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) {
|
|
|
|
|
config.owner = userId;
|
|
|
|
|
await this.setupOwner();
|
|
|
|
|
this.log.info(`用户 ID: ${userId} 成为了 Bot 主人`);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async setupOwner() {
|
|
|
|
|
if (!this.owner && config.owner) {
|
|
|
|
|
this.owner = await this.tgBot.getChat(config.owner);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-25 15:47:47 +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('应该不会运行到这里');
|
|
|
|
|
}
|
2022-02-25 15:47:47 +00:00
|
|
|
|
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) => {
|
2022-02-25 15:47:47 +00:00
|
|
|
|
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-24 13:16:19 +00:00
|
|
|
|
}, 'user');
|
2022-02-17 13:40:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-18 04:48:28 +00:00
|
|
|
|
public async createOicq(uin: number, password: string, platform: Platform) {
|
2022-02-23 06:09:54 +00:00
|
|
|
|
return await OicqClient.create({
|
2022-02-18 06:22:29 +00:00
|
|
|
|
uin, password, platform,
|
|
|
|
|
onQrCode: async (file) => {
|
|
|
|
|
await this.owner.sendMessage({
|
|
|
|
|
message: '请使用已登录这个账号的手机 QQ 扫描这个二维码授权',
|
2022-02-18 06:36:17 +00:00
|
|
|
|
file: new CustomFile('qrcode.png', file.length, '', file),
|
2022-02-18 06:22:29 +00:00
|
|
|
|
buttons: Button.text('我已扫码', true, true),
|
|
|
|
|
});
|
|
|
|
|
await this.waitForOwnerInput();
|
|
|
|
|
},
|
|
|
|
|
onVerifyDevice: async (phone) => {
|
|
|
|
|
return await this.waitForOwnerInput(`请输入手机 ${phone} 收到的验证码`);
|
|
|
|
|
},
|
|
|
|
|
onVerifySlider: async (url) => {
|
2022-02-19 07:39:40 +00:00
|
|
|
|
const res = await this.waitForOwnerInput(`收到滑块验证码 <code>${url}</code>\n` +
|
|
|
|
|
'请使用<a href="https://github.com/mzdluo123/TxCaptchaHelper/releases">此软件</a>验证并输入 Ticket\n' +
|
2022-02-18 06:22:29 +00:00
|
|
|
|
'或者点击下方的按钮切换到扫码登录', [
|
|
|
|
|
Button.text('切换到扫码登录', true, true),
|
|
|
|
|
]);
|
|
|
|
|
if (res === '切换到扫码登录') return '';
|
|
|
|
|
return res;
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
}
|
2022-02-18 04:48:28 +00:00
|
|
|
|
|
2022-02-18 06:22:29 +00:00
|
|
|
|
public saveOicqLoginInfo(uin: number, password: string, platform: Platform) {
|
|
|
|
|
config.qqUin = uin;
|
|
|
|
|
config.qqPassword = password;
|
|
|
|
|
config.qqPlatform = platform;
|
2022-02-18 04:48:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 15:26:48 +00:00
|
|
|
|
public async finishConfig() {
|
|
|
|
|
config.isSetup = true;
|
2022-02-17 13:40:09 +00:00
|
|
|
|
await saveConfig();
|
2022-02-17 13:09:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|