Skip to content

Commit

Permalink
src: Support Bot API v4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
kamikazechaser authored and GochoMugo committed May 7, 2019
1 parent 0b476fe commit f1a044e
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -5,6 +5,11 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased][Unreleased]

Added:

1. Support Bot API v4.2: (by @kamikazechaser)
* Add methods: *TelegramBot#sendPoll()*, *TelegramBot#stopPoll()*
* Support events: *poll*


* * *
Expand Down
35 changes: 34 additions & 1 deletion doc/api.md
Expand Up @@ -61,6 +61,8 @@ TelegramBot
* [.stopMessageLiveLocation([options])](#TelegramBot+stopMessageLiveLocation) ⇒ <code>Promise</code>
* [.sendVenue(chatId, latitude, longitude, title, address, [options])](#TelegramBot+sendVenue) ⇒ <code>Promise</code>
* [.sendContact(chatId, phoneNumber, firstName, [options])](#TelegramBot+sendContact) ⇒ <code>Promise</code>
* [.sendPoll(chatId, question, pollOptions, [options])](#TelegramBot+sendPoll) ⇒ <code>Promise</code>
* [.stopPoll(chatId, pollId, [options])](#TelegramBot+stopPoll) ⇒ <code>Promise</code>
* [.getFile(fileId, [options])](#TelegramBot+getFile) ⇒ <code>Promise</code>
* [.getFileLink(fileId, [options])](#TelegramBot+getFileLink) ⇒ <code>Promise</code>
* [.getFileStream(fileId, [options])](#TelegramBot+getFileStream) ⇒ <code>stream.Readable</code>
Expand Down Expand Up @@ -393,7 +395,7 @@ Send Document
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**

- https://core.telegram.org/bots/api#senddocument
- https://core.telegram.org/bots/api#sendDocument
- https://github.com/yagop/node-telegram-bot-api/blob/master/doc/usage.md#sending-files


Expand Down Expand Up @@ -886,6 +888,37 @@ Use this method to send phone contacts.
| firstName | <code>String</code> | Contact's first name |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+sendPoll"></a>

### telegramBot.sendPoll(chatId, question, pollOptions, [options]) ⇒ <code>Promise</code>
Send poll.
Use this method to send a native poll.

**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#sendpoll

| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the group/channel |
| question | <code>String</code> | Poll question, 255 char limit |
| pollOptions | <code>Array</code> | Poll options, between 2-10 options |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+stopPoll"></a>

### telegramBot.stopPoll(chatId, pollId, [options]) ⇒ <code>Promise</code>
Stop poll.
Use this method to stop a native poll.

**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#stoppoll

| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the group/channel |
| pollId | <code>Number</code> | Identifier of the original message with the poll |
| [options] | <code>Object</code> | Additional Telegram query options |

<a name="TelegramBot+getFile"></a>

### telegramBot.getFile(fileId, [options]) ⇒ <code>Promise</code>
Expand Down
3 changes: 2 additions & 1 deletion doc/usage.md
Expand Up @@ -17,7 +17,7 @@ that emits the following events:
`sticker`, `video`, `voice`, `contact`, `location`,
`new_chat_members`, `left_chat_member`, `new_chat_title`,
`new_chat_photo`, `delete_chat_photo`, `group_chat_created`,
`game`, `pinned_message`, `migrate_from_chat_id`, `migrate_to_chat_id`,
`game`, `pinned_message`, `poll`, `migrate_from_chat_id`, `migrate_to_chat_id`,
`channel_chat_created`, `supergroup_chat_created`,
`successful_payment`, `invoice`, `video_note`
1. **Arguments**: `message` ([Message][message]), `metadata` (`{ type?:string }`)
Expand All @@ -34,6 +34,7 @@ that emits the following events:
1. `edited_channel_post_caption`
1. `shipping_query`: Received a new incoming shipping query
1. `pre_checkout_query`: Received a new incoming pre-checkout query
1. `poll`: Received a new incoming poll
1. `polling_error`: Error occurred during polling. See [polling errors](#polling-errors).
1. `webhook_error`: Error occurred handling a webhook request. See [webhook errors](#webhook-errors).
1. `error`: Unexpected error occurred, usually fatal!
Expand Down
38 changes: 38 additions & 0 deletions src/telegram.js
Expand Up @@ -40,6 +40,7 @@ const _messageTypes = [
'passport_data',
'photo',
'pinned_message',
'poll',
'sticker',
'successful_payment',
'supergroup_chat_created',
Expand Down Expand Up @@ -586,6 +587,7 @@ class TelegramBot extends EventEmitter {
const callbackQuery = update.callback_query;
const shippingQuery = update.shipping_query;
const preCheckoutQuery = update.pre_checkout_query;
const poll = update.poll;

if (message) {
debug('Process Update message %j', message);
Expand Down Expand Up @@ -663,6 +665,9 @@ class TelegramBot extends EventEmitter {
} else if (preCheckoutQuery) {
debug('Process Update pre_checkout_query %j', preCheckoutQuery);
this.emit('pre_checkout_query', preCheckoutQuery);
} else if (poll) {
debug('Process Update poll %j', poll);
this.emit('poll', poll);
}
}

Expand Down Expand Up @@ -1370,6 +1375,39 @@ class TelegramBot extends EventEmitter {
return this._request('sendContact', { form });
}

/**
* Send poll.
* Use this method to send a native poll.
*
* @param {Number|String} chatId Unique identifier for the group/channel
* @param {String} question Poll question, 255 char limit
* @param {Array} pollOptions Poll options, between 2-10 options
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#sendpoll
*/
sendPoll(chatId, question, pollOptions, form = {}) {
form.chat_id = chatId;
form.question = question;
form.options = stringify(pollOptions);
return this._request('sendPoll', { form });
}

/**
* Stop poll.
* Use this method to stop a native poll.
*
* @param {Number|String} chatId Unique identifier for the group/channel
* @param {Number} pollId Identifier of the original message with the poll
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#stoppoll
*/
stopPoll(chatId, pollId, form = {}) {
form.chat_id = chatId;
form.message_id = pollId;
return this._request('stopPoll', { form });
}

/**
* Get file.
Expand Down

0 comments on commit f1a044e

Please sign in to comment.