diff --git a/README.md b/README.md index a9d5bd6..a3a25ba 100644 --- a/README.md +++ b/README.md @@ -60,7 +60,6 @@ TelegramBot * [new TelegramBot(token, [options])](#new_TelegramBot_new) * [.stopPolling()](#TelegramBot+stopPolling) ⇒ Promise * [.getMe()](#TelegramBot+getMe) ⇒ Promise - * [.getChat(chatId)](#TelegramBot+getChat) ⇒ Promise * [.setWebHook(url, [cert])](#TelegramBot+setWebHook) * [.getUpdates([timeout], [limit], [offset])](#TelegramBot+getUpdates) ⇒ Promise * [.sendMessage(chatId, text, [options])](#TelegramBot+sendMessage) ⇒ Promise @@ -87,6 +86,10 @@ TelegramBot * [.downloadFile(fileId, downloadDir)](#TelegramBot+downloadFile) ⇒ Promise * [.onText(regexp, callback)](#TelegramBot+onText) * [.onReplyToMessage(chatId, messageId, callback)](#TelegramBot+onReplyToMessage) + * [.getChat(chatId)](#TelegramBot+getChat) ⇒ Promise + * [.getChatAdministrators(chatId)](#TelegramBot+getChatAdministrators) ⇒ Promise + * [.getChatMembersCount(chatId)](#TelegramBot+getChatMembersCount) ⇒ Promise + * [.getChatMember(chatId, userId)](#TelegramBot+getChatMember) ⇒ Promise @@ -121,20 +124,6 @@ Returns basic information about the bot in form of a `User` object. **Kind**: instance method of [TelegramBot](#TelegramBot) **See**: https://core.telegram.org/bots/api#getme - - -### telegramBot.getChat(chatId) ⇒ Promise -Use this method to get up to date information about the chat -(current name of the user for one-on-one conversations, current -username of a user, group or channel, etc.). - -**Kind**: instance method of [TelegramBot](#TelegramBot) -**See**: https://core.telegram.org/bots/api#getchat - -| Param | Type | Description | -| --- | --- | --- | -| chatId | Number | String | Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername) | - ### telegramBot.setWebHook(url, [cert]) @@ -531,4 +520,55 @@ Register a reply to wait for a message response. | messageId | Number | String | The message id to be replied. | | callback | function | Callback will be called with the reply message. | + + +### telegramBot.getChat(chatId) ⇒ Promise +Use this method to get up to date information about the chat +(current name of the user for one-on-one conversations, current +username of a user, group or channel, etc.). + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#getchat + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number | String | Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername) | + + + +### telegramBot.getChatAdministrators(chatId) ⇒ Promise +Returns the administrators in a chat in form of an Array of `ChatMember` objects. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#getchatadministrators + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number | String | Unique identifier for the target group or username of the target supergroup | + + + +### telegramBot.getChatMembersCount(chatId) ⇒ Promise +Use this method to get the number of members in a chat. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#getchatmemberscount + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number | String | Unique identifier for the target group or username of the target supergroup | + + + +### telegramBot.getChatMember(chatId, userId) ⇒ Promise +Use this method to get information about a member of a chat. + +**Kind**: instance method of [TelegramBot](#TelegramBot) +**See**: https://core.telegram.org/bots/api#getchatmember + +| Param | Type | Description | +| --- | --- | --- | +| chatId | Number | String | Unique identifier for the target group or username of the target supergroup | +| userId | String | Unique identifier of the target user | + * * * diff --git a/src/telegram.js b/src/telegram.js index 49eaacb..efcc0c1 100644 --- a/src/telegram.js +++ b/src/telegram.js @@ -205,21 +205,6 @@ class TelegramBot extends EventEmitter { return this._request(_path); } - /** - * Use this method to get up to date information about the chat - * (current name of the user for one-on-one conversations, current - * username of a user, group or channel, etc.). - * @param {Number|String} chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername) - * @return {Promise} - * @see https://core.telegram.org/bots/api#getchat - */ - getChat(chatId) { - const form = { - chat_id: chatId - }; - return this._request('getChat', { form }); - } - /** * Specify an url to receive incoming updates via an outgoing webHook. * @param {String} url URL where Telegram will make HTTP Post. Leave empty to @@ -755,6 +740,62 @@ class TelegramBot extends EventEmitter { callback }); } + + /** + * Use this method to get up to date information about the chat + * (current name of the user for one-on-one conversations, current + * username of a user, group or channel, etc.). + * @param {Number|String} chatId Unique identifier for the target chat or username of the target supergroup or channel (in the format @channelusername) + * @return {Promise} + * @see https://core.telegram.org/bots/api#getchat + */ + getChat(chatId) { + const form = { + chat_id: chatId + }; + return this._request('getChat', { form }); + } + + /** + * Returns the administrators in a chat in form of an Array of `ChatMember` objects. + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup + * @return {Promise} + * @see https://core.telegram.org/bots/api#getchatadministrators + */ + getChatAdministrators(chatId) { + const form = { + chat_id: chatId + }; + return this._request('getChatAdministrators', { form }); + } + + /** + * Use this method to get the number of members in a chat. + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup + * @return {Promise} + * @see https://core.telegram.org/bots/api#getchatmemberscount + */ + getChatMembersCount(chatId) { + const form = { + chat_id: chatId + }; + return this._request('getChatMembersCount', { form }); + } + + /** + * Use this method to get information about a member of a chat. + * @param {Number|String} chatId Unique identifier for the target group or username of the target supergroup + * @param {String} userId Unique identifier of the target user + * @return {Promise} + * @see https://core.telegram.org/bots/api#getchatmember + */ + getChatMember(chatId, userId) { + const form = { + chat_id: chatId, + user_id: userId + }; + return this._request('getChatMember', { form }); + } } module.exports = TelegramBot; diff --git a/test/README.md b/test/README.md index a34547c..4e8f583 100644 --- a/test/README.md +++ b/test/README.md @@ -4,5 +4,7 @@ Running the tests: export TEST_TELEGRAM_TOKEN= # User Id which you want to send the messages. export TEST_USER_ID= +# Group Id which to use in some of the tests, e.g. for TelegramBot#getChat() +export TEST_GROUP_ID= npm run test ``` diff --git a/test/index.js b/test/index.js index cf4b0a4..4c04789 100644 --- a/test/index.js +++ b/test/index.js @@ -14,6 +14,7 @@ if (!TOKEN) { // Telegram service if not User Id const USERID = process.env.TEST_USER_ID || 777000; +const GROUPID = process.env.TEST_GROUP_ID || -1001075450562; describe('Telegram', function telegramSuite() { describe('#setWebHook', function setWebHookSuite() { @@ -140,6 +141,34 @@ describe('Telegram', function telegramSuite() { }); }); + describe('#getChatAdministrators', function getChatAdministratorsSuite() { + it('should return an Array', function test() { + const bot = new Telegram(TOKEN); + return bot.getChatAdministrators(GROUPID).then(resp => { + assert.ok(Array.isArray(resp)); + }); + }); + }); + + describe('#getChatMembersCount', function getChatMembersCountSuite() { + it('should return an Integer', function test() { + const bot = new Telegram(TOKEN); + return bot.getChatMembersCount(GROUPID).then(resp => { + assert.ok(Number.isInteger(resp)); + }); + }); + }); + + describe('#getChatMember', function getChatMemberSuite() { + it('should return a ChatMember', function test() { + const bot = new Telegram(TOKEN); + return bot.getChatMember(GROUPID, USERID).then(resp => { + assert.ok(is.object(resp.user)); + assert.ok(is.string(resp.status)); + }); + }); + }); + describe('#getUpdates', function getUpdatesSuite() { it('should return an Array', function test() { const bot = new Telegram(TOKEN);