diff --git a/packages/client/scripts/update-types.txt b/packages/client/scripts/update-types.txt index 8af16a9d..f6e58cdb 100644 --- a/packages/client/scripts/update-types.txt +++ b/packages/client/scripts/update-types.txt @@ -14,3 +14,4 @@ history_read = HistoryReadUpdate bot_stopped = BotStoppedUpdate bot_chat_join_request = BotChatJoinRequestUpdate chat_join_request = ChatJoinRequestUpdate +pre_checkout_query = PreCheckoutQuery diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index e331c3f8..03437e87 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -43,6 +43,7 @@ import { Poll, PollUpdate, PollVoteUpdate, + PreCheckoutQuery, RawDocument, ReplyMarkup, SentCode, @@ -83,6 +84,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 { answerPreCheckoutQuery } from './methods/bots/answer-pre-checkout-query' import { deleteMyCommands } from './methods/bots/delete-my-commands' import { getBotMenuButton } from './methods/bots/get-bot-menu-button' import { getCallbackAnswer } from './methods/bots/get-callback-answer' @@ -412,6 +414,16 @@ export interface TelegramClient extends BaseTelegramClient { name: 'chat_join_request', handler: (upd: ChatJoinRequestUpdate) => void ): this + /** + * Register a pre checkout query handler + * + * @param name Event name + * @param handler Pre checkout query handler + */ + on( + name: 'pre_checkout_query', + handler: (upd: PreCheckoutQuery) => void + ): this /** * Accept the given TOS * @@ -830,6 +842,13 @@ export interface TelegramClient extends BaseTelegramClient { parseMode?: string | null } ): Promise + /** + * Answer a pre-checkout query. + * + * @param queryId Pre-checkout query ID + * @param error If pre-checkout is rejected, error message to show to the user + */ + answerPreCheckoutQuery(queryId: tl.Long, error?: string): Promise /** * Delete commands for the current bot and the given scope. * @@ -4100,6 +4119,7 @@ export class TelegramClient extends BaseTelegramClient { start = start answerCallbackQuery = answerCallbackQuery answerInlineQuery = answerInlineQuery + answerPreCheckoutQuery = answerPreCheckoutQuery deleteMyCommands = deleteMyCommands getBotMenuButton = getBotMenuButton getCallbackAnswer = getCallbackAnswer diff --git a/packages/client/src/methods/_imports.ts b/packages/client/src/methods/_imports.ts index 25624d4b..2c6e42f7 100644 --- a/packages/client/src/methods/_imports.ts +++ b/packages/client/src/methods/_imports.ts @@ -57,7 +57,8 @@ import { ChatJoinRequestUpdate, PeerReaction, MessageReactions, - Sticker + Sticker, + PreCheckoutQuery } from '../types' // @copy diff --git a/packages/client/src/methods/bots/answer-pre-checkout-query.ts b/packages/client/src/methods/bots/answer-pre-checkout-query.ts new file mode 100644 index 00000000..25a495fc --- /dev/null +++ b/packages/client/src/methods/bots/answer-pre-checkout-query.ts @@ -0,0 +1,23 @@ +import { tl } from '@mtcute/tl' + +import { TelegramClient } from '../../client' + +/** + * Answer a pre-checkout query. + * + * @param queryId Pre-checkout query ID + * @param error If pre-checkout is rejected, error message to show to the user + * @internal + */ +export async function answerPreCheckoutQuery( + this: TelegramClient, + queryId: tl.Long, + error?: string +): Promise { + await this.call({ + _: 'messages.setBotPrecheckoutResults', + queryId, + success: !error, + error, + }) +} diff --git a/packages/client/src/types/updates/index.ts b/packages/client/src/types/updates/index.ts index 368f2480..b3d6eede 100644 --- a/packages/client/src/types/updates/index.ts +++ b/packages/client/src/types/updates/index.ts @@ -11,6 +11,7 @@ import { HistoryReadUpdate } from './history-read-update' import { BotStoppedUpdate } from './bot-stopped' import { BotChatJoinRequestUpdate } from './bot-chat-join-request' import { ChatJoinRequestUpdate } from './chat-join-request' +import { PreCheckoutQuery } from './pre-checkout-query' export { DeleteMessageUpdate, @@ -24,6 +25,7 @@ export { BotStoppedUpdate, BotChatJoinRequestUpdate, ChatJoinRequestUpdate, + PreCheckoutQuery, } // begin-codegen @@ -43,5 +45,6 @@ export type ParsedUpdate = | { name: 'bot_stopped'; data: BotStoppedUpdate } | { name: 'bot_chat_join_request'; data: BotChatJoinRequestUpdate } | { name: 'chat_join_request'; data: ChatJoinRequestUpdate } + | { name: 'pre_checkout_query'; data: PreCheckoutQuery } // end-codegen diff --git a/packages/client/src/types/updates/parse-update.ts b/packages/client/src/types/updates/parse-update.ts index 81b96576..97e3438b 100644 --- a/packages/client/src/types/updates/parse-update.ts +++ b/packages/client/src/types/updates/parse-update.ts @@ -18,6 +18,7 @@ import { PollVoteUpdate, UserStatusUpdate, UserTypingUpdate, + PreCheckoutQuery } from '../index' type ParserFunction = ( @@ -123,6 +124,10 @@ const PARSERS: Partial< (client, upd, peers) => new ChatJoinRequestUpdate(client, upd as any, peers), ], + updateBotPrecheckoutQuery: [ + 'pre_checkout_query', + (client, upd, peers) => new PreCheckoutQuery(client, upd as any, peers), + ] } /** @internal */ diff --git a/packages/client/src/types/updates/pre-checkout-query.ts b/packages/client/src/types/updates/pre-checkout-query.ts new file mode 100644 index 00000000..e087256f --- /dev/null +++ b/packages/client/src/types/updates/pre-checkout-query.ts @@ -0,0 +1,85 @@ +import { tl } from '@mtcute/tl' + +import { TelegramClient } from '../../client' +import { makeInspectable } from '../utils' +import { PeersIndex, User } from '../peers' + +export class PreCheckoutQuery { + constructor( + public readonly client: TelegramClient, + public readonly raw: tl.RawUpdateBotPrecheckoutQuery, + public readonly _peers: PeersIndex + ) {} + + /** + * ID of the query + */ + get queryId(): tl.Long { + return this.raw.queryId + } + + /** + * ID of the user who sent the query + */ + get userId(): number { + return this.raw.userId + } + + private _user?: User + /** + * User who sent the query + */ + get user(): User { + if (!this._user) { + this._user = new User(this.client, this._peers.user(this.userId)) + } + + return this._user + } + + /** + * Bot-defined payload of the original invoice + * (see {@link InputMediaInvoice.payload}) + */ + get payload(): Buffer { + return this.raw.payload + } + + /** + * User-provided payment info (like name, phone, shipping address, etc.) + */ + get paymentInfo(): tl.RawPaymentRequestedInfo | null { + if (!this.raw.info) return null + return this.raw.info + } + + /** + * Currency of the payment + */ + get currency(): string { + return this.raw.currency + } + + /** + * Total price of the payment + */ + get totalAmount(): tl.Long { + return this.raw.totalAmount + } + + /** + * Approve the query + */ + approve(): Promise { + return this.client.answerPreCheckoutQuery(this.queryId) + } + + /** + * Reject the query + */ + reject(error = ''): Promise { + return this.client.answerPreCheckoutQuery(this.queryId, error) + } +} + +makeInspectable(PreCheckoutQuery) diff --git a/packages/dispatcher/src/dispatcher.ts b/packages/dispatcher/src/dispatcher.ts index 5a36213f..c33a0dc0 100644 --- a/packages/dispatcher/src/dispatcher.ts +++ b/packages/dispatcher/src/dispatcher.ts @@ -19,6 +19,7 @@ import { BotStoppedUpdate, BotChatJoinRequestUpdate, ChatJoinRequestUpdate, + PreCheckoutQuery, } from '@mtcute/client' import { tl } from '@mtcute/tl' @@ -41,6 +42,7 @@ import { BotStoppedHandler, BotChatJoinRequestHandler, ChatJoinRequestHandler, + PreCheckoutQueryHandler, } from './handler' // end-codegen-imports @@ -1554,5 +1556,36 @@ export class Dispatcher { this._addKnownHandler('chat_join_request', filter, handler, group) } + /** + * Register a pre checkout query handler without any filters + * + * @param handler Pre checkout query handler + * @param group Handler group index + */ + onPreCheckoutQuery( + handler: PreCheckoutQueryHandler['callback'], + group?: number + ): void + + /** + * Register a pre checkout query handler with a filter + * + * @param filter Update filter + * @param handler Pre checkout query handler + * @param group Handler group index + */ + onPreCheckoutQuery( + filter: UpdateFilter, + handler: PreCheckoutQueryHandler< + filters.Modify + >['callback'], + group?: number + ): void + + /** @internal */ + onPreCheckoutQuery(filter: any, handler?: any, group?: number): void { + this._addKnownHandler('pre_checkout_query', filter, handler, group) + } + // end-codegen } diff --git a/packages/dispatcher/src/handler.ts b/packages/dispatcher/src/handler.ts index 3287e9ea..8a2c48f1 100644 --- a/packages/dispatcher/src/handler.ts +++ b/packages/dispatcher/src/handler.ts @@ -16,6 +16,7 @@ import { BotStoppedUpdate, BotChatJoinRequestUpdate, ChatJoinRequestUpdate, + PreCheckoutQuery, } from '@mtcute/client' import { tl } from '@mtcute/tl' @@ -28,7 +29,11 @@ export interface BaseUpdateHandler { check?: Checker } -export type ParsedUpdateHandler = BaseUpdateHandler< +export type ParsedUpdateHandler< + Name, + Update, + State = never +> = BaseUpdateHandler< Name, (update: Update, state: State) => MaybeAsync, (update: Update, state: State) => MaybeAsync @@ -102,6 +107,10 @@ export type BotChatJoinRequestHandler = ParsedUpdateHandler<'bot_chat_join_request', T> export type ChatJoinRequestHandler = ParsedUpdateHandler<'chat_join_request', T> +export type PreCheckoutQueryHandler = ParsedUpdateHandler< + 'pre_checkout_query', + T +> export type UpdateHandler = | RawUpdateHandler @@ -120,5 +129,6 @@ export type UpdateHandler = | BotStoppedHandler | BotChatJoinRequestHandler | ChatJoinRequestHandler + | PreCheckoutQueryHandler // end-codegen