From 6e86ca464a3feec9a51dd17dc3bf04eeb5041513 Mon Sep 17 00:00:00 2001 From: teidesu Date: Sun, 16 May 2021 12:59:00 +0300 Subject: [PATCH] feat(client): getCallbackAnswer method --- packages/client/src/client.ts | 38 ++++++++++++ .../src/methods/bots/get-callback-answer.ts | 61 +++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 packages/client/src/methods/bots/get-callback-answer.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 8e6932bf..f01be465 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -17,6 +17,7 @@ import { startTest } from './methods/auth/start-test' import { start } from './methods/auth/start' import { answerCallbackQuery } from './methods/bots/answer-callback-query' import { answerInlineQuery } from './methods/bots/answer-inline-query' +import { getCallbackAnswer } from './methods/bots/get-callback-answer' import { getGameHighScores, getInlineGameHighScores, @@ -589,6 +590,42 @@ export interface TelegramClient extends BaseTelegramClient { parseMode?: string | null } ): Promise + /** + * Request a callback answer from a bot, + * i.e. click an inline button that contains data. + * + * @param chatId Chat ID where the message was found + * @param message ID of the message containing the button + * @param data Data contained in the button + * @param params + */ + getCallbackAnswer( + chatId: InputPeerLike, + message: number, + data: string | Buffer, + params?: { + /** + * Timeout for the query in ms. + * + * Defaults to `10000` (10 sec) + */ + timeout?: number + + /** + * Whether this is a "play game" button + */ + game?: boolean + + /** + * If the button requires password entry, + * your 2FA password. + * + * Your password is never exposed to the + * bot, it is checked by Telegram. + */ + password?: string + } + ): Promise /** * Get high scores of a game * @@ -2961,6 +2998,7 @@ export class TelegramClient extends BaseTelegramClient { start = start answerCallbackQuery = answerCallbackQuery answerInlineQuery = answerInlineQuery + getCallbackAnswer = getCallbackAnswer getGameHighScores = getGameHighScores getInlineGameHighScores = getInlineGameHighScores setGameScore = setGameScore diff --git a/packages/client/src/methods/bots/get-callback-answer.ts b/packages/client/src/methods/bots/get-callback-answer.ts new file mode 100644 index 00000000..38165eb7 --- /dev/null +++ b/packages/client/src/methods/bots/get-callback-answer.ts @@ -0,0 +1,61 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { tl } from '@mtcute/tl' +import { computeSrpParams } from '@mtcute/core/dist' + +/** + * Request a callback answer from a bot, + * i.e. click an inline button that contains data. + * + * @param chatId Chat ID where the message was found + * @param message ID of the message containing the button + * @param data Data contained in the button + * @param params + * @internal + */ +export async function getCallbackAnswer( + this: TelegramClient, + chatId: InputPeerLike, + message: number, + data: string | Buffer, + params?: { + /** + * Timeout for the query in ms. + * + * Defaults to `10000` (10 sec) + */ + timeout?: number + + /** + * Whether this is a "play game" button + */ + game?: boolean + + /** + * If the button requires password entry, + * your 2FA password. + * + * Your password is never exposed to the + * bot, it is checked by Telegram. + */ + password?: string + } +): Promise { + let password: tl.TypeInputCheckPasswordSRP | undefined = undefined + if (params?.password) { + const pwd = await this.call({ _: 'account.getPassword' }) + password = await computeSrpParams(this._crypto, pwd, params.password) + } + + return await this.call( + { + _: 'messages.getBotCallbackAnswer', + peer: await this.resolvePeer(chatId), + msgId: message, + data: typeof data === 'string' ? Buffer.from(data) : data, + password, + game: params?.game, + }, + { timeout: params?.timeout ?? 10000 } + ) +}