-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add InlineQuery and InlineQueryEvent classes
- Loading branch information
1 parent
9233537
commit df3a5a9
Showing
2 changed files
with
199 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |