From 43cdd870e9091cd5373562fbbd858790130915d9 Mon Sep 17 00:00:00 2001 From: Clansty Date: Fri, 25 Feb 2022 23:47:47 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BD=BF=E7=94=A8=E8=A1=8C=E5=86=85?= =?UTF-8?q?=E9=94=AE=E7=9B=98=E8=BE=93=E5=85=A5=E7=99=BB=E5=BD=95=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=20https://t.me/Clanstty/234?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/TelegramChat.ts | 5 +++ src/client/TelegramSession.ts | 4 +++ src/services/SetupService.ts | 11 ++++--- src/utils/inlineDigitInput.ts | 59 +++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 src/utils/inlineDigitInput.ts diff --git a/src/client/TelegramChat.ts b/src/client/TelegramChat.ts index a0614b9..f99ebf0 100644 --- a/src/client/TelegramChat.ts +++ b/src/client/TelegramChat.ts @@ -6,6 +6,7 @@ import { SendMessageParams } from 'telegram/client/messages'; import { CustomFile } from 'telegram/client/uploads'; import Telegram from './Telegram'; import createPaginatedInlineSelector from '../utils/paginatedInlineSelector'; +import inlineDigitInput from '../utils/inlineDigitInput'; export default class TelegramChat { public readonly inputPeer: Api.TypeInputPeer; @@ -50,6 +51,10 @@ export default class TelegramChat { return createPaginatedInlineSelector(this, message, choices); } + public inlineDigitInput(length: number) { + return inlineDigitInput(this, length); + } + public async setProfilePhoto(photo: Buffer) { if (!(this.entity instanceof Api.Chat)) throw new Error('不是群组,无法设置头像'); diff --git a/src/client/TelegramSession.ts b/src/client/TelegramSession.ts index ded461b..0bb7feb 100644 --- a/src/client/TelegramSession.ts +++ b/src/client/TelegramSession.ts @@ -68,6 +68,10 @@ export default class TelegramSession extends MemorySession { .then(e => this.log.trace('authKey update result', e)); } + get authKey() { + return this._authKey; + } + processEntities(tlo: any) { this.log.trace('processEntities'); const entitiesSet = this._entitiesToRows(tlo); diff --git a/src/services/SetupService.ts b/src/services/SetupService.ts index 2a70697..f76d9a0 100644 --- a/src/services/SetupService.ts +++ b/src/services/SetupService.ts @@ -43,18 +43,18 @@ export default class SetupService { } } - public async informOwner(message: string) { + public async informOwner(message: string, buttons?: MarkupLike) { if (!this.owner) { throw new Error('应该不会运行到这里'); } - await this.owner.sendMessage({ message }); + await this.owner.sendMessage({ message, buttons: buttons || Button.clear(), linkPreview: false }); } public async waitForOwnerInput(message?: string, buttons?: MarkupLike) { if (!this.owner) { throw new Error('应该不会运行到这里'); } - message && await this.owner.sendMessage({ message, buttons: buttons || Button.clear(), linkPreview: false }); + message && await this.informOwner(message, buttons); const { message: reply } = await this.owner.waitForInput(); return reply; } @@ -69,7 +69,10 @@ export default class SetupService { return await this.waitForOwnerInput(`请输入你的二步验证密码${hint ? '\n密码提示:' + hint : ''}`); }, phoneCode: async (isCodeViaApp?: boolean) => { - return await this.waitForOwnerInput(`请输入你${isCodeViaApp ? ' Telegram APP 中' : '手机上'}收到的验证码`); + await this.informOwner(`请输入你${isCodeViaApp ? ' Telegram APP 中' : '手机上'}收到的验证码\n` + + '👇请使用下面的按钮输入,不要在文本框输入,否则验证码会发不出去并立即失效', + Button.text('👆请使用上面的按钮输入', true, true)); + return await this.owner.inlineDigitInput(5); }, onError: (err) => this.log.error(err), }, 'user'); diff --git a/src/utils/inlineDigitInput.ts b/src/utils/inlineDigitInput.ts new file mode 100644 index 0000000..05e1868 --- /dev/null +++ b/src/utils/inlineDigitInput.ts @@ -0,0 +1,59 @@ +import TelegramChat from '../client/TelegramChat'; +import { Button } from 'telegram/tl/custom/button'; + +export default async function inlineDigitInput(chat: TelegramChat, length: number) { + return new Promise(async resolve => { + const SYMBOL_EMPTY = '-'; + const SYMBOL_INPUT = '_'; + const SYMBOL_SPACE = ' '; + + let input = ''; + + function getDisplay() { + const leftLength = length - input.length; + let display = Array.from(input); + leftLength > 0 && display.push(SYMBOL_INPUT); + leftLength > 1 && display.push(...SYMBOL_EMPTY.repeat(leftLength - 1)); + // 增大一点键盘的大小,方便按 + return `>>> ${display.join(SYMBOL_SPACE)} <<<`; + } + + function refreshDisplay() { + if (input.length === length) { + resolve(input); + message.edit({ + text: `${input}`, + buttons: Button.clear(), + }); + return; + } + message.edit({ + text: getDisplay(), + }); + } + + function inputButton(digit: number | string) { + digit = digit.toString(); + return Button.inline(digit, chat.parent.registerCallback(() => { + input += digit; + refreshDisplay(); + })); + } + + const backspaceButton = Button.inline('⌫', chat.parent.registerCallback(() => { + if (!input.length) return; + input = input.substring(0, input.length - 1); + refreshDisplay(); + })); + + const message = await chat.sendMessage({ + message: getDisplay(), + buttons: [ + [inputButton(1), inputButton(2), inputButton(3)], + [inputButton(4), inputButton(5), inputButton(6)], + [inputButton(7), inputButton(8), inputButton(9)], + [inputButton(0), backspaceButton], + ], + }); + }); +}