mirror of
https://github.com/yagop/node-telegram-bot-api
synced 2025-08-31 14:25:57 +00:00
58 lines
3.3 KiB
Handlebars
58 lines
3.3 KiB
Handlebars
[](https://travis-ci.org/yagop/node-telegram-bot-api) [](https://ci.appveyor.com/project/yagop/node-telegram-bot-api/branch/master) [](https://coveralls.io/r/yagop/node-telegram-bot-api?branch=master) [](https://www.bithound.io/github/yagop/node-telegram-bot-api) [](https://telegram.me/node_telegram_bot_api) [](https://telegram.me/Yago_Perez)
|
|
|
|
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](https://telegram.me/BotFather) and create a new bot.
|
|
|
|
```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});
|
|
|
|
// Matches /echo [whatever]
|
|
bot.onText(/\/echo (.+)/, function (msg, match) {
|
|
var fromId = msg.from.id;
|
|
var resp = match[1];
|
|
bot.sendMessage(fromId, resp);
|
|
});
|
|
|
|
// Any kind of message
|
|
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 = 'cats.png';
|
|
bot.sendPhoto(chatId, photo, {caption: 'Lovely kittens'});
|
|
});
|
|
```
|
|
|
|
There are some other examples on [examples](https://github.com/yagop/node-telegram-bot-api/tree/master/examples).
|
|
|
|
### Events
|
|
Every time TelegramBot receives a message, it emits a `message`. Depending on which [message](https://core.telegram.org/bots/api#message) was received, emits an event from this ones: `text`, `audio`, `document`, `photo`, `sticker`, `video`, `voice`, `contact`, `location`, `new_chat_participant`, `left_chat_participant`, `new_chat_title`, `new_chat_photo`, `delete_chat_photo`, `group_chat_created`. Its much better to listen a specific event rather than a `message` in order to stay safe from the content.
|
|
TelegramBot emits `inline_query` when receives an [https://core.telegram.org/bots/api#inlinequery](Inline Query) and `chosen_inline_result` when receives a [ChosenInlineResult](https://core.telegram.org/bots/api#choseninlineresult). Bot must be enabled on [inline mode](https://core.telegram.org/bots/api#inline-mode)
|
|
* * *
|
|
|
|
### WebHooks
|
|
|
|
Telegram only supports HTTPS connections to WebHooks, in order to set a WebHook a private key file and public certificate must be used. Since August 29, 2015 Telegram supports self signed ones, to generate them:
|
|
```bash
|
|
# Our private cert will be key.pem, keep in private this file.
|
|
openssl genrsa -out key.pem 2048
|
|
# Our public certificate will be crt.pem
|
|
openssl req -new -sha256 -key key.pem -out crt.pem
|
|
```
|
|
Once they are generated, the `crt.pem` can be provided to `telegramBot.setWebHook(url, crt)` as `crt`.
|
|
|
|
## API Reference
|
|
{{#class name="TelegramBot"~}}
|
|
{{>header~}}
|
|
{{>body~}}
|
|
{{>member-index~}}
|
|
{{>members~}}
|
|
{{/class}}
|
|
* * *
|