Skip to content

Commit

Permalink
feat: Add InlineQuery and InlineQueryEvent classes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkdeathoriginal committed Jul 8, 2024
1 parent 9233537 commit df3a5a9
Show file tree
Hide file tree
Showing 2 changed files with 199 additions and 0 deletions.
198 changes: 198 additions & 0 deletions gramjs/events/InlineQuery.ts
Original file line number Diff line number Diff line change
@@ -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<NewInlineQueryInterface> = {}) {
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<AnswerInlineQueryParams> = {}) {
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;
}
}
1 change: 1 addition & 0 deletions gramjs/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { Raw } from "./Raw";
export { NewMessage, NewMessageEvent } from "./NewMessage";
export { InlineQuery, InlineQueryEvent } from "./InlineQuery";

0 comments on commit df3a5a9

Please sign in to comment.