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
}
/**
* 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<tl.messages.TypeBotCallbackAnswer>
/**
* 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<tl.messages.TypeBotCallbackAnswer>
/**
* Get high scores of a game
* **Available**: 🤖 bots only

View file

@ -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<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)
@ -112,5 +128,12 @@ export async function answerInlineQuery(
startParam: switchPm.parameter,
} :
undefined,
switchWebview: switchWebview ?
{
_: 'inlineBotWebView',
text: switchWebview.text,
url: switchWebview.url,
} :
undefined,
})
}

View file

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

View file

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

View file

@ -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<MessageContext, { command: string[] }>) =>
(params: MaybeArray<string | RegExp>): UpdateFilter<MessageContext, { command: string[] }> => {
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 <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 = (params: MaybeArray<string | RegExp>): UpdateFilter<MessageContext, { command: string[] }> => {
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 <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)