fix: improved surface api

This commit is contained in:
alina 🌸 2023-10-29 00:48:37 +03:00
parent 04c702dfd2
commit fdec2b8621
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
5 changed files with 119 additions and 76 deletions

View file

@ -835,6 +835,22 @@ export interface TelegramClient extends BaseTelegramClient {
parameter: string 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. * Parse mode to use when parsing inline message text.
* Defaults to current default parse mode (if any). * Defaults to current default parse mode (if any).
@ -915,13 +931,8 @@ export interface TelegramClient extends BaseTelegramClient {
* *
* @param params * @param params
*/ */
getCallbackAnswer(params: { getCallbackAnswer(
/** Chat ID where the message was found */ params: InputMessageId & {
chatId: InputPeerLike
/** ID of the message containing the button */
message: number
/** Data contained in the button */ /** Data contained in the button */
data: string | Uint8Array data: string | Uint8Array
@ -944,7 +955,8 @@ export interface TelegramClient extends BaseTelegramClient {
* it is checked by Telegram. * it is checked by Telegram.
*/ */
password?: string password?: string
}): Promise<tl.messages.TypeBotCallbackAnswer> },
): Promise<tl.messages.TypeBotCallbackAnswer>
/** /**
* Get high scores of a game * Get high scores of a game
* **Available**: 🤖 bots only * **Available**: 🤖 bots only

View file

@ -81,6 +81,22 @@ export async function answerInlineQuery(
parameter: string 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. * Parse mode to use when parsing inline message text.
* Defaults to current default parse mode (if any). * Defaults to current default parse mode (if any).
@ -93,7 +109,7 @@ export async function answerInlineQuery(
parseMode?: string | null parseMode?: string | null
}, },
): Promise<void> { ): Promise<void> {
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) const [defaultGallery, tlResults] = await BotInline._convertToTl(client, results, parseMode)
@ -112,5 +128,12 @@ export async function answerInlineQuery(
startParam: switchPm.parameter, startParam: switchPm.parameter,
} : } :
undefined, undefined,
switchWebview: switchWebview ?
{
_: 'inlineBotWebView',
text: switchWebview.text,
url: switchWebview.url,
} :
undefined,
}) })
} }

View file

@ -1,7 +1,7 @@
import { BaseTelegramClient, tl } from '@mtcute/core' import { BaseTelegramClient, tl } from '@mtcute/core'
import { computeSrpParams, utf8EncodeToBuffer } from '@mtcute/core/utils.js' 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' import { resolvePeer } from '../users/resolve-peer.js'
/** /**
@ -12,13 +12,7 @@ import { resolvePeer } from '../users/resolve-peer.js'
*/ */
export async function getCallbackAnswer( export async function getCallbackAnswer(
client: BaseTelegramClient, client: BaseTelegramClient,
params: { params: InputMessageId & {
/** Chat ID where the message was found */
chatId: InputPeerLike
/** ID of the message containing the button */
message: number
/** Data contained in the button */ /** Data contained in the button */
data: string | Uint8Array data: string | Uint8Array
@ -43,7 +37,8 @@ export async function getCallbackAnswer(
password?: string password?: string
}, },
): Promise<tl.messages.TypeBotCallbackAnswer> { ): Promise<tl.messages.TypeBotCallbackAnswer> {
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 let password: tl.TypeInputCheckPasswordSRP | undefined = undefined

View file

@ -3,6 +3,7 @@ module.exports = {
entryPoints: [ entryPoints: [
'./src/index.ts', './src/index.ts',
'./src/utils/index.ts', './src/utils/index.ts',
'./src/methods/updates/index.ts',
], ],
entryPointStrategy: 'expand', entryPointStrategy: 'expand',
} }

View file

@ -98,13 +98,9 @@ export const start = and(chat('private'), command('start'))
*/ */
export const startGroup = and(or(chat('supergroup'), chat('group')), command('start')) export const startGroup = and(or(chat('supergroup'), chat('group')), command('start'))
/** const deeplinkBase =
* Filter for deep links (i.e. `/start <deeplink_parameter>`). (base: UpdateFilter<MessageContext, { command: string[] }>) =>
* (params: MaybeArray<string | RegExp>): UpdateFilter<MessageContext, { command: string[] }> => {
* 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<string | RegExp>): UpdateFilter<MessageContext, { command: string[] }> => {
if (!Array.isArray(params)) { if (!Array.isArray(params)) {
return and(start, (_msg: Message) => { return and(start, (_msg: Message) => {
const msg = _msg as Message & { command: string[] } const msg = _msg as Message & { command: string[] }
@ -123,7 +119,7 @@ export const deeplink = (params: MaybeArray<string | RegExp>): UpdateFilter<Mess
}) })
} }
return and(start, (_msg: Message) => { return and(base, (_msg: Message) => {
const msg = _msg as Message & { command: string[] } const msg = _msg as Message & { command: string[] }
if (msg.command.length !== 2) return false if (msg.command.length !== 2) return false
@ -143,4 +139,20 @@ export const deeplink = (params: MaybeArray<string | RegExp>): UpdateFilter<Mess
return false return false
}) })
} }
/**
* Filter for deep links (i.e. `/start <deeplink_parameter>`).
*
* 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 = deeplinkBase(start)
/**
* Filter for group deep links (i.e. `/start <deeplink_parameter>`).
*
* 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)