diff --git a/gramjs/events/InlineQuery.ts b/gramjs/events/InlineQuery.ts new file mode 100644 index 00000000..1639cba4 --- /dev/null +++ b/gramjs/events/InlineQuery.ts @@ -0,0 +1,198 @@ +import { TelegramClient } from "../client/TelegramClient"; +import { EntityLike } from "../define"; +import { returnBigInt } from "../Helpers"; +import { Api } from "../tl"; +import { _getEntityPair } from "../Utils"; +import { EventBuilder, EventCommonSender } from "./common"; + +export interface NewInlineQueryInterface { + chats: EntityLike[]; + func?: { (event: InlineQuery): boolean }; + fromUsers: EntityLike[]; + blacklistUsers: EntityLike[]; + pattern?: RegExp; +} + +export const NewInlineQueryDefaults: NewInlineQueryInterface = { + chats: [], + fromUsers: [], + blacklistUsers: [], +}; +/** + * Occurs whenever you sign in as a bot and an inline query is sent. + * + * @example + * ```ts + * async function printQuery(event: InlineQueryEvent) { + * console.log(event.query); + * } + * ``` + */ +export class InlineQuery extends EventBuilder { + match?: RegExp; + + private _noCheck: boolean; + + constructor(inlineQueryParams: Partial = {}) { + const { chats, func, pattern } = inlineQueryParams; + super({ chats, func, blacklistChats: false }); + + this.match = pattern; + + this._noCheck = [this.chats, this.func, this.match].every( + (i) => i === null || i === undefined + ); + } + + build( + update: Api.TypeUpdate | Api.TypeUpdates, + callback: undefined, + selfId = undefined + ) { + if (update instanceof Api.UpdateBotInlineQuery) { + return new InlineQueryEvent( + update, + new Api.PeerUser({ userId: returnBigInt(update.userId) }), + Number(update.queryId) + ); + } + } + + filter(event: InlineQueryEvent) { + if (this._noCheck) return event; + + if (this.chats) { + let inside; + if (event.chatId) { + inside = inside || this.chats.includes(event.chatId.toString()); + } + + if (inside === this.blacklistChats) { + return; + } + } + + if (this.match) { + const data = event.query; + const result = this.match.exec(data); + this.match.lastIndex = 0; + if (result) { + event.patternMatch = result; + } else { + return; + } + } + + if (this.func) { + return this.func(event); + } + + return true; + } +} + +export interface AnswerInlineQueryParams { + results: Api.TypeInputBotInlineResult[]; + cacheTime?: number; + gallery?: boolean; + isPrivate?: boolean; + switchPm?: Api.TypeInlineBotSwitchPM; + nextOffset?: string; + switchWebview?: Api.TypeInlineBotWebView; +} + +export class InlineQueryEvent extends EventCommonSender { + query: string; + queryId: bigInt.BigInteger; + + /** + * The regex match object returned from successfully matching the + * query `data` with the provided pattern in your event handler. + */ + patternMatch: RegExpMatchArray | undefined; + + private _message: Api.Message | undefined; + private _answered: boolean; + /** + * The original {@link Api.UpdateBotInlineQuery} object. + */ + event: Api.UpdateBotInlineQuery; + + constructor( + query: Api.UpdateBotInlineQuery, + peer: Api.TypePeer, + msgId: number + ) { + super({ + msgId, + chatPeer: peer, + broadcast: false, + }); + this.query = query.query; + this.patternMatch = undefined; + this.queryId = query.queryId; + this._senderId = returnBigInt(query.userId); + this._message = undefined; + this._answered = false; + this.event = query; + } + + _setClient(client: TelegramClient) { + super._setClient(client); + const [sender, inputSender] = _getEntityPair( + this._senderId!.toString(), + this._entities, + client._entityCache + ); + this._sender = sender; + this._inputSender = inputSender; + } + + get id() { + return this.event.queryId; + } + + get messageId() { + return this._messageId!; + } + + get data() { + return this.query; + } + + async answer({ + results, + cacheTime, + gallery, + isPrivate, + switchPm, + nextOffset, + switchWebview, + }: Partial = {}) { + if (this._answered) return; + + return await this._client!.invoke( + new Api.messages.SetInlineBotResults({ + queryId: this.queryId, + results, + cacheTime, + gallery, + private: isPrivate, + switchPm, + nextOffset, + switchWebview, + }) + ).then((res) => { + this._answered = true; + return res; + }); + } + + get viaInline() { + return this.event instanceof Api.UpdateBotInlineQuery; + } + + get sender() { + return this._sender; + } +} diff --git a/gramjs/events/index.ts b/gramjs/events/index.ts index 8e5c2538..7431c55c 100644 --- a/gramjs/events/index.ts +++ b/gramjs/events/index.ts @@ -1,2 +1,3 @@ export { Raw } from "./Raw"; export { NewMessage, NewMessageEvent } from "./NewMessage"; +export { InlineQuery, InlineQueryEvent } from "./InlineQuery";