fix: 使用行内键盘输入登录验证码

https://t.me/Clanstty/234
This commit is contained in:
Clansty 2022-02-25 23:47:47 +08:00
parent 5275f8830d
commit 43cdd870e9
No known key found for this signature in database
GPG Key ID: 05F8479BA63A8E92
4 changed files with 75 additions and 4 deletions

View File

@ -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('不是群组,无法设置头像');

View File

@ -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);

View File

@ -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` +
'👇请使用下面的按钮输入,不要在文本框输入,<b>否则验证码会发不出去并立即失效</b>',
Button.text('👆请使用上面的按钮输入', true, true));
return await this.owner.inlineDigitInput(5);
},
onError: (err) => this.log.error(err),
}, 'user');

View File

@ -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<string>(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: `<b>${input}</b>`,
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],
],
});
});
}