Skip to content

Commit

Permalink
added anyButtonHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
NivEz committed Jan 6, 2024
1 parent 82ba28f commit 8b2e3ac
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
13 changes: 13 additions & 0 deletions examples/inlineKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ bot.onButton('hello', callbackQuery => {
bot.sendTextMessage(`Hello ${senderName}! How are you?`, callbackQuery.message.chat.id);
});

bot.onButton('', callbackQuery => {
bot.sendTextMessage(
'The random button was pressed, it was targeted via the button handler with empty string parameter',
callbackQuery.message.chat.id,
);
});

const inlineKeyboard = [
[
{
Expand All @@ -41,4 +48,10 @@ const inlineKeyboard = [
callback_data: 'exit',
},
],
[
{
text: 'Random button (onButton with empty string)',
callback_data: 'random',
},
],
];
14 changes: 12 additions & 2 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Telenode {
this.arrRegexHandlers = [];
this.anyTextHandler = null;
this.buttonHandlers = {};
this.anyButtonHandler = null;
this.#baseUrl = 'https://api.telegram.org/bot' + apiToken;
this.#secretToken = secretToken;
this.useLongPolling = false;
Expand Down Expand Up @@ -87,6 +88,11 @@ class Telenode {
const buttonHandler = this.buttonHandlers[buttonData];
if (buttonHandler) {
buttonHandler(callbackQuery);
return;
}
// Similar to the text message handler, this should be the final step to validate that no matches occurred
if (this.anyButtonHandler) {
this.anyButtonHandler(callbackQuery);
}
}

Expand All @@ -106,10 +112,14 @@ class Telenode {
}

onButton(buttonDataTrigger, handler) {
if (!buttonDataTrigger || typeof buttonDataTrigger !== 'string') {
if (typeof buttonDataTrigger !== 'string') {
return;
}
this.buttonHandlers[buttonDataTrigger] = handler;
if (buttonDataTrigger === '') {
this.anyButtonHandler = handler;
} else {
this.buttonHandlers[buttonDataTrigger] = handler;
}
}

async sendTextMessage(text, chatId) {
Expand Down

0 comments on commit 8b2e3ac

Please sign in to comment.