From fdec2b862101e1d69747282b11ae626efe8c86d9 Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Sun, 29 Oct 2023 00:48:37 +0300 Subject: [PATCH] fix: improved surface api --- packages/client/src/client.ts | 66 ++++++++------ .../src/methods/bots/answer-inline-query.ts | 25 +++++- .../src/methods/bots/get-callback-answer.ts | 13 +-- packages/client/typedoc.cjs | 1 + packages/dispatcher/src/filters/bots.ts | 90 +++++++++++-------- 5 files changed, 119 insertions(+), 76 deletions(-) diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 5c3ad658..cdbf5dfd 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -835,6 +835,22 @@ export interface TelegramClient extends BaseTelegramClient { parameter: string } + /** + * If passed, clients will display a button on top of the remaining inline result + * list with the specified text, that switches the user to the specified bot web app. + */ + switchWebview?: { + /** + * Text of the button + */ + text: string + + /** + * URL to open + */ + url: string + } + /** * Parse mode to use when parsing inline message text. * Defaults to current default parse mode (if any). @@ -915,36 +931,32 @@ export interface TelegramClient extends BaseTelegramClient { * * @param params */ - getCallbackAnswer(params: { - /** Chat ID where the message was found */ - chatId: InputPeerLike + getCallbackAnswer( + params: InputMessageId & { + /** Data contained in the button */ + data: string | Uint8Array - /** ID of the message containing the button */ - message: number + /** + * Timeout for the query in ms. + * + * Defaults to `10000` (10 sec) + */ + timeout?: number - /** Data contained in the button */ - data: string | Uint8Array + /** + * Whether this is a "play game" button + */ + game?: boolean - /** - * 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 + /** + * 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 * **Available**: 🤖 bots only diff --git a/packages/client/src/methods/bots/answer-inline-query.ts b/packages/client/src/methods/bots/answer-inline-query.ts index 622829d2..f60dc7fb 100644 --- a/packages/client/src/methods/bots/answer-inline-query.ts +++ b/packages/client/src/methods/bots/answer-inline-query.ts @@ -81,6 +81,22 @@ export async function answerInlineQuery( parameter: string } + /** + * If passed, clients will display a button on top of the remaining inline result + * list with the specified text, that switches the user to the specified bot web app. + */ + switchWebview?: { + /** + * Text of the button + */ + text: string + + /** + * URL to open + */ + url: string + } + /** * Parse mode to use when parsing inline message text. * Defaults to current default parse mode (if any). @@ -93,7 +109,7 @@ export async function answerInlineQuery( parseMode?: string | null }, ): Promise { - const { cacheTime = 300, gallery, private: priv, nextOffset, switchPm, parseMode } = params ?? {} + const { cacheTime = 300, gallery, private: priv, nextOffset, switchPm, switchWebview, parseMode } = params ?? {} const [defaultGallery, tlResults] = await BotInline._convertToTl(client, results, parseMode) @@ -112,5 +128,12 @@ export async function answerInlineQuery( startParam: switchPm.parameter, } : undefined, + switchWebview: switchWebview ? + { + _: 'inlineBotWebView', + text: switchWebview.text, + url: switchWebview.url, + } : + undefined, }) } diff --git a/packages/client/src/methods/bots/get-callback-answer.ts b/packages/client/src/methods/bots/get-callback-answer.ts index b8f3d8c8..0f0ba8a6 100644 --- a/packages/client/src/methods/bots/get-callback-answer.ts +++ b/packages/client/src/methods/bots/get-callback-answer.ts @@ -1,7 +1,7 @@ import { BaseTelegramClient, tl } from '@mtcute/core' import { computeSrpParams, utf8EncodeToBuffer } from '@mtcute/core/utils.js' -import { InputPeerLike } from '../../types/index.js' +import { InputMessageId, normalizeInputMessageId } from '../../types/index.js' import { resolvePeer } from '../users/resolve-peer.js' /** @@ -12,13 +12,7 @@ import { resolvePeer } from '../users/resolve-peer.js' */ export async function getCallbackAnswer( client: BaseTelegramClient, - params: { - /** Chat ID where the message was found */ - chatId: InputPeerLike - - /** ID of the message containing the button */ - message: number - + params: InputMessageId & { /** Data contained in the button */ data: string | Uint8Array @@ -43,7 +37,8 @@ export async function getCallbackAnswer( password?: string }, ): Promise { - const { chatId, message, data, game, timeout = 10000 } = params + const { chatId, message } = normalizeInputMessageId(params) + const { data, game, timeout = 10000 } = params let password: tl.TypeInputCheckPasswordSRP | undefined = undefined diff --git a/packages/client/typedoc.cjs b/packages/client/typedoc.cjs index 9ffc29d1..0e7446f0 100644 --- a/packages/client/typedoc.cjs +++ b/packages/client/typedoc.cjs @@ -3,6 +3,7 @@ module.exports = { entryPoints: [ './src/index.ts', './src/utils/index.ts', + './src/methods/updates/index.ts', ], entryPointStrategy: 'expand', } diff --git a/packages/dispatcher/src/filters/bots.ts b/packages/dispatcher/src/filters/bots.ts index 16797756..4f79d219 100644 --- a/packages/dispatcher/src/filters/bots.ts +++ b/packages/dispatcher/src/filters/bots.ts @@ -98,49 +98,61 @@ export const start = and(chat('private'), command('start')) */ export const startGroup = and(or(chat('supergroup'), chat('group')), command('start')) +const deeplinkBase = + (base: UpdateFilter) => + (params: MaybeArray): UpdateFilter => { + if (!Array.isArray(params)) { + return and(start, (_msg: Message) => { + const msg = _msg as Message & { command: string[] } + + if (msg.command.length !== 2) return false + + const p = msg.command[1] + if (typeof params === 'string' && p === params) return true + + const m = p.match(params) + if (!m) return false + + msg.command.push(...m.slice(1)) + + return true + }) + } + + return and(base, (_msg: Message) => { + const msg = _msg as Message & { command: string[] } + + if (msg.command.length !== 2) return false + + const p = msg.command[1] + + for (const param of params) { + if (typeof param === 'string' && p === param) return true + + const m = p.match(param) + if (!m) continue + + msg.command.push(...m.slice(1)) + + return true + } + + return false + }) + } + /** * Filter for deep links (i.e. `/start `). * * If the parameter is a regex, groups are added to `msg.command`, * meaning that the first group is available in `msg.command[2]`. */ -export const deeplink = (params: MaybeArray): UpdateFilter => { - if (!Array.isArray(params)) { - return and(start, (_msg: Message) => { - const msg = _msg as Message & { command: string[] } +export const deeplink = deeplinkBase(start) - if (msg.command.length !== 2) return false - - const p = msg.command[1] - if (typeof params === 'string' && p === params) return true - - const m = p.match(params) - if (!m) return false - - msg.command.push(...m.slice(1)) - - return true - }) - } - - return and(start, (_msg: Message) => { - const msg = _msg as Message & { command: string[] } - - if (msg.command.length !== 2) return false - - const p = msg.command[1] - - for (const param of params) { - if (typeof param === 'string' && p === param) return true - - const m = p.match(param) - if (!m) continue - - msg.command.push(...m.slice(1)) - - return true - } - - return false - }) -} +/** + * Filter for group deep links (i.e. `/start `). + * + * If the parameter is a regex, groups are added to `msg.command`, + * meaning that the first group is available in `msg.command[2]`. + */ +export const deeplinkGroup = deeplinkBase(startGroup)