Skip to content

Commit

Permalink
Merge pull request #15 from NivEz/feature/edit-inline-keyboard
Browse files Browse the repository at this point in the history
added edit inline keyboard support
  • Loading branch information
NivEz committed Jan 12, 2024
2 parents 41e7bdb + b9e21a9 commit 275cc19
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 3 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ Lightweight Telegram API framework for Node.js

✅ Explicit messages handlers
<br>
✅ Fallback messages handler (empty string)
✅ Fallback messages handler (empty string - any messages)
<br>
✅ Regex matching on text messages
<br>
✅ Buttons support (inline keyboard, reply keyboard and remove reply keyboard)
<br>
✅ Fallback handler for any button (empty string - button callback_data)
<br>
✅ Edit inline keyboards
<br>
✅ Secret token support
<br>
✅ Long polling support
Expand Down Expand Up @@ -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.

<br>
The method accepts `pollingDelay` - a number that represents milliseconds (must be at least 50ms).
<br>
Expand Down Expand Up @@ -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
31 changes: 31 additions & 0 deletions examples/inlineKeyboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[
{
Expand All @@ -54,4 +79,10 @@ const inlineKeyboard = [
callback_data: 'random',
},
],
[
{
text: 'Add button - edit (editMessageReplyMarkup)',
callback_data: 'edit',
},
],
];
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
31 changes: 31 additions & 0 deletions scripts/deleteWebhook.js
Original file line number Diff line number Diff line change
@@ -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);
}
})();
17 changes: 17 additions & 0 deletions src/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 275cc19

Please sign in to comment.