From 479399f2964c52135a8f0a24a090eabdfb068741 Mon Sep 17 00:00:00 2001 From: Niv Ezra Date: Sat, 13 Jan 2024 00:27:15 +0200 Subject: [PATCH 1/3] added editInlineKeyboard support --- examples/inlineKeyboard.js | 31 +++++++++++++++++++++++++++++++ src/bot.js | 17 +++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/examples/inlineKeyboard.js b/examples/inlineKeyboard.js index 9db7f66..7566b02 100644 --- a/examples/inlineKeyboard.js +++ b/examples/inlineKeyboard.js @@ -31,6 +31,31 @@ bot.onButton('', callbackQuery => { ); }); +bot.onButton('edit', callbackQuery => { + inlineKeyboard.push([ + { + text: 'New button - remove me', + callback_data: 'remove', + }, + ]); + bot.editInlineKeyboard( + callbackQuery.message.chat.id, + callbackQuery.message.message_id, + null, + inlineKeyboard, + ); +}); + +bot.onButton('remove', callbackQuery => { + inlineKeyboard.pop(); + bot.editInlineKeyboard( + callbackQuery.message.chat.id, + callbackQuery.message.message_id, + null, + inlineKeyboard, + ); +}); + const inlineKeyboard = [ [ { @@ -54,4 +79,10 @@ const inlineKeyboard = [ callback_data: 'random', }, ], + [ + { + text: 'Add button - edit (editMessageReplyMarkup)', + callback_data: 'edit', + }, + ], ]; diff --git a/src/bot.js b/src/bot.js index a0f4e85..9d9fddf 100644 --- a/src/bot.js +++ b/src/bot.js @@ -144,6 +144,23 @@ class Telenode { }); } + async editInlineKeyboard(chatId, messageId, inlineMessageId, inlineKeyboard) { + if (!inlineMessageId && (!chatId || !messageId)) { + throw new Error( + 'inlineMessageId is required when chatId and messageId are not specified', + ); + } + const url = this.#baseUrl + '/editMessageReplyMarkup'; + return await axios.post(url, { + chat_id: chatId, + message_id: messageId, + inline_message_id: inlineMessageId, + reply_markup: { + inline_keyboard: inlineKeyboard, + }, + }); + } + async sendReplyKeyboard(chatId, text, replyKeyboard, oneTimeKeyboard) { if (!text) { throw Error('text parameter is required'); From 116beea1fdb15ea94ab92119288ad4ae62439a78 Mon Sep 17 00:00:00 2001 From: Niv Ezra Date: Sat, 13 Jan 2024 00:46:08 +0200 Subject: [PATCH 2/3] added delete-webhook script --- package.json | 1 + scripts/deleteWebhook.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 scripts/deleteWebhook.js diff --git a/package.json b/package.json index aca557f..36e68e0 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "dev": "nodemon ./scripts/runDev.js", "publish:dry": "npm publish --dry-run", "set-webhook": "node ./scripts/setWebhook.js", + "delete-webhook": "node ./scripts/deleteWebhook.js", "prepare": "husky install", "format": "npx prettier . --write" }, diff --git a/scripts/deleteWebhook.js b/scripts/deleteWebhook.js new file mode 100644 index 0000000..2dc3221 --- /dev/null +++ b/scripts/deleteWebhook.js @@ -0,0 +1,31 @@ +#!/usr/bin/env node + +const axios = require('axios'); + +try { + require('dotenv').config(); +} catch (e) { + if (e.code === 'MODULE_NOT_FOUND') { + console.log( + '[] dotenv is not installed, trying with environment variables or command line arguments.', + ); + } +} + +const apiToken = process.env.API_TOKEN || process.env.npm_config_apiToken; + +const url = `https://api.telegram.org/bot${apiToken}/deleteWebhook`; + +(async () => { + try { + const res = await axios.post(url); + console.log('[] Deleting webhook'); + if (res.status === 200) { + console.log('[]', res.data.description); + } + } catch (e) { + console.log('[] Failed deleting webhook! Maybe the api token is invalid'); + console.log(' ', e?.message); + console.log(' ', e?.code); + } +})(); From b9e21a9f32a28c12bb26b1661bb1fa9ff3e75e5c Mon Sep 17 00:00:00 2001 From: Niv Ezra Date: Sat, 13 Jan 2024 00:50:13 +0200 Subject: [PATCH 3/3] updated README --- README.md | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1eb2aee..78bf387 100644 --- a/README.md +++ b/README.md @@ -12,12 +12,16 @@ Lightweight Telegram API framework for Node.js ✅ Explicit messages handlers
-✅ Fallback messages handler (empty string) +✅ Fallback messages handler (empty string - any messages)
✅ Regex matching on text messages
✅ Buttons support (inline keyboard, reply keyboard and remove reply keyboard)
+✅ Fallback handler for any button (empty string - button callback_data) +
+✅ Edit inline keyboards +
✅ Secret token support
✅ Long polling support @@ -70,9 +74,16 @@ Then you can execute the following command: npx set-webhook ``` +* If you want to delete a webhook use the command: + +``` +npx delete-webhook +``` + ### Long polling -If you prefer to use long polling method over creating a server with webhook you can use the `startLongPolling` method instead of `createServer`. +If you prefer to use long polling method over creating a server with webhook you can use the `startLongPolling` method instead of `createServer`. If you have a webhook set up already, you need to delete it, you can use the `npx delete-webhook` command to delete it. +
The method accepts `pollingDelay` - a number that represents milliseconds (must be at least 50ms).
@@ -194,6 +205,5 @@ to use the feature. - [ ] Chat ID handlers - [ ] Arguments validations - [ ] Optimize Telegram API requests -- [ ] Support edit reply markup - [ ] Add extra security with query params token - [ ] Add tests