Skip to content

Commit

Permalink
add hears
Browse files Browse the repository at this point in the history
  • Loading branch information
Sempai-07 committed Jul 14, 2023
1 parent 8a13afe commit 919fefc
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion src/TelegramBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export class TelegramBot<F = Buffer> extends BaseClient<F> {
intents?: string[] | number[] | number | null;
baseUrl: string = "";
session?: any;
updatesProcess?: CombinedClass<F>;

constructor(
token: string,
Expand Down Expand Up @@ -161,6 +160,34 @@ export class TelegramBot<F = Buffer> extends BaseClient<F> {
this.session = params;
}

/**
* Registers a callback function to be executed when a message is received
* that includes the specified text.
* ```ts
* import { TelegramBot } from "telegramsjs"
*
* const bot = new TelegramBot('BOT_TOKEN');
*
* bot.hears('hi', (ctx) => ctx.reply('hi!'));
*
* bot.login()
* ```
* @param {string | string[]} text - The text to match in the received messages.
* @param {(message: (Message.TextMessage & Context<F>)) => void} callback - The callback function to be executed when a matching message is received.
* It receives the matched message object as a parameter.
* @returns void
*/
public hears(
text: string,
callback: (message: Message.TextMessage & Context<F>) => void
): void {
this.on("message", (message: Message.TextMessage & Context<F>) => {
if (message.text.includes(text)) {
callback(message);
}
});
}

/**
* The function that starts the whole process.
* ```ts
Expand Down

0 comments on commit 919fefc

Please sign in to comment.