Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added anyButtonHandler #11

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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