-
Notifications
You must be signed in to change notification settings - Fork 37
Home
Slimbot is a fuss-free, thin wrapper around Telegram Bot API for Node.js. No frills. It is built around the concept of Promises, and uses Bluebird, Request-Promise and EventEmitter3 as its core dependencies. It is also designed to be magic-free, so that developers can easily grok how it works.
Slimbot uses #getUpdates to receive updates out of the box. This makes it easy for developers to jump in and start playing with it without messing around with web hooks.
npm i slimbot
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_BOT_TOKEN']);
// Register listeners
slimbot.on('message', message => {
slimbot.sendMessage(message.chat.id, 'Message received');
});
// Call API
slimbot.startPolling();
Events you can listen to:
- 'message'
- 'edited_message'
- 'channel_post'
- 'edited_channel_post'
- 'callback_query'
- 'inline_query'
- 'chosen_inline_result'
- 'shipping_query'
- 'pre_checkout_query'
- 'poll'
- 'poll_answer'
- 'my_chat_member'
- 'chat_member'
Take note that
inline_query
andchosen_inline_result
only works if you have sent/setinline
and/setinlinefeedback
commands to @BotFather. Read the docs for more information.
The bot must be an administrator in the chat to receive
chat_member
updates about other chat members. By default, onlymy_chat_member
updates about the bot itself are received.
slimbot.on('message', message => {
// do something with message
});
slimbot.on('edited_message', message => {
// do something with message
});
// and many more...
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:
// Simple usage, without any optional parameters
slimbot.sendMessage('123456789', 'hello');
// Defining optional parameters
let optionalParams = {
parse_mode: "Markdown",
disable_web_page_preview: true,
disable_notification: true,
reply_to_message_id: 1234,
reply_markup: JSON.stringify({
inline_keyboard: [[
{ text: 'Today', callback_data: 'pick_today' },
{ text: 'Pick a date', callback_data: 'pick_date' }
]]
})
}
// Calling a method with optional parameters
slimbot.sendMessage('123456789', 'hello', optionalParams);
There are 3 ways to send files:
- Method 1: Using a
file_id
when the the file already exists on Telegram's servers - Method 2: Using a
HTTP URL
- Method 3: Uploading a file to Telegram's servers
Check out the full example to learn how it works.
const fs = require('fs');
// Method 1
slimbot.sendPhoto(chat_id, 'AgADBQADqacxG2gbbxCWBkgvcmeAgxVPyjIABBlug37DKyhDEU0AAgI');
// Method 2
slimbot.sendPhoto(chat_id, 'https://fbatwork.files.wordpress.com/2016/10/govtech-logo.jpg');
// Method 3
let inputFile = fs.createReadStream(__dirname + '/bulb.png');
slimbot.sendPhoto(chat_id, inputFile).then(message => {
// once successful, you can grab the file_id of the file
console.log(message.result.photo[0].file_id);
});
These are actually convenience methods that use the same underlying editMessageText
method in the API.
- editInlineMessageText
- editInlineMessageMedia
- editInlineMessageCaption
- editInlineMessageReplyMarkup
- setInlineChatStickerSet
- deleteInlineChatStickerSet
Call these additional methods with inline_message_id
rather than chat_id
and message_id
.
// slimbot.editMessageText(chat_id, message_id, 'edited message');
slimbot.editMessageText('123456789', 1234, 'edited message');
// slimbot.editInlineMessageText(inline_message_id, 'edited message');
slimbot.editInlineMessageText('4321', 'edited message');
slimbot.stopPolling();
If you'd like more fine-grained control over how your bot receives updates, set up a webhook. At the moment, you will have to parse the updates yourself, giving you freedom in choosing a Node.js server framework of your liking.
Using Restify for a simple API endpoint:
const Slimbot = require('slimbot');
const slimbot = new Slimbot(process.env['TELEGRAM_TOKEN']);
const restify = require('restify');
let server = restify.createServer();
server.use(restify.bodyParser());
// Setup webhook integration
slimbot.setWebhook({ url: 'https://www.example.com/bot_updates' });
// Get webhook status
slimbot.getWebhookInfo();
// Handle updates (example)
server.post('/bot_updates', function handle(req, res) {
let update = req.body;
// handle type of update here...
// i.e. if (update.message) { ... }
});
server.listen(8443);