From 479399f2964c52135a8f0a24a090eabdfb068741 Mon Sep 17 00:00:00 2001 From: Niv Ezra Date: Sat, 13 Jan 2024 00:27:15 +0200 Subject: [PATCH] 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');