2015-06-29 22:56:08 +02:00
[](https://travis-ci.org/yagop/node-telegram-bot-api) [](https://coveralls.io/r/yagop/node-telegram-bot-api?branch=master)
2015-06-29 01:04:39 +02:00
2015-06-29 22:37:38 +02:00
Node.js module to interact with official [Telegram Bot API ](https://core.telegram.org/bots/api ). A bot token is needed, to obtain one, talk to [@botfather ](telegram.me/BotFather ) and create a new bot.
2015-06-29 22:19:19 +02:00
```sh
npm install node-telegram-bot-api
```
```js
var TelegramBot = require('node-telegram-bot-api');
var token = 'YOUR_TELEGRAM_BOT_TOKEN';
// Setup polling way
var bot = new TelegramBot(token, {polling: true});
bot.on('message', function (msg) {
var chatId = msg.chat.id;
// photo can be: a file path, a stream or a Telegram file_id
var photo = 'bot.gif';
bot.sendPhoto(chatId, photo, {caption: "I'm a bot!"});
});
```
2015-06-29 22:37:38 +02:00
There are some other examples on [examples ](https://github.com/yagop/node-telegram-bot-api/tree/master/examples ).
2015-06-29 00:51:10 +02:00
* * *
2015-06-29 22:49:16 +02:00
## TelegramBot
Both request method to obtain messages are implemented. To use standard polling, set `polling: true`
on `options` . Notice that [webHook ](https://core.telegram.org/bots/api#setwebhook ) will need a valid (not self signed) SSL certificate.
Emmits `message` when a message arrives.
See: https://core.telegram.org/bots/api
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **String** *token* Bot Token
* **Object** *[options]*
* **Boolean|Object** *[options.polling=false]* Set true to enable polling
* **String|Number** *[options.polling.timeout=4]* Polling time
* **Boolean|Object** *[options.webHook=false]* Set true to enable WebHook
* **String** *[options.webHook.key]* PEM private key to webHook server
* **String** *[options.webHook.cert]* PEM certificate key to webHook server
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## getMe()
2015-06-29 00:51:10 +02:00
Returns basic information about the bot in form of a `User` object.
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise**
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## setWebHook(url)
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
Specify a url to receive incoming updates via an outgoing webHook.
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **String** *url* URL
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## getUpdates([timeout], [limit], [offset])
2015-06-29 00:51:10 +02:00
Use this method to receive incoming updates using long polling
2015-06-29 22:49:16 +02:00
See: https://core.telegram.org/bots/api#getupdates
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Number|String** *[timeout]* Timeout in seconds for long polling.
* **Number|String** *[limit]* Limits the number of updates to be retrieved.
* **Number|String** *[offset]* Identifier of the first update to be returned.
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise** Updates
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## sendMessage(chatId, text, [options])
2015-06-29 00:51:10 +02:00
Send text message.
2015-06-29 22:49:16 +02:00
See: https://core.telegram.org/bots/api#sendmessage
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Number|String** *chatId* Unique identifier for the message recipient
* **Sting** *text* Text of the message to be sent
* **Object** *[options]* Additional Telegram query options
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise**
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## forwardMessage(chatId, fromChatId, messageId)
2015-06-29 00:51:10 +02:00
Forward messages of any kind.
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Number|String** *chatId* Unique identifier for the message recipient
* **Number|String** *fromChatId* Unique identifier for the chat where the original message was sent
* **Number|String** *messageId* Unique message identifier
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise**
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## sendPhoto(chatId, photo, [options])
2015-06-29 00:51:10 +02:00
Send photo
2015-06-29 22:49:16 +02:00
See: https://core.telegram.org/bots/api#sendphoto
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Number|String** *chatId* Unique identifier for the message recipient
* **String|stream.Stream** *photo* A file path or a Stream. Can also be a `file_id` previously uploaded
* **Object** *[options]* Additional Telegram query options
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise**
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
## sendAudio(chatId, audio, [options])
2015-06-29 00:51:10 +02:00
Send audio
2015-06-29 22:49:16 +02:00
See: https://core.telegram.org/bots/api#sendaudio
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Params:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Number|String** *chatId* Unique identifier for the message recipient
* **String|stream.Stream** *audio* A file path or a Stream. Can also be a `file_id` previously uploaded.
* **Object** *[options]* Additional Telegram query options
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
### Return:
2015-06-29 00:51:10 +02:00
2015-06-29 22:49:16 +02:00
* **Promise**
2015-07-06 01:40:54 +02:00
## sendChatAction(chatId, action)
Send chat action. Type of `action` to broadcast. `typing` for text messages, `upload_photo` for photos, `record_video` or `upload_video` for videos, `record_audio` or `upload_audio` for audio files, `upload_document` for general files, `find_location` for location data.
See: https://core.telegram.org/bots/api#sendchataction
### Params:
* **Number|String** *chatId* Unique identifier for the message recipient
2015-07-06 02:04:27 +02:00
* **String** *action* Type of action to broadcast.
2015-07-06 01:40:54 +02:00
### Return:
* **Promise**