Skip to content

Commit

Permalink
Ensure consistency of methods signatures
Browse files Browse the repository at this point in the history
Bug:

  The library assumes signatures of methods to be, somewhat:

    methodName(requiredParam1, requiredParam2, form = {})

  where 'requiredParam1' ('requiredParam2', ..., 'requiredParamN')
  are parameters that MUST be provided, and
  'form' is an optional object allowing supplying any additional,
  optional parameters that the Bot API allows.

  This allows any new parameters added by Telegram to be
  readily-supported by our library.
  • Loading branch information
GochoMugo committed Dec 21, 2016
1 parent 7f30aef commit b71e553
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 48 deletions.
28 changes: 13 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ TelegramBot
* [new TelegramBot(token, [options])](#new_TelegramBot_new)
* [.stopPolling()](#TelegramBot+stopPolling) ⇒ <code>Promise</code>
* [.getMe()](#TelegramBot+getMe) ⇒ <code>Promise</code>
* [.setWebHook(url, [cert])](#TelegramBot+setWebHook)
* [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
* [.setWebHook(url, [options])](#TelegramBot+setWebHook)
* [.getUpdates([options])](#TelegramBot+getUpdates) ⇒ <code>Promise</code>
* [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ <code>Promise</code>
* [.answerInlineQuery(inlineQueryId, results, [options])](#TelegramBot+answerInlineQuery) ⇒ <code>Promise</code>
* [.forwardMessage(chatId, fromChatId, messageId)](#TelegramBot+forwardMessage) ⇒ <code>Promise</code>
* [.forwardMessage(chatId, fromChatId, messageId, [options])](#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, doc, [options], [fileOpts])](#TelegramBot+sendDocument) ⇒ <code>Promise</code>
Expand All @@ -91,7 +91,7 @@ TelegramBot
* [.editMessageText(text, [options])](#TelegramBot+editMessageText) ⇒ <code>Promise</code>
* [.editMessageCaption(caption, [options])](#TelegramBot+editMessageCaption) ⇒ <code>Promise</code>
* [.editMessageReplyMarkup(replyMarkup, [options])](#TelegramBot+editMessageReplyMarkup) ⇒ <code>Promise</code>
* [.getUserProfilePhotos(userId, [offset], [limit])](#TelegramBot+getUserProfilePhotos) ⇒ <code>Promise</code>
* [.getUserProfilePhotos(userId, [options])](#TelegramBot+getUserProfilePhotos) ⇒ <code>Promise</code>
* [.sendLocation(chatId, latitude, longitude, [options])](#TelegramBot+sendLocation) ⇒ <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>
Expand Down Expand Up @@ -146,7 +146,7 @@ Returns basic information about the bot in form of a `User` object.
**See**: https://core.telegram.org/bots/api#getme
<a name="TelegramBot+setWebHook"></a>

### telegramBot.setWebHook(url, [cert])
### telegramBot.setWebHook(url, [options])
Specify an url to receive incoming updates via an outgoing webHook.

**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
Expand All @@ -155,22 +155,20 @@ Specify an url to receive incoming updates via an outgoing webHook.
| Param | Type | Description |
| --- | --- | --- |
| url | <code>String</code> | URL where Telegram will make HTTP Post. Leave empty to delete webHook. |
| [cert] | <code>String</code> &#124; <code>stream.Stream</code> | PEM certificate key (public). |
| [options] | <code>Object</code> | Additional Telegram query options |
| [options.certificate] | <code>String</code> &#124; <code>stream.Stream</code> | PEM certificate key (public). |

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

### telegramBot.getUpdates([timeout], [limit], [offset]) ⇒ <code>Promise</code>
### telegramBot.getUpdates([options]) ⇒ <code>Promise</code>
Use this method to receive incoming updates using long polling

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

| Param | Type | Description |
| --- | --- | --- |
| [timeout] | <code>Number</code> &#124; <code>String</code> | Timeout in seconds for long polling. |
| [limit] | <code>Number</code> &#124; <code>String</code> | Limits the number of updates to be retrieved. |
| [offset] | <code>Number</code> &#124; <code>String</code> | Identifier of the first update to be returned. |
| [options] | <code>Object</code> | Additional Telegram query options |

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

Expand Down Expand Up @@ -202,7 +200,7 @@ Send answers to an inline query.

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

### telegramBot.forwardMessage(chatId, fromChatId, messageId) ⇒ <code>Promise</code>
### telegramBot.forwardMessage(chatId, fromChatId, messageId, [options]) ⇒ <code>Promise</code>
Forward messages of any kind.

**Kind**: instance method of <code>[TelegramBot](#TelegramBot)</code>
Expand All @@ -212,6 +210,7 @@ Forward messages of any kind.
| chatId | <code>Number</code> &#124; <code>String</code> | Unique identifier for the message recipient |
| fromChatId | <code>Number</code> &#124; <code>String</code> | Unique identifier for the chat where the original message was sent |
| messageId | <code>Number</code> &#124; <code>String</code> | Unique message identifier |
| [options] | <code>Object</code> | Additional Telegram query options |

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

Expand Down Expand Up @@ -422,7 +421,7 @@ inline_message_id in your request.

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

### telegramBot.getUserProfilePhotos(userId, [offset], [limit]) ⇒ <code>Promise</code>
### telegramBot.getUserProfilePhotos(userId, [options]) ⇒ <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.

Expand All @@ -432,8 +431,7 @@ Returns a [UserProfilePhotos](https://core.telegram.org/bots/api#userprofilephot
| Param | Type | Description |
| --- | --- | --- |
| userId | <code>Number</code> &#124; <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. |
| [options] | <code>Object</code> | Additional Telegram query options |

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

Expand Down
90 changes: 57 additions & 33 deletions src/telegram.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class TelegramBot extends EventEmitter {
}
} else if (channelPost) {
debug('Process Update channel_post %j', channelPost);
this.emit('channel_post', channelPost);
this.emit('channel_post', channelPost);
} else if (editedChannelPost) {
debug('Process Update edited_channel_post %j', editedChannelPost);
this.emit('edited_channel_post', editedChannelPost);
Expand All @@ -148,7 +148,7 @@ class TelegramBot extends EventEmitter {
}
if (editedChannelPost.caption) {
this.emit('edited_channel_post_caption', editedChannelPost);
}
}
} else if (inlineQuery) {
debug('Process Update inline_query %j', inlineQuery);
this.emit('inline_query', inlineQuery);
Expand Down Expand Up @@ -241,22 +241,37 @@ class TelegramBot extends EventEmitter {

/**
* Specify an url to receive incoming updates via an outgoing webHook.
* @param {String} url URL where Telegram will make HTTP Post. Leave empty to
* @param {String} url URL where Telegram will make HTTP Post. Leave empty to
* delete webHook.
* @param {String|stream.Stream} [cert] PEM certificate key (public).
* @param {Object} [options] Additional Telegram query options
* @param {String|stream.Stream} [options.certificate] PEM certificate key (public).
* @see https://core.telegram.org/bots/api#setwebhook
*/
setWebHook(url, cert) {
const _path = 'setWebHook';
const opts = { qs: { url } };
setWebHook(url, options = {}) {
/* The older method signature was setWebHook(url, cert).
* We need to ensure backwards-compatibility while maintaining
* consistency of the method signatures throughout the library */
let cert;
// Note: 'options' could be an object, if a stream was provided (in place of 'cert')
if (typeof options !== 'object' || options instanceof stream.Stream) {
cert = options;
options = {}; // eslint-disable-line no-param-reassign
} else {
cert = options.certificate;
}

const opts = {
qs: options,
};
opts.qs.url = url;

if (cert) {
const [formData, certificate] = this._formatSendData('certificate', cert);
opts.formData = formData;
opts.qs.certificate = certificate;
}

return this._request(_path, opts)
return this._request('setWebHook', opts)
.then(resp => {
if (!resp) {
throw new Error(resp);
Expand All @@ -268,18 +283,23 @@ class TelegramBot extends EventEmitter {

/**
* Use this method to receive incoming updates using long polling
* @param {Number|String} [timeout] Timeout in seconds for long polling.
* @param {Number|String} [limit] Limits the number of updates to be retrieved.
* @param {Number|String} [offset] Identifier of the first update to be returned.
* @return {Promise} Updates
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#getupdates
*/
getUpdates(timeout, limit, offset) {
const form = {
offset,
limit,
timeout,
};
getUpdates(form = {}) {
/* The older method signature was getUpdates(timeout, limit, offset).
* We need to ensure backwards-compatibility while maintaining
* consistency of the method signatures throughout the library */
if (typeof form !== 'object') {
/* eslint-disable no-param-reassign, prefer-rest-params */
form = {
timeout: arguments[0],
limit: arguments[1],
offset: arguments[2],
};
/* eslint-enable no-param-reassign, prefer-rest-params */
}

return this._request('getUpdates', { form });
}
Expand Down Expand Up @@ -318,15 +338,13 @@ class TelegramBot extends EventEmitter {
* @param {Number|String} fromChatId Unique identifier for the chat where the
* original message was sent
* @param {Number|String} messageId Unique message identifier
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
*/
forwardMessage(chatId, fromChatId, messageId) {
const form = {
chat_id: chatId,
from_chat_id: fromChatId,
message_id: messageId
};

forwardMessage(chatId, fromChatId, messageId, form = {}) {
form.chat_id = chatId;
form.from_chat_id = fromChatId;
form.message_id = messageId;
return this._request('forwardMessage', { form });
}

Expand Down Expand Up @@ -636,17 +654,23 @@ class TelegramBot extends EventEmitter {
* Returns a [UserProfilePhotos](https://core.telegram.org/bots/api#userprofilephotos) object.
*
* @param {Number|String} userId Unique identifier of the target user
* @param {Number} [offset] Sequential number of the first photo to be returned. By default, all photos are returned.
* @param {Number} [limit] Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100.
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#getuserprofilephotos
*/
getUserProfilePhotos(userId, offset, limit) {
const form = {
user_id: userId,
offset,
limit
};
getUserProfilePhotos(userId, form = {}) {
/* The older method signature was getUserProfilePhotos(userId, offset, limit).
* We need to ensure backwards-compatibility while maintaining
* consistency of the method signatures throughout the library */
if (typeof form !== 'object') {
/* eslint-disable no-param-reassign, prefer-rest-params */
form = {
offset: arguments[1],
limit: arguments[2],
};
/* eslint-enable no-param-reassign, prefer-rest-params */
}
form.user_id = userId;
return this._request('getUserProfilePhotos', { form });
}

Expand Down

0 comments on commit b71e553

Please sign in to comment.