Slimbot
A fuss-free, thin wrapper around Telegram Bot API for Node.js. No frills.
Updated for Bot API 2.3.1.
Getting started
npm i slimbot
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_TOKEN']);
slimbot.on('message', message => {
slimbot.sendMessage(message.chat.id, 'Message received');
});
slimbot.startPolling();
Now go ahead and type a message to your bot in Telegram. It should reply you with 'Message received' in the chat.
All methods return a promise. This means you can inspect the returned objects if you want to:
slimbot.sendMessage('123456789', 'Message received').then(message => {
console.log(message);
});
In this case, the sendMessage
method returns a Message object as stated in the documentation.
Events
Events you can listen to:
- 'message'
- 'edited_message'
- 'channel_post'
- 'edited_channel_post'
- 'callback_query'
- 'inline_query'
- 'chosen_inline_result'
Take note that inline_query
and chosen_inline_result
only works if you have sent /setinline
and /setinlinefeedback
commands to @BotFather. Read the docs for more information.
slimbot.on('message', message => {
});
slimbot.on('edited_message', message => {
});
slimbot.on('channel_post', post => {
});
slimbot.on('edited_channel_post', post => {
});
slimbot.on('callback_query', query => {
});
slimbot.on('inline_query', query => {
});
slimbot.on('chosen_inline_result', result => {
});
Methods
All methods found in the Telegram Bot API Documentation have been implemented.
Use them as they are described in the docs, providing the required parameters and if you wish, the optional parameters:
slimbot.sendMessage('123456789', 'hello');
let optionalParams = {
parse_mode: true,
disable_web_page_preview: true,
disable_notification: true,
reply_to_message_id: 1234,
reply_markup: {
inline_keyboard: [[
{ text: 'Today', callback_data: 'pick_today' },
{ text: 'Pick a date', callback_data: 'pick_date' }
]]
}
}
slimbot.sendMessage('123456789', 'hello', optionalParams);
Sending files
There are 3 ways to send files:
- Using a
file_id
when the the file already exists on Telegram's servers - Using a
HTTP URL
- Uploading a file to Telegram's servers
Check out the full example to learn how it works.
const fs = require('fs');
slimbot.sendPhoto(chat_id, 'AgADBQADqacxG2gbbxCWBkgvcmeAgxVPyjIABBlug37DKyhDEU0AAgI');
slimbot.sendPhoto(chat_id, 'https://fbatwork.files.wordpress.com/2016/10/govtech-logo.jpg');
let inputFile = fs.createReadStream(__dirname + '/bulb.png');
slimbot.sendPhoto(chat_id, inputFile).then(message => {
console.log(message.result.photo[0].file_id);
});
Additional methods implemented
These are actually convenience methods that use the same underlying editMessageText
method in the API.
- editInlineMessageText
- editInlineMessageCaption
- editInlineMessageReplyMarkup
Call these additional methods with inline_message_id
rather than chat_id
and message_id
.
slimbot.editMessageText('123456789', 1234, 'edited message');
slimbot.editInlineMessageText('4321', 'edited message');
Webhook
If you're familiar with the Telegram Bot API, you'd have realised by now that this library defaults to using getUpdates
for simplicity and ease of use.
Assuming you already have a domain name with SSL:
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_TOKEN']);
slimbot.on('message', message => {
slimbot.sendMessage(message.chat.id, 'Message received');
});
slimbot.setWebhook('https://www.example.com');
slimbot.getWebhookInfo();
slimbot.deleteWebhook();