feat(core): support business callback queries
This commit is contained in:
parent
80097fa8be
commit
ecad89b70d
13 changed files with 194 additions and 20 deletions
|
@ -8,6 +8,7 @@ inline_query = InlineQuery in InlineQueryContext
|
||||||
chosen_inline_result = ChosenInlineResult in ChosenInlineResultContext
|
chosen_inline_result = ChosenInlineResult in ChosenInlineResultContext
|
||||||
callback_query = CallbackQuery + State in CallbackQueryContext
|
callback_query = CallbackQuery + State in CallbackQueryContext
|
||||||
inline_callback_query = InlineCallbackQuery + State in InlineCallbackQueryContext
|
inline_callback_query = InlineCallbackQuery + State in InlineCallbackQueryContext
|
||||||
|
business_callback_query = BusinessCallbackQuery + State in BusinessCallbackQueryContext
|
||||||
poll: PollUpdate = PollUpdate
|
poll: PollUpdate = PollUpdate
|
||||||
poll_vote = PollVoteUpdate
|
poll_vote = PollVoteUpdate
|
||||||
user_status: UserStatusUpdate = UserStatusUpdate
|
user_status: UserStatusUpdate = UserStatusUpdate
|
||||||
|
|
|
@ -59,9 +59,12 @@ export function _findMessageInUpdate(
|
||||||
if (isEdit) {
|
if (isEdit) {
|
||||||
if (
|
if (
|
||||||
!(
|
!(
|
||||||
|
(
|
||||||
u._ === 'updateEditMessage' ||
|
u._ === 'updateEditMessage' ||
|
||||||
u._ === 'updateEditChannelMessage' ||
|
u._ === 'updateEditChannelMessage' ||
|
||||||
u._ === 'updateBotEditBusinessMessage'
|
u._ === 'updateBotEditBusinessMessage' ||
|
||||||
|
u._ === 'updateBotNewBusinessMessage'
|
||||||
|
) // for whatever reason
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { MtArgumentError } from '../../../types/errors.js'
|
||||||
import { makeInspectable } from '../../utils/index.js'
|
import { makeInspectable } from '../../utils/index.js'
|
||||||
import { encodeInlineMessageId } from '../../utils/inline-utils.js'
|
import { encodeInlineMessageId } from '../../utils/inline-utils.js'
|
||||||
import { memoizeGetters } from '../../utils/memoize.js'
|
import { memoizeGetters } from '../../utils/memoize.js'
|
||||||
|
import { Message } from '../messages/message.js'
|
||||||
import { Chat } from '../peers/chat.js'
|
import { Chat } from '../peers/chat.js'
|
||||||
import { PeersIndex } from '../peers/peers-index.js'
|
import { PeersIndex } from '../peers/peers-index.js'
|
||||||
import { User } from '../peers/user.js'
|
import { User } from '../peers/user.js'
|
||||||
|
@ -12,7 +13,10 @@ import { User } from '../peers/user.js'
|
||||||
/** Base class for callback queries */
|
/** Base class for callback queries */
|
||||||
class BaseCallbackQuery {
|
class BaseCallbackQuery {
|
||||||
constructor(
|
constructor(
|
||||||
readonly raw: tl.RawUpdateBotCallbackQuery | tl.RawUpdateInlineBotCallbackQuery,
|
readonly raw:
|
||||||
|
| tl.RawUpdateBotCallbackQuery
|
||||||
|
| tl.RawUpdateInlineBotCallbackQuery
|
||||||
|
| tl.RawUpdateBusinessBotCallbackQuery,
|
||||||
readonly _peers: PeersIndex,
|
readonly _peers: PeersIndex,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
@ -67,6 +71,8 @@ class BaseCallbackQuery {
|
||||||
* short name of the game that should be returned.
|
* short name of the game that should be returned.
|
||||||
*/
|
*/
|
||||||
get game(): string | null {
|
get game(): string | null {
|
||||||
|
if (this.raw._ === 'updateBusinessBotCallbackQuery') return null
|
||||||
|
|
||||||
return this.raw.gameShortName ?? null
|
return this.raw.gameShortName ?? null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,3 +144,35 @@ export class InlineCallbackQuery extends BaseCallbackQuery {
|
||||||
|
|
||||||
memoizeGetters(InlineCallbackQuery, ['user', 'dataStr', 'inlineMessageIdStr'])
|
memoizeGetters(InlineCallbackQuery, ['user', 'dataStr', 'inlineMessageIdStr'])
|
||||||
makeInspectable(InlineCallbackQuery)
|
makeInspectable(InlineCallbackQuery)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A callback query originating from a message sent by the bot via a business connection
|
||||||
|
*/
|
||||||
|
export class BusinessCallbackQuery extends BaseCallbackQuery {
|
||||||
|
constructor(
|
||||||
|
readonly raw: tl.RawUpdateBusinessBotCallbackQuery,
|
||||||
|
_peers: PeersIndex,
|
||||||
|
) {
|
||||||
|
super(raw, _peers)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** ID of the business connection */
|
||||||
|
get connectionId(): string {
|
||||||
|
return this.raw.connectionId
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Message containing the button */
|
||||||
|
get message(): Message {
|
||||||
|
return new Message(this.raw.message, this._peers)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Message that {@link message} is a reply to (if any) */
|
||||||
|
get replyToMessage(): Message | null {
|
||||||
|
if (!this.raw.replyToMessage) return null
|
||||||
|
|
||||||
|
return new Message(this.raw.replyToMessage, this._peers)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
memoizeGetters(BusinessCallbackQuery, ['user', 'dataStr', 'message', 'replyToMessage'])
|
||||||
|
makeInspectable(BusinessCallbackQuery)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import type { BusinessConnection, Message } from '../../types/index.js'
|
import type { BusinessConnection, Message } from '../../types/index.js'
|
||||||
import { BotChatJoinRequestUpdate } from './bot-chat-join-request.js'
|
import { BotChatJoinRequestUpdate } from './bot-chat-join-request.js'
|
||||||
import { BotStoppedUpdate } from './bot-stopped.js'
|
import { BotStoppedUpdate } from './bot-stopped.js'
|
||||||
import { CallbackQuery, InlineCallbackQuery } from './callback-query.js'
|
import { BusinessCallbackQuery, CallbackQuery, InlineCallbackQuery } from './callback-query.js'
|
||||||
import { ChatJoinRequestUpdate } from './chat-join-request.js'
|
import { ChatJoinRequestUpdate } from './chat-join-request.js'
|
||||||
import { ChatMemberUpdate } from './chat-member-update.js'
|
import { ChatMemberUpdate } from './chat-member-update.js'
|
||||||
import { InlineQuery } from './inline-query.js'
|
import { InlineQuery } from './inline-query.js'
|
||||||
|
@ -25,6 +25,7 @@ export {
|
||||||
BotReactionCountUpdate,
|
BotReactionCountUpdate,
|
||||||
BotReactionUpdate,
|
BotReactionUpdate,
|
||||||
BotStoppedUpdate,
|
BotStoppedUpdate,
|
||||||
|
BusinessCallbackQuery,
|
||||||
BusinessMessage,
|
BusinessMessage,
|
||||||
CallbackQuery,
|
CallbackQuery,
|
||||||
ChatJoinRequestUpdate,
|
ChatJoinRequestUpdate,
|
||||||
|
@ -55,6 +56,7 @@ export type ParsedUpdate =
|
||||||
| { name: 'chosen_inline_result'; data: ChosenInlineResult }
|
| { name: 'chosen_inline_result'; data: ChosenInlineResult }
|
||||||
| { name: 'callback_query'; data: CallbackQuery }
|
| { name: 'callback_query'; data: CallbackQuery }
|
||||||
| { name: 'inline_callback_query'; data: InlineCallbackQuery }
|
| { name: 'inline_callback_query'; data: InlineCallbackQuery }
|
||||||
|
| { name: 'business_callback_query'; data: BusinessCallbackQuery }
|
||||||
| { name: 'poll'; data: PollUpdate }
|
| { name: 'poll'; data: PollUpdate }
|
||||||
| { name: 'poll_vote'; data: PollVoteUpdate }
|
| { name: 'poll_vote'; data: PollVoteUpdate }
|
||||||
| { name: 'user_status'; data: UserStatusUpdate }
|
| { name: 'user_status'; data: UserStatusUpdate }
|
||||||
|
|
|
@ -6,6 +6,7 @@ import {
|
||||||
BotReactionCountUpdate,
|
BotReactionCountUpdate,
|
||||||
BotReactionUpdate,
|
BotReactionUpdate,
|
||||||
BotStoppedUpdate,
|
BotStoppedUpdate,
|
||||||
|
BusinessCallbackQuery,
|
||||||
BusinessConnection,
|
BusinessConnection,
|
||||||
BusinessMessage,
|
BusinessMessage,
|
||||||
CallbackQuery,
|
CallbackQuery,
|
||||||
|
@ -105,6 +106,8 @@ export function _parseUpdate(update: tl.TypeUpdate, peers: PeersIndex): ParsedUp
|
||||||
return { name: 'edit_business_message', data: new BusinessMessage(update, peers) }
|
return { name: 'edit_business_message', data: new BusinessMessage(update, peers) }
|
||||||
case 'updateBotDeleteBusinessMessage':
|
case 'updateBotDeleteBusinessMessage':
|
||||||
return { name: 'delete_business_message', data: new DeleteBusinessMessageUpdate(update, peers) }
|
return { name: 'delete_business_message', data: new DeleteBusinessMessageUpdate(update, peers) }
|
||||||
|
case 'updateBusinessBotCallbackQuery':
|
||||||
|
return { name: 'business_callback_query', data: new BusinessCallbackQuery(update, peers) }
|
||||||
default:
|
default:
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { CallbackQuery, InlineCallbackQuery, MaybePromise, Message } from '@mtcute/core'
|
import { BusinessCallbackQuery, CallbackQuery, InlineCallbackQuery, MaybePromise, Message } from '@mtcute/core'
|
||||||
import { TelegramClient } from '@mtcute/core/client.js'
|
import { TelegramClient } from '@mtcute/core/client.js'
|
||||||
|
|
||||||
import { UpdateContext } from './base.js'
|
import { UpdateContext } from './base.js'
|
||||||
|
@ -88,3 +88,37 @@ export class InlineCallbackQueryContext extends InlineCallbackQuery implements U
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Context of an callback query update originated from a business connection message
|
||||||
|
*
|
||||||
|
* This is a subclass of {@link BusinessCallbackQuery}, so all its fields are also available.
|
||||||
|
*/
|
||||||
|
export class BusinessCallbackQueryContext
|
||||||
|
extends BusinessCallbackQuery
|
||||||
|
implements UpdateContext<BusinessCallbackQuery> {
|
||||||
|
readonly _name = 'business_callback_query'
|
||||||
|
|
||||||
|
constructor(
|
||||||
|
readonly client: TelegramClient,
|
||||||
|
query: BusinessCallbackQuery,
|
||||||
|
) {
|
||||||
|
super(query.raw, query._peers)
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Answer to this callback query */
|
||||||
|
answer(params: Parameters<TelegramClient['answerCallbackQuery']>[1]) {
|
||||||
|
return this.client.answerCallbackQuery(this.id, params)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Edit the message that contained the callback button that was clicked.
|
||||||
|
*/
|
||||||
|
async editMessage(params: Omit<Parameters<TelegramClient['editInlineMessage']>[0], 'messageId'>) {
|
||||||
|
return this.client.editMessage({
|
||||||
|
message: this.message,
|
||||||
|
businessConnectionId: this.connectionId,
|
||||||
|
...params,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
export * from './base.js'
|
export * from './base.js'
|
||||||
|
export * from './business-message.js'
|
||||||
export * from './callback-query.js'
|
export * from './callback-query.js'
|
||||||
export * from './chat-join-request.js'
|
export * from './chat-join-request.js'
|
||||||
export * from './chosen-inline-result.js'
|
export * from './chosen-inline-result.js'
|
||||||
|
|
|
@ -3,7 +3,7 @@ import { TelegramClient } from '@mtcute/core/client.js'
|
||||||
|
|
||||||
import { UpdateContextDistributed } from './base.js'
|
import { UpdateContextDistributed } from './base.js'
|
||||||
import { BusinessMessageContext } from './business-message.js'
|
import { BusinessMessageContext } from './business-message.js'
|
||||||
import { CallbackQueryContext, InlineCallbackQueryContext } from './callback-query.js'
|
import { BusinessCallbackQueryContext, CallbackQueryContext, InlineCallbackQueryContext } from './callback-query.js'
|
||||||
import { ChatJoinRequestUpdateContext } from './chat-join-request.js'
|
import { ChatJoinRequestUpdateContext } from './chat-join-request.js'
|
||||||
import { ChosenInlineResultContext } from './chosen-inline-result.js'
|
import { ChosenInlineResultContext } from './chosen-inline-result.js'
|
||||||
import { InlineQueryContext } from './inline-query.js'
|
import { InlineQueryContext } from './inline-query.js'
|
||||||
|
@ -25,6 +25,8 @@ export function _parsedUpdateToContext(client: TelegramClient, update: ParsedUpd
|
||||||
return new CallbackQueryContext(client, update.data)
|
return new CallbackQueryContext(client, update.data)
|
||||||
case 'inline_callback_query':
|
case 'inline_callback_query':
|
||||||
return new InlineCallbackQueryContext(client, update.data)
|
return new InlineCallbackQueryContext(client, update.data)
|
||||||
|
case 'business_callback_query':
|
||||||
|
return new BusinessCallbackQueryContext(client, update.data)
|
||||||
case 'bot_chat_join_request':
|
case 'bot_chat_join_request':
|
||||||
return new ChatJoinRequestUpdateContext(client, update.data)
|
return new ChatJoinRequestUpdateContext(client, update.data)
|
||||||
case 'pre_checkout_query':
|
case 'pre_checkout_query':
|
||||||
|
|
|
@ -30,6 +30,7 @@ import { TelegramClient } from '@mtcute/core/client.js'
|
||||||
import { UpdateContext } from './context/base.js'
|
import { UpdateContext } from './context/base.js'
|
||||||
import { BusinessMessageContext } from './context/business-message.js'
|
import { BusinessMessageContext } from './context/business-message.js'
|
||||||
import {
|
import {
|
||||||
|
BusinessCallbackQueryContext,
|
||||||
CallbackQueryContext,
|
CallbackQueryContext,
|
||||||
ChatJoinRequestUpdateContext,
|
ChatJoinRequestUpdateContext,
|
||||||
ChosenInlineResultContext,
|
ChosenInlineResultContext,
|
||||||
|
@ -47,6 +48,7 @@ import {
|
||||||
BotReactionCountUpdateHandler,
|
BotReactionCountUpdateHandler,
|
||||||
BotReactionUpdateHandler,
|
BotReactionUpdateHandler,
|
||||||
BotStoppedHandler,
|
BotStoppedHandler,
|
||||||
|
BusinessCallbackQueryHandler,
|
||||||
BusinessConnectionUpdateHandler,
|
BusinessConnectionUpdateHandler,
|
||||||
BusinessMessageGroupHandler,
|
BusinessMessageGroupHandler,
|
||||||
CallbackQueryHandler,
|
CallbackQueryHandler,
|
||||||
|
@ -1141,7 +1143,7 @@ export class Dispatcher<State extends object = never> {
|
||||||
*/
|
*/
|
||||||
onAnyCallbackQuery(
|
onAnyCallbackQuery(
|
||||||
handler: CallbackQueryHandler<
|
handler: CallbackQueryHandler<
|
||||||
CallbackQueryContext | InlineCallbackQueryContext,
|
CallbackQueryContext | InlineCallbackQueryContext | BusinessCallbackQueryContext,
|
||||||
State extends never ? never : UpdateState<State>
|
State extends never ? never : UpdateState<State>
|
||||||
>['callback'],
|
>['callback'],
|
||||||
group?: number,
|
group?: number,
|
||||||
|
@ -1155,9 +1157,13 @@ export class Dispatcher<State extends object = never> {
|
||||||
* @param group Handler group index
|
* @param group Handler group index
|
||||||
*/
|
*/
|
||||||
onAnyCallbackQuery<Mod>(
|
onAnyCallbackQuery<Mod>(
|
||||||
filter: UpdateFilter<CallbackQueryContext | InlineCallbackQueryContext, Mod, State>,
|
filter: UpdateFilter<
|
||||||
|
CallbackQueryContext | InlineCallbackQueryContext | BusinessCallbackQueryContext,
|
||||||
|
Mod,
|
||||||
|
State
|
||||||
|
>,
|
||||||
handler: CallbackQueryHandler<
|
handler: CallbackQueryHandler<
|
||||||
filters.Modify<CallbackQueryContext | InlineCallbackQueryContext, Mod>,
|
filters.Modify<CallbackQueryContext | InlineCallbackQueryContext | BusinessCallbackQueryContext, Mod>,
|
||||||
State extends never ? never : UpdateState<State>
|
State extends never ? never : UpdateState<State>
|
||||||
>['callback'],
|
>['callback'],
|
||||||
group?: number,
|
group?: number,
|
||||||
|
@ -1171,9 +1177,9 @@ export class Dispatcher<State extends object = never> {
|
||||||
* @param group Handler group index
|
* @param group Handler group index
|
||||||
*/
|
*/
|
||||||
onAnyCallbackQuery<Mod>(
|
onAnyCallbackQuery<Mod>(
|
||||||
filter: UpdateFilter<CallbackQueryContext | InlineCallbackQueryContext, Mod>,
|
filter: UpdateFilter<CallbackQueryContext | InlineCallbackQueryContext | BusinessCallbackQueryContext, Mod>,
|
||||||
handler: CallbackQueryHandler<
|
handler: CallbackQueryHandler<
|
||||||
filters.Modify<CallbackQueryContext | InlineCallbackQueryContext, Mod>,
|
filters.Modify<CallbackQueryContext | InlineCallbackQueryContext | BusinessCallbackQueryContext, Mod>,
|
||||||
State extends never ? never : UpdateState<State>
|
State extends never ? never : UpdateState<State>
|
||||||
>['callback'],
|
>['callback'],
|
||||||
group?: number,
|
group?: number,
|
||||||
|
@ -1183,6 +1189,7 @@ export class Dispatcher<State extends object = never> {
|
||||||
onAnyCallbackQuery(filter: any, handler?: any, group?: number): void {
|
onAnyCallbackQuery(filter: any, handler?: any, group?: number): void {
|
||||||
this._addKnownHandler('callback_query', filter, handler, group)
|
this._addKnownHandler('callback_query', filter, handler, group)
|
||||||
this._addKnownHandler('inline_callback_query', filter, handler, group)
|
this._addKnownHandler('inline_callback_query', filter, handler, group)
|
||||||
|
this._addKnownHandler('business_callback_query', filter, handler, group)
|
||||||
}
|
}
|
||||||
|
|
||||||
// begin-codegen
|
// begin-codegen
|
||||||
|
@ -1537,6 +1544,57 @@ export class Dispatcher<State extends object = never> {
|
||||||
this._addKnownHandler('inline_callback_query', filter, handler, group)
|
this._addKnownHandler('inline_callback_query', filter, handler, group)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a business callback query handler without any filters
|
||||||
|
*
|
||||||
|
* @param handler Business callback query handler
|
||||||
|
* @param group Handler group index
|
||||||
|
*/
|
||||||
|
onBusinessCallbackQuery(
|
||||||
|
handler: BusinessCallbackQueryHandler<
|
||||||
|
BusinessCallbackQueryContext,
|
||||||
|
State extends never ? never : UpdateState<State>
|
||||||
|
>['callback'],
|
||||||
|
group?: number,
|
||||||
|
): void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a business callback query handler with a filter
|
||||||
|
*
|
||||||
|
* @param filter Update filter
|
||||||
|
* @param handler Business callback query handler
|
||||||
|
* @param group Handler group index
|
||||||
|
*/
|
||||||
|
onBusinessCallbackQuery<Mod>(
|
||||||
|
filter: UpdateFilter<BusinessCallbackQueryContext, Mod, State>,
|
||||||
|
handler: BusinessCallbackQueryHandler<
|
||||||
|
filters.Modify<BusinessCallbackQueryContext, Mod>,
|
||||||
|
State extends never ? never : UpdateState<State>
|
||||||
|
>['callback'],
|
||||||
|
group?: number,
|
||||||
|
): void
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register a business callback query handler with a filter
|
||||||
|
*
|
||||||
|
* @param filter Update filter
|
||||||
|
* @param handler Business callback query handler
|
||||||
|
* @param group Handler group index
|
||||||
|
*/
|
||||||
|
onBusinessCallbackQuery<Mod>(
|
||||||
|
filter: UpdateFilter<BusinessCallbackQueryContext, Mod>,
|
||||||
|
handler: BusinessCallbackQueryHandler<
|
||||||
|
filters.Modify<BusinessCallbackQueryContext, Mod>,
|
||||||
|
State extends never ? never : UpdateState<State>
|
||||||
|
>['callback'],
|
||||||
|
group?: number,
|
||||||
|
): void
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
onBusinessCallbackQuery(filter: any, handler?: any, group?: number): void {
|
||||||
|
this._addKnownHandler('business_callback_query', filter, handler, group)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a poll update handler without any filters
|
* Register a poll update handler without any filters
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import {
|
import {
|
||||||
BotChatJoinRequestUpdate,
|
BotChatJoinRequestUpdate,
|
||||||
|
BusinessMessage,
|
||||||
Chat,
|
Chat,
|
||||||
ChatMemberUpdate,
|
ChatMemberUpdate,
|
||||||
ChatType,
|
ChatType,
|
||||||
|
@ -48,12 +49,14 @@ export const chat =
|
||||||
export const chatId: {
|
export const chatId: {
|
||||||
(id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<
|
(id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<
|
||||||
| Message
|
| Message
|
||||||
|
| BusinessMessage
|
||||||
| ChatMemberUpdate
|
| ChatMemberUpdate
|
||||||
| PollVoteUpdate
|
| PollVoteUpdate
|
||||||
| BotChatJoinRequestUpdate
|
| BotChatJoinRequestUpdate
|
||||||
>>
|
>>
|
||||||
(id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<
|
(id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<
|
||||||
| Message
|
| Message
|
||||||
|
| BusinessMessage
|
||||||
| ChatMemberUpdate
|
| ChatMemberUpdate
|
||||||
| UserTypingUpdate
|
| UserTypingUpdate
|
||||||
| HistoryReadUpdate
|
| HistoryReadUpdate
|
||||||
|
|
|
@ -1,15 +1,30 @@
|
||||||
import { CallbackQuery, ChosenInlineResult, InlineCallbackQuery, InlineQuery, Message } from '@mtcute/core'
|
import {
|
||||||
|
BusinessCallbackQuery,
|
||||||
|
BusinessMessage,
|
||||||
|
CallbackQuery,
|
||||||
|
ChosenInlineResult,
|
||||||
|
InlineCallbackQuery,
|
||||||
|
InlineQuery,
|
||||||
|
Message,
|
||||||
|
} from '@mtcute/core'
|
||||||
|
|
||||||
import { UpdateContextDistributed } from '../context/base.js'
|
import { UpdateContextDistributed } from '../context/base.js'
|
||||||
import { UpdateFilter } from './types.js'
|
import { UpdateFilter } from './types.js'
|
||||||
|
|
||||||
type UpdatesWithText = UpdateContextDistributed<
|
type UpdatesWithText = UpdateContextDistributed<
|
||||||
Message | InlineQuery | ChosenInlineResult | CallbackQuery | InlineCallbackQuery
|
| Message
|
||||||
|
| BusinessMessage
|
||||||
|
| InlineQuery
|
||||||
|
| ChosenInlineResult
|
||||||
|
| CallbackQuery
|
||||||
|
| InlineCallbackQuery
|
||||||
|
| BusinessCallbackQuery
|
||||||
>
|
>
|
||||||
|
|
||||||
function extractText(obj: UpdatesWithText): string | null {
|
function extractText(obj: UpdatesWithText): string | null {
|
||||||
switch (obj._name) {
|
switch (obj._name) {
|
||||||
case 'new_message':
|
case 'new_message':
|
||||||
|
case 'new_business_message':
|
||||||
return obj.text
|
return obj.text
|
||||||
case 'inline_query':
|
case 'inline_query':
|
||||||
return obj.query
|
return obj.query
|
||||||
|
@ -17,6 +32,7 @@ function extractText(obj: UpdatesWithText): string | null {
|
||||||
return obj.id
|
return obj.id
|
||||||
case 'callback_query':
|
case 'callback_query':
|
||||||
case 'inline_callback_query':
|
case 'inline_callback_query':
|
||||||
|
case 'business_callback_query':
|
||||||
if (obj.raw.data) return obj.dataStr
|
if (obj.raw.data) return obj.dataStr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,7 +44,7 @@ function extractText(obj: UpdatesWithText): string | null {
|
||||||
* - for `Message`, `Message.text` is used
|
* - for `Message`, `Message.text` is used
|
||||||
* - for `InlineQuery`, `InlineQuery.query` is used
|
* - for `InlineQuery`, `InlineQuery.query` is used
|
||||||
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used
|
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult#id} is used
|
||||||
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
* - for callback queries, `dataStr` is used
|
||||||
*
|
*
|
||||||
* When a regex matches, the match array is stored in a
|
* When a regex matches, the match array is stored in a
|
||||||
* type-safe extension field `.match` of the object
|
* type-safe extension field `.match` of the object
|
||||||
|
@ -57,7 +73,7 @@ export const regex =
|
||||||
* - for `Message`, `Message.text` is used
|
* - for `Message`, `Message.text` is used
|
||||||
* - for `InlineQuery`, `InlineQuery.query` is used
|
* - for `InlineQuery`, `InlineQuery.query` is used
|
||||||
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
||||||
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
* - for callback queries, `dataStr` is used
|
||||||
*
|
*
|
||||||
* @param str String to be matched
|
* @param str String to be matched
|
||||||
* @param ignoreCase Whether string case should be ignored
|
* @param ignoreCase Whether string case should be ignored
|
||||||
|
@ -77,7 +93,7 @@ export const equals = (str: string, ignoreCase = false): UpdateFilter<UpdatesWit
|
||||||
* - for `Message`, `Message.text` is used
|
* - for `Message`, `Message.text` is used
|
||||||
* - for `InlineQuery`, `InlineQuery.query` is used
|
* - for `InlineQuery`, `InlineQuery.query` is used
|
||||||
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
||||||
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
* - for callback queries, `dataStr` is used
|
||||||
*
|
*
|
||||||
* @param str Substring to be matched
|
* @param str Substring to be matched
|
||||||
* @param ignoreCase Whether string case should be ignored
|
* @param ignoreCase Whether string case should be ignored
|
||||||
|
@ -105,7 +121,7 @@ export const contains = (str: string, ignoreCase = false): UpdateFilter<UpdatesW
|
||||||
* - for `Message`, `Message.text` is used
|
* - for `Message`, `Message.text` is used
|
||||||
* - for `InlineQuery`, `InlineQuery.query` is used
|
* - for `InlineQuery`, `InlineQuery.query` is used
|
||||||
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
||||||
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
* - for callback queries, `dataStr` is used
|
||||||
*
|
*
|
||||||
* @param str Substring to be matched
|
* @param str Substring to be matched
|
||||||
* @param ignoreCase Whether string case should be ignored
|
* @param ignoreCase Whether string case should be ignored
|
||||||
|
@ -133,7 +149,7 @@ export const startsWith = (str: string, ignoreCase = false): UpdateFilter<Update
|
||||||
* - for `Message`, `Message.text` is used
|
* - for `Message`, `Message.text` is used
|
||||||
* - for `InlineQuery`, `InlineQuery.query` is used
|
* - for `InlineQuery`, `InlineQuery.query` is used
|
||||||
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
* - for {@link ChosenInlineResult}, {@link ChosenInlineResult.id} is used
|
||||||
* - for `CallbackQuery`, `CallbackQuery.dataStr` is used
|
* - for callback queries, `dataStr` is used
|
||||||
*
|
*
|
||||||
* @param str Substring to be matched
|
* @param str Substring to be matched
|
||||||
* @param ignoreCase Whether string case should be ignored
|
* @param ignoreCase Whether string case should be ignored
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import {
|
import {
|
||||||
BotChatJoinRequestUpdate,
|
BotChatJoinRequestUpdate,
|
||||||
|
BusinessCallbackQuery,
|
||||||
|
BusinessMessage,
|
||||||
CallbackQuery,
|
CallbackQuery,
|
||||||
ChatMemberUpdate,
|
ChatMemberUpdate,
|
||||||
ChosenInlineResult,
|
ChosenInlineResult,
|
||||||
|
@ -39,6 +41,7 @@ export const bot: UpdateFilter<Message, { sender: User }> = (msg) => msg.sender.
|
||||||
export const userId: {
|
export const userId: {
|
||||||
(id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<
|
(id: MaybeArray<number>): UpdateFilter<UpdateContextDistributed<
|
||||||
| Message
|
| Message
|
||||||
|
| BusinessMessage
|
||||||
| StoryUpdate
|
| StoryUpdate
|
||||||
| DeleteStoryUpdate
|
| DeleteStoryUpdate
|
||||||
| InlineQuery
|
| InlineQuery
|
||||||
|
@ -46,11 +49,13 @@ export const userId: {
|
||||||
| ChosenInlineResult
|
| ChosenInlineResult
|
||||||
| CallbackQuery
|
| CallbackQuery
|
||||||
| InlineCallbackQuery
|
| InlineCallbackQuery
|
||||||
|
| BusinessCallbackQuery
|
||||||
| PollVoteUpdate
|
| PollVoteUpdate
|
||||||
| BotChatJoinRequestUpdate
|
| BotChatJoinRequestUpdate
|
||||||
>>
|
>>
|
||||||
(id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<
|
(id: MaybeArray<number | string>): UpdateFilter<UpdateContextDistributed<
|
||||||
| Message
|
| Message
|
||||||
|
| BusinessMessage
|
||||||
| UserStatusUpdate
|
| UserStatusUpdate
|
||||||
| UserTypingUpdate
|
| UserTypingUpdate
|
||||||
| StoryUpdate
|
| StoryUpdate
|
||||||
|
@ -61,6 +66,7 @@ export const userId: {
|
||||||
| ChosenInlineResult
|
| ChosenInlineResult
|
||||||
| CallbackQuery
|
| CallbackQuery
|
||||||
| InlineCallbackQuery
|
| InlineCallbackQuery
|
||||||
|
| BusinessCallbackQuery
|
||||||
| PollVoteUpdate
|
| PollVoteUpdate
|
||||||
| BotChatJoinRequestUpdate
|
| BotChatJoinRequestUpdate
|
||||||
>>
|
>>
|
||||||
|
|
|
@ -21,8 +21,9 @@ import {
|
||||||
import { TelegramClient } from '@mtcute/core/client.js'
|
import { TelegramClient } from '@mtcute/core/client.js'
|
||||||
|
|
||||||
import { UpdateContext } from './context/base.js'
|
import { UpdateContext } from './context/base.js'
|
||||||
import { BusinessMessageContext } from './context/business-message.js'
|
|
||||||
import {
|
import {
|
||||||
|
BusinessCallbackQueryContext,
|
||||||
|
BusinessMessageContext,
|
||||||
CallbackQueryContext,
|
CallbackQueryContext,
|
||||||
ChatJoinRequestUpdateContext,
|
ChatJoinRequestUpdateContext,
|
||||||
ChosenInlineResultContext,
|
ChosenInlineResultContext,
|
||||||
|
@ -70,6 +71,11 @@ export type InlineCallbackQueryHandler<T = InlineCallbackQueryContext, S = never
|
||||||
T,
|
T,
|
||||||
S
|
S
|
||||||
>
|
>
|
||||||
|
export type BusinessCallbackQueryHandler<T = BusinessCallbackQueryContext, S = never> = ParsedUpdateHandler<
|
||||||
|
'business_callback_query',
|
||||||
|
T,
|
||||||
|
S
|
||||||
|
>
|
||||||
export type PollUpdateHandler<T = UpdateContext<PollUpdate>> = ParsedUpdateHandler<'poll', T>
|
export type PollUpdateHandler<T = UpdateContext<PollUpdate>> = ParsedUpdateHandler<'poll', T>
|
||||||
export type PollVoteHandler<T = UpdateContext<PollVoteUpdate>> = ParsedUpdateHandler<'poll_vote', T>
|
export type PollVoteHandler<T = UpdateContext<PollVoteUpdate>> = ParsedUpdateHandler<'poll_vote', T>
|
||||||
export type UserStatusUpdateHandler<T = UpdateContext<UserStatusUpdate>> = ParsedUpdateHandler<'user_status', T>
|
export type UserStatusUpdateHandler<T = UpdateContext<UserStatusUpdate>> = ParsedUpdateHandler<'user_status', T>
|
||||||
|
@ -127,6 +133,7 @@ export type UpdateHandler =
|
||||||
| ChosenInlineResultHandler
|
| ChosenInlineResultHandler
|
||||||
| CallbackQueryHandler
|
| CallbackQueryHandler
|
||||||
| InlineCallbackQueryHandler
|
| InlineCallbackQueryHandler
|
||||||
|
| BusinessCallbackQueryHandler
|
||||||
| PollUpdateHandler
|
| PollUpdateHandler
|
||||||
| PollVoteHandler
|
| PollVoteHandler
|
||||||
| UserStatusUpdateHandler
|
| UserStatusUpdateHandler
|
||||||
|
|
Loading…
Reference in a new issue