node-telegram-bot-api
Advanced tools
Comparing version 0.12.1 to 0.13.0
{ | ||
"name": "node-telegram-bot-api", | ||
"version": "0.12.1", | ||
"version": "0.13.0", | ||
"description": "Telegram Bot API", | ||
@@ -18,3 +18,4 @@ "main": "index.js", | ||
"test": "./node_modules/.bin/mocha test/index.js", | ||
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" | ||
"test-cov": "./node_modules/.bin/istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage", | ||
"gen-doc": "./node_modules/.bin/jsdoc2md --src src/telegram.js -t README.hbs > README.md" | ||
}, | ||
@@ -30,4 +31,7 @@ "author": "Yago Pérez <yagoperezs@gmail.com>", | ||
"devDependencies": { | ||
"contributor": "^0.1.25", | ||
"coveralls": "^2.11.2", | ||
"del": "^2.0.2", | ||
"istanbul": "^0.3.17", | ||
"jsdoc-to-markdown": "^1.2.0", | ||
"mocha": "^2.2.5", | ||
@@ -47,11 +51,29 @@ "mocha-lcov-reporter": "0.0.2", | ||
{ | ||
"name": "Guido García", | ||
"email": "palmerabollo@gmail.com", | ||
"url": "https://github.com/palmerabollo", | ||
"contributions": 1, | ||
"additions": 1, | ||
"deletions": 1, | ||
"hireable": true | ||
}, | ||
{ | ||
"name": "Yago", | ||
"email": "yago@yago.me", | ||
"url": "https://github.com/yagop", | ||
"contributions": 89, | ||
"additions": 1598, | ||
"deletions": 435, | ||
"contributions": 99, | ||
"additions": 1685, | ||
"deletions": 504, | ||
"hireable": true | ||
}, | ||
{ | ||
"name": "Anton", | ||
"email": null, | ||
"url": "https://github.com/Feverqwe", | ||
"contributions": 1, | ||
"additions": 23, | ||
"deletions": 5, | ||
"hireable": null | ||
}, | ||
{ | ||
"name": "Sebastian Troć", | ||
@@ -63,7 +85,16 @@ "email": "sebastian.troc@gucman.pl", | ||
"deletions": 1, | ||
"hireable": false | ||
"hireable": null | ||
}, | ||
{ | ||
"name": "Patricio López Juri", | ||
"email": "lopezjuripatricio@gmail.com", | ||
"url": "https://github.com/mrpatiwi", | ||
"contributions": 1, | ||
"additions": 49, | ||
"deletions": 2, | ||
"hireable": true | ||
}, | ||
{ | ||
"name": "Riddler", | ||
"email": "", | ||
"email": null, | ||
"url": "https://github.com/Waterloo", | ||
@@ -76,2 +107,11 @@ "contributions": 3, | ||
{ | ||
"name": "Ivan Skorokhodov", | ||
"email": "iskorokhodov@gmail.com", | ||
"url": "https://github.com/universome", | ||
"contributions": 3, | ||
"additions": 219, | ||
"deletions": 5, | ||
"hireable": null | ||
}, | ||
{ | ||
"name": "Ilias Ismanalijev", | ||
@@ -78,0 +118,0 @@ "email": "hello@illyism.com", |
346
README.md
@@ -26,10 +26,36 @@ [![Build Status](https://travis-ci.org/yagop/node-telegram-bot-api.svg?branch=master)](https://travis-ci.org/yagop/node-telegram-bot-api) [![Build status](https://ci.appveyor.com/api/projects/status/ujko6bsum3g5msjh/branch/master?svg=true)](https://ci.appveyor.com/project/yagop/node-telegram-bot-api/branch/master) [![Coverage Status](https://coveralls.io/repos/yagop/node-telegram-bot-api/badge.svg?branch=master)](https://coveralls.io/r/yagop/node-telegram-bot-api?branch=master) [![bitHound Score](https://www.bithound.io/github/yagop/node-telegram-bot-api/badges/score.svg)](https://www.bithound.io/github/yagop/node-telegram-bot-api) | ||
### 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`, `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. | ||
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. | ||
* * * | ||
<!-- Start src/telegram.js --> | ||
## API Reference | ||
<a name="TelegramBot"></a> | ||
## TelegramBot | ||
TelegramBot | ||
**Kind**: global class | ||
**See**: https://core.telegram.org/bots/api | ||
* [TelegramBot](#TelegramBot) | ||
* [new TelegramBot(token, [options])](#new_TelegramBot_new) | ||
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code> | ||
* [.setWebHook(url)](#TelegramBot+setWebHook) | ||
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code> | ||
* [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code> | ||
* [.forwardMessage(chatId, fromChatId, messageId)](#TelegramBot+forwardMessage) ⇒ <code>Promise</code> | ||
* [.sendPhoto(chatId, photo, [options])](#TelegramBot+sendPhoto) ⇒ <code>Promise</code> | ||
* [.sendAudio(chatId, audio, [options])](#TelegramBot+sendAudio) ⇒ <code>Promise</code> | ||
* [.sendDocument(chatId, A, [options])](#TelegramBot+sendDocument) ⇒ <code>Promise</code> | ||
* [.sendSticker(chatId, A, [options])](#TelegramBot+sendSticker) ⇒ <code>Promise</code> | ||
* [.sendVideo(chatId, A, [options])](#TelegramBot+sendVideo) ⇒ <code>Promise</code> | ||
* [.sendVoice(chatId, voice, [options])](#TelegramBot+sendVoice) ⇒ <code>Promise</code> | ||
* [.sendChatAction(chatId, action)](#TelegramBot+sendChatAction) ⇒ <code>Promise</code> | ||
* [.getUserProfilePhotos(userId, [offset], [limit])](#TelegramBot+getUserProfilePhotos) ⇒ <code>Promise</code> | ||
* [.sendLocation(chatId, latitude, longitude, [options])](#TelegramBot+sendLocation) ⇒ <code>Promise</code> | ||
* [.getFile(fileId)](#TelegramBot+getFile) ⇒ <code>Promise</code> | ||
* [.getFileLink(fileId)](#TelegramBot+getFileLink) ⇒ <code>Promise</code> | ||
* [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ <code>Promise</code> | ||
<a name="new_TelegramBot_new"></a> | ||
### new TelegramBot(token, [options]) | ||
Both request method to obtain messages are implemented. To use standard polling, set `polling: true` | ||
@@ -39,163 +65,150 @@ on `options`. Notice that [webHook](https://core.telegram.org/bots/api#setwebhook) will need a valid (not self signed) SSL certificate. | ||
See: https://core.telegram.org/bots/api | ||
### Params: | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| token | <code>String</code> | | Bot Token | | ||
| [options] | <code>Object</code> | | | | ||
| [options.polling] | <code>Boolean</code> | <code>Object</code> | <code>false</code> | Set true to enable polling or set options | | ||
| [options.polling.timeout] | <code>String</code> | <code>Number</code> | <code>4</code> | Polling time | | ||
| [options.polling.interval] | <code>String</code> | <code>Number</code> | <code>2000</code> | Interval between requests in miliseconds | | ||
| [options.webHook] | <code>Boolean</code> | <code>Object</code> | <code>false</code> | Set true to enable WebHook or set options | | ||
| [options.webHook.key] | <code>String</code> | | PEM private key to webHook server | | ||
| [options.webHook.cert] | <code>String</code> | | PEM certificate key to webHook server | | ||
* **String** *token* Bot Token | ||
* **Object** *[options]* | ||
* **Boolean|Object** *[options.polling=false]* Set true to enable polling or set options | ||
* **String|Number** *[options.polling.timeout=4]* Polling time | ||
* **String|Number** *[options.polling.interval=2000]* Interval between requests in miliseconds | ||
* **Boolean|Object** *[options.webHook=false]* Set true to enable WebHook or set options | ||
* **String** *[options.webHook.key]* PEM private key to webHook server | ||
* **String** *[options.webHook.cert]* PEM certificate key to webHook server | ||
## getMe() | ||
<a name="TelegramBot+getMe"></a> | ||
### telegramBot.getMe() ⇒ <code>Promise</code> | ||
Returns basic information about the bot in form of a `User` object. | ||
See: https://core.telegram.org/bots/api#getme | ||
### Return: | ||
* **Promise** | ||
## setWebHook(url) | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#getme | ||
<a name="TelegramBot+setWebHook"></a> | ||
### telegramBot.setWebHook(url) | ||
Specify an url to receive incoming updates via an outgoing webHook. | ||
See: https://core.telegram.org/bots/api#setwebhook | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#setwebhook | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| url | <code>String</code> | URL where Telegram will make HTTP Post. Leave empty to delete webHook. | | ||
* **String** *url* URL where Telegram will make HTTP Post. Leave empty to delete webHook. | ||
## getUpdates([timeout], [limit], [offset]) | ||
<a name="TelegramBot+getUpdates"></a> | ||
### telegramBot.getUpdates([timeout], [limit], [offset]) ⇒ <code>Promise</code> | ||
Use this method to receive incoming updates using long polling | ||
See: https://core.telegram.org/bots/api#getupdates | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**Returns**: <code>Promise</code> - Updates | ||
**See**: https://core.telegram.org/bots/api#getupdates | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| [timeout] | <code>Number</code> | <code>String</code> | Timeout in seconds for long polling. | | ||
| [limit] | <code>Number</code> | <code>String</code> | Limits the number of updates to be retrieved. | | ||
| [offset] | <code>Number</code> | <code>String</code> | Identifier of the first update to be returned. | | ||
* **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. | ||
### Return: | ||
* **Promise** Updates | ||
## sendMessage(chatId, text, [options]) | ||
<a name="TelegramBot+sendMessage"></a> | ||
### telegramBot.sendMessage(chatId, text, [options]) ⇒ <code>Promise</code> | ||
Send text message. | ||
See: https://core.telegram.org/bots/api#sendmessage | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendmessage | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| text | <code>String</code> | Text of the message to be sent | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **String** *text* Text of the message to be sent | ||
* **Object** *[options]* Additional Telegram query options | ||
### Return: | ||
* **Promise** | ||
## forwardMessage(chatId, fromChatId, messageId) | ||
<a name="TelegramBot+forwardMessage"></a> | ||
### telegramBot.forwardMessage(chatId, fromChatId, messageId) ⇒ <code>Promise</code> | ||
Forward messages of any kind. | ||
### Params: | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
* **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 | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| fromChatId | <code>Number</code> | <code>String</code> | Unique identifier for the chat where the original message was sent | | ||
| messageId | <code>Number</code> | <code>String</code> | Unique message identifier | | ||
### Return: | ||
* **Promise** | ||
## sendPhoto(chatId, photo, [options]) | ||
<a name="TelegramBot+sendPhoto"></a> | ||
### telegramBot.sendPhoto(chatId, photo, [options]) ⇒ <code>Promise</code> | ||
Send photo | ||
See: https://core.telegram.org/bots/api#sendphoto | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendphoto | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| photo | <code>String</code> | <code>stream.Stream</code> | A file path or a Stream. Can also be a `file_id` previously uploaded | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **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 | ||
### Return: | ||
* **Promise** | ||
## sendAudio(chatId, audio, [options]) | ||
<a name="TelegramBot+sendAudio"></a> | ||
### telegramBot.sendAudio(chatId, audio, [options]) ⇒ <code>Promise</code> | ||
Send audio | ||
See: https://core.telegram.org/bots/api#sendaudio | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendaudio | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| audio | <code>String</code> | <code>stream.Stream</code> | A file path or a Stream. Can also be a `file_id` previously uploaded. | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **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 | ||
### Return: | ||
* **Promise** | ||
## sendDocument(chatId, A, [options]) | ||
<a name="TelegramBot+sendDocument"></a> | ||
### telegramBot.sendDocument(chatId, A, [options]) ⇒ <code>Promise</code> | ||
Send Document | ||
See: https://core.telegram.org/bots/api#sendDocument | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendDocument | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| A | <code>String</code> | <code>stream.Stream</code> | file path or a Stream. Can also be a `file_id` previously uploaded. | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **String|stream.Stream** *A* file path or a Stream. Can also be a `file_id` previously uploaded. | ||
* **Object** *[options]* Additional Telegram query options | ||
### Return: | ||
* **Promise** | ||
## sendSticker(chatId, A, [options]) | ||
<a name="TelegramBot+sendSticker"></a> | ||
### telegramBot.sendSticker(chatId, A, [options]) ⇒ <code>Promise</code> | ||
Send .webp stickers. | ||
See: https://core.telegram.org/bots/api#sendsticker | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendsticker | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| A | <code>String</code> | <code>stream.Stream</code> | file path or a Stream. Can also be a `file_id` previously uploaded. | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **String|stream.Stream** *A* file path or a Stream. Can also be a `file_id` previously uploaded. | ||
* **Object** *[options]* Additional Telegram query options | ||
### Return: | ||
* **Promise** | ||
## sendVideo(chatId, A, [options]) | ||
<a name="TelegramBot+sendVideo"></a> | ||
### telegramBot.sendVideo(chatId, A, [options]) ⇒ <code>Promise</code> | ||
Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). | ||
See: https://core.telegram.org/bots/api#sendvideo | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendvideo | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| A | <code>String</code> | <code>stream.Stream</code> | file path or a Stream. Can also be a `file_id` previously uploaded. | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **String|stream.Stream** *A* file path or a Stream. Can also be a `file_id` previously uploaded. | ||
* **Object** *[options]* Additional Telegram query options | ||
<a name="TelegramBot+sendVoice"></a> | ||
### telegramBot.sendVoice(chatId, voice, [options]) ⇒ <code>Promise</code> | ||
Send voice | ||
### Return: | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendvoice | ||
* **Promise** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| voice | <code>String</code> | <code>stream.Stream</code> | A file path or a Stream. Can also be a `file_id` previously uploaded. | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
## sendChatAction(chatId, action) | ||
<a name="TelegramBot+sendChatAction"></a> | ||
### telegramBot.sendChatAction(chatId, action) ⇒ <code>Promise</code> | ||
Send chat action. | ||
@@ -207,48 +220,81 @@ `typing` for text messages, | ||
See: https://core.telegram.org/bots/api#sendchataction | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendchataction | ||
### Params: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| action | <code>String</code> | Type of action to broadcast. | | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **String** *action* Type of action to broadcast. | ||
<a name="TelegramBot+getUserProfilePhotos"></a> | ||
### telegramBot.getUserProfilePhotos(userId, [offset], [limit]) ⇒ <code>Promise</code> | ||
Use this method to get a list of profile pictures for a user. | ||
Returns a [UserProfilePhotos](https://core.telegram.org/bots/api#userprofilephotos) object. | ||
### Return: | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#getuserprofilephotos | ||
* **Promise** | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| userId | <code>Number</code> | <code>String</code> | Unique identifier of the target user | | ||
| [offset] | <code>Number</code> | Sequential number of the first photo to be returned. By default, all photos are returned. | | ||
| [limit] | <code>Number</code> | Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100. | | ||
## getUserProfilePhotos(userId, [offset], [limit]) | ||
<a name="TelegramBot+sendLocation"></a> | ||
### telegramBot.sendLocation(chatId, latitude, longitude, [options]) ⇒ <code>Promise</code> | ||
Send location. | ||
Use this method to send point on the map. | ||
Use this method to get a list of profile pictures for a user. | ||
Returns a [UserProfilePhotos](https://core.telegram.org/bots/api#userprofilephotos) object. | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#sendlocation | ||
See: https://core.telegram.org/bots/api#getuserprofilephotos | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| chatId | <code>Number</code> | <code>String</code> | Unique identifier for the message recipient | | ||
| latitude | <code>Float</code> | Latitude of location | | ||
| longitude | <code>Float</code> | Longitude of location | | ||
| [options] | <code>Object</code> | Additional Telegram query options | | ||
### Params: | ||
<a name="TelegramBot+getFile"></a> | ||
### telegramBot.getFile(fileId) ⇒ <code>Promise</code> | ||
Get file. | ||
Use this method to get basic info about a file and prepare it for downloading. | ||
Attention: link will be valid for 1 hour. | ||
* **Number|String** *userId* Unique identifier of the target user | ||
* **Number** *[offset]* Sequential number of the first photo to be returned. By default, all photos are returned. | ||
* **Number** *[limit]* Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100. | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**See**: https://core.telegram.org/bots/api#getfile | ||
### Return: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fileId | <code>String</code> | File identifier to get info about | | ||
* **Promise** | ||
<a name="TelegramBot+getFileLink"></a> | ||
### telegramBot.getFileLink(fileId) ⇒ <code>Promise</code> | ||
Get link for file. | ||
Use this method to get link for file for subsequent use. | ||
Attention: link will be valid for 1 hour. | ||
## sendLocation(chatId, latitude, longitude, [options]) | ||
This method is a sugar extension of the (getFile)[#getfilefileid] method, which returns just path to file on remote server (you will have to manually build full uri after that). | ||
Send location. | ||
Use this method to send point on the map. | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**Returns**: <code>Promise</code> - promise Promise which will have *fileURI* in resolve callback | ||
**See**: https://core.telegram.org/bots/api#getfile | ||
See: https://core.telegram.org/bots/api#sendlocation | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fileId | <code>String</code> | File identifier to get info about | | ||
### Params: | ||
<a name="TelegramBot+downloadFile"></a> | ||
### telegramBot.downloadFile(fileId, downloadDir) ⇒ <code>Promise</code> | ||
Downloads file in the specified folder. | ||
This is just a sugar for (getFile)[#getfilefiled] method | ||
* **Number|String** *chatId* Unique identifier for the message recipient | ||
* **Float** *latitude* Latitude of location | ||
* **Float** *longitude* Longitude of location | ||
* **Object** *[options]* Additional Telegram query options | ||
**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code> | ||
**Returns**: <code>Promise</code> - promise Promise, which will have *filePath* of downloaded file in resolve callback | ||
### Return: | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fileId | <code>String</code> | File identifier to get info about | | ||
| downloadDir | <code>String</code> | Absolute path to the folder in which file will be saved | | ||
* **Promise** | ||
<!-- End src/telegram.js --> | ||
* * * |
@@ -35,5 +35,6 @@ var TelegramBotWebHook = require('./telegramWebHook'); | ||
options = options || {}; | ||
this.options = options; | ||
this.token = token; | ||
this.messageTypes = [ | ||
'text', 'audio', 'document', 'photo', 'sticker', 'video', 'contact', | ||
'text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'contact', | ||
'location', 'new_chat_participant', 'left_chat_participant', 'new_chat_title', | ||
@@ -43,10 +44,10 @@ 'new_chat_photo', 'delete_chat_photo', 'group_chat_created' | ||
var processUpdate = this._processUpdate.bind(this); | ||
this.processUpdate = this._processUpdate.bind(this); | ||
if (options.polling) { | ||
this._polling = new TelegramBotPolling(token, options.polling, processUpdate); | ||
this.initPolling(); | ||
} | ||
if (options.webHook) { | ||
this._WebHook = new TelegramBotWebHook(token, options.webHook, processUpdate); | ||
this._WebHook = new TelegramBotWebHook(token, options.webHook, this.processUpdate); | ||
} | ||
@@ -57,2 +58,10 @@ }; | ||
TelegramBot.prototype.initPolling = function() { | ||
if (this._polling) { | ||
this._polling.abort = true; | ||
this._polling.lastRequest.cancel("Polling restart"); | ||
} | ||
this._polling = new TelegramBotPolling(this.token, this.options.polling, this.processUpdate); | ||
}; | ||
TelegramBot.prototype._processUpdate = function (update) { | ||
@@ -79,7 +88,3 @@ debug('Process Update %j', update); | ||
options = options || {}; | ||
options.url = URL.format({ | ||
protocol: 'https', | ||
host: 'api.telegram.org', | ||
pathname: '/bot'+this.token+'/'+path | ||
}); | ||
options.url = this._buildURL(path); | ||
debug('HTTP request: %j', options); | ||
@@ -101,2 +106,17 @@ return requestPromise(options) | ||
/** | ||
* Generates url with bot token and provided path/method you want to be got/executed by bot | ||
* @return {String} url | ||
* @param {String} path | ||
* @private | ||
* @see https://core.telegram.org/bots/api#making-requests | ||
*/ | ||
TelegramBot.prototype._buildURL = function(path) { | ||
return URL.format({ | ||
protocol: 'https', | ||
host: 'api.telegram.org', | ||
pathname: '/bot' + this.token + '/' + path | ||
}); | ||
} | ||
/** | ||
* Returns basic information about the bot in form of a `User` object. | ||
@@ -309,3 +329,23 @@ * @return {Promise} | ||
/** | ||
* Send voice | ||
* @param {Number|String} chatId Unique identifier for the message recipient | ||
* @param {String|stream.Stream} voice A file path or a Stream. Can | ||
* also be a `file_id` previously uploaded. | ||
* @param {Object} [options] Additional Telegram query options | ||
* @return {Promise} | ||
* @see https://core.telegram.org/bots/api#sendvoice | ||
*/ | ||
TelegramBot.prototype.sendVoice = function (chatId, voice, options) { | ||
var opts = { | ||
qs: options || {} | ||
}; | ||
opts.qs.chat_id = chatId; | ||
var content = this._formatSendData('voice', voice); | ||
opts.formData = content[0]; | ||
opts.qs.voice = content[1]; | ||
return this._request('sendVoice', opts); | ||
}; | ||
/** | ||
@@ -369,2 +409,68 @@ * Send chat action. | ||
/** | ||
* Get file. | ||
* Use this method to get basic info about a file and prepare it for downloading. | ||
* Attention: link will be valid for 1 hour. | ||
* | ||
* @param {String} fileId File identifier to get info about | ||
* @return {Promise} | ||
* @see https://core.telegram.org/bots/api#getfile | ||
*/ | ||
TelegramBot.prototype.getFile = function(fileId) { | ||
var query = { file_id: fileId }; | ||
return this._request('getFile', {qs: query}); | ||
}; | ||
/** | ||
* Get link for file. | ||
* Use this method to get link for file for subsequent use. | ||
* Attention: link will be valid for 1 hour. | ||
* | ||
* This method is a sugar extension of the (getFile)[#getfilefileid] method, which returns just path to file on remote server (you will have to manually build full uri after that). | ||
* | ||
* @param {String} fileId File identifier to get info about | ||
* @return {Promise} promise Promise which will have *fileURI* in resolve callback | ||
* @see https://core.telegram.org/bots/api#getfile | ||
*/ | ||
TelegramBot.prototype.getFileLink = function(fileId) { | ||
var self = this; | ||
return self.getFile(fileId).then(function (resp) { | ||
return URL.format({ | ||
protocol: 'https', | ||
host: 'api.telegram.org', | ||
pathname: '/file/bot' + self.token + '/' + resp.file_path | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Downloads file in the specified folder. | ||
* This is just a sugar for (getFile)[#getfilefiled] method | ||
* | ||
* @param {String} fileId File identifier to get info about | ||
* @param {String} downloadDir Absolute path to the folder in which file will be saved | ||
* @return {Promise} promise Promise, which will have *filePath* of downloaded file in resolve callback | ||
*/ | ||
TelegramBot.prototype.downloadFile = function(fileId, downloadDir) { | ||
return this.getFileLink(fileId).then(function (fileURI) { | ||
var fileName = fileURI.slice(fileURI.lastIndexOf('/') + 1); | ||
// TODO: Ensure fileName doesn't contains slashes | ||
var filePath = downloadDir + '/' + fileName; | ||
return new Promise(function (resolve, reject) { | ||
request({uri: fileURI}) | ||
.pipe(fs.createWriteStream(filePath)) | ||
.on('error', reject) | ||
.on('close', function() { | ||
resolve(filePath); | ||
}); | ||
}); | ||
}); | ||
} | ||
module.exports = TelegramBot; |
@@ -19,2 +19,5 @@ var debug = require('debug')('node-telegram-bot-api'); | ||
this.interval = options.interval || 2000; | ||
this.lastUpdate = 0; | ||
this.lastRequest = null; | ||
this.abort = false; | ||
this._polling(); | ||
@@ -26,3 +29,4 @@ }; | ||
this._getUpdates().then(function (updates) { | ||
this.lastRequest = this._getUpdates().then(function (updates) { | ||
self.lastUpdate = Date.now(); | ||
debug('polling data %j', updates); | ||
@@ -40,2 +44,7 @@ updates.forEach(function (update, index) { | ||
}).finally(function () { | ||
if (self.abort) { | ||
debug('Polling is aborted!'); | ||
return; | ||
} | ||
debug('setTimeout for %s miliseconds', self.interval); | ||
@@ -60,3 +69,3 @@ setTimeout(self._polling.bind(self), self.interval); | ||
debug('polling with options: %j', opts); | ||
return requestPromise(opts).then(function (resp) { | ||
return requestPromise(opts).cancellable().then(function (resp) { | ||
if (resp[0].statusCode !== 200) { | ||
@@ -63,0 +72,0 @@ throw new Error(resp[0].statusCode+' '+resp[0].body); |
@@ -333,2 +333,13 @@ var TelegramPolling = require('../src/telegramPolling'); | ||
describe('#sendVoice', function () { | ||
it('should send an OGG audio as voice', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
var voice = request('https://upload.wikimedia.org/wikipedia/commons/c/c8/Example.ogg'); | ||
bot.sendVoice(USERID, voice).then(function (resp) { | ||
resp.should.be.an.instanceOf(Object); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#getUserProfilePhotos', function () { | ||
@@ -361,2 +372,86 @@ it('should get user profile photos', function (done) { | ||
describe('#getFile', function () { | ||
var fileId; | ||
// To get a file we have to send any file first | ||
it('should send a photo from file', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
var photo = __dirname + '/bot.gif'; | ||
bot.sendPhoto(USERID, photo).then(function (resp) { | ||
resp.should.be.an.instanceOf(Object); | ||
fileId = resp.photo[0].file_id; | ||
done(); | ||
}); | ||
}); | ||
it('should get a file', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
bot.getFile(fileId).then(function (resp) { | ||
resp.should.be.an.instanceOf(Object); | ||
resp.file_path.should.be.an.instanceOf(String); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
describe('#getFileLink', function () { | ||
var fileId; | ||
// To get a file we have to send any file first | ||
it('should send a photo from file', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
var photo = __dirname + '/bot.gif'; | ||
bot.sendPhoto(USERID, photo).then(function (resp) { | ||
resp.should.be.an.instanceOf(Object); | ||
fileId = resp.photo[0].file_id; | ||
done(); | ||
}); | ||
}); | ||
it('should get a file link', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
bot.getFileLink(fileId).then(function (fileURI) { | ||
fileURI.should.be.an.instanceOf(String); | ||
fileURI.should.startWith('https'); | ||
done(); // TODO: validate URL with some library or regexp | ||
}); | ||
}); | ||
}); | ||
describe('#downloadFile', function () { | ||
var fileId; | ||
var downloadPath = __dirname; | ||
// To get a file we have to send some file first | ||
it('should send a photo from file', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
var photo = __dirname + '/bot.gif'; | ||
bot.sendPhoto(USERID, photo).then(function (resp) { | ||
resp.should.be.an.instanceOf(Object); | ||
fileId = resp.photo[0].file_id; | ||
done(); | ||
}); | ||
}); | ||
it('should download a file', function (done) { | ||
var bot = new Telegram(TOKEN); | ||
bot.downloadFile(fileId, downloadPath).then(function (filePath) { | ||
filePath.should.be.an.instanceOf(String); | ||
fs.existsSync(filePath).should.be.true(); | ||
fs.unlinkSync(filePath); // Delete file after test | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); // End Telegram | ||
@@ -363,0 +458,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
557447
20
1053
298
8