2
0
mirror of https://github.com/yagop/node-telegram-bot-api synced 2025-08-22 09:57:10 +00:00

Add new method sendDice, getMyCommands, setMyCommands and more (#796)

* Add new method sendPoll

* getMyCommands

* setMyCommands

* Update doc/api

* 1# Fix Test sendPhoto

The sendPhoto method does not support .gif files, use sendAnimation.

This fix remplace .gif file by .png

* CustomTitle and ChatPermissions support

setChatAdministratorCustomTitle
setChatPermissions

* Update Readme

Update Badge Telegram API Version

* Minor Fix and Fix setChatPhoto

The Telegram Bots api has a bug that they are fixing and gives problems with the previous image. While they don't fix it, the image replacement is the fastest solution

* Minor Fix and Add Test

Fixed setChatPermissions

Test:
- sendDice
- getMyCommands
- setMyCommands
- setChatAdministratorCustomTitle
- setChatPermissions

* Update Changelog and Package.json version

* Fix typos in Changelog

* Add support for poll_answer

From: https://github.com/yagop/node-telegram-bot-api/pull/777

* Add JieJiSS contribution in Changelog

* Add sendPoll Test
This commit is contained in:
Daniel Pérez Fernández 2020-05-12 06:01:30 +02:00 committed by GitHub
parent c6a0eedc9a
commit 1bae9c2964
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 447 additions and 177 deletions

View File

@ -3,7 +3,29 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).
## [0.40.0][0.40.0] - 2019-10-15
## [0.50.0][0.50.0] - 2020-05-2020
1. Support Bot API v4.8: (by @danielperez9430)
* Add methods: *sendDice()*
1. Support Bot API v4.7: (by @danielperez9430)
* Add methods: *getMyCommands()*,*setMyCommands()*
1. Support Bot API v4.5: (by @danielperez9430)
* Add methods: *setChatAdministratorCustomTitle()*
1. Support Bot API v4.4: (by @danielperez9430)
* Add methods: *setChatPermissions()*
1. Support for poll_answer (by @JieJiSS)
Changed: (by @danielperez9430)
* New message type: *dice*
* Fix Bugs in tests
New Test: (by @danielperez9430)
* sendDice
* getMyCommands
* setMyCommands
* setChatAdministratorCustomTitle
* setChatPermissions
## [0.40.0][0.40.0] - 2019-05-04
Added:

View File

@ -4,7 +4,7 @@
Node.js module to interact with the official [Telegram Bot API](https://core.telegram.org/bots/api).
[![Bot API](https://img.shields.io/badge/Bot%20API-v.4.4.0-00aced.svg?style=flat-square&logo=telegram)](https://core.telegram.org/bots/api)
[![Bot API](https://img.shields.io/badge/Bot%20API-v.4.8.0-00aced.svg?style=flat-square&logo=telegram)](https://core.telegram.org/bots/api)
[![npm package](https://img.shields.io/npm/v/node-telegram-bot-api?logo=npm&style=flat-square)](https://www.npmjs.org/package/node-telegram-bot-api)
[![Build Status](https://img.shields.io/travis/yagop/node-telegram-bot-api/master?style=flat-square&logo=travis)](https://travis-ci.org/yagop/node-telegram-bot-api)
[![Coverage Status](https://img.shields.io/codecov/c/github/yagop/node-telegram-bot-api?style=flat-square&logo=codecov)](https://codecov.io/gh/yagop/node-telegram-bot-api)

View File

@ -43,6 +43,8 @@ TelegramBot
* [.unbanChatMember(chatId, userId, [options])](#TelegramBot+unbanChatMember) ⇒ <code>Promise</code>
* [.restrictChatMember(chatId, userId, [options])](#TelegramBot+restrictChatMember) ⇒ <code>Promise</code>
* [.promoteChatMember(chatId, userId, [options])](#TelegramBot+promoteChatMember) ⇒ <code>Promise</code>
* [.setChatAdministratorCustomTitle(chatId, userId, customTitle, [options])](#TelegramBot+setChatAdministratorCustomTitle) ⇒ <code>Promise</code>
* [.setChatPermissions(chatId, ChatPermissions, [options])](#TelegramBot+setChatPermissions) ⇒ <code>Promise</code>
* [.exportChatInviteLink(chatId, [options])](#TelegramBot+exportChatInviteLink) ⇒ <code>Promise</code>
* [.setChatPhoto(chatId, photo, [options], [fileOptions])](#TelegramBot+setChatPhoto) ⇒ <code>Promise</code>
* [.deleteChatPhoto(chatId, [options])](#TelegramBot+deleteChatPhoto) ⇒ <code>Promise</code>
@ -51,6 +53,8 @@ TelegramBot
* [.pinChatMessage(chatId, messageId, [options])](#TelegramBot+pinChatMessage) ⇒ <code>Promise</code>
* [.unpinChatMessage(chatId, [options])](#TelegramBot+unpinChatMessage) ⇒ <code>Promise</code>
* [.answerCallbackQuery(callbackQueryId, [options])](#TelegramBot+answerCallbackQuery) ⇒ <code>Promise</code>
* [.setMyCommands(commands, [options])](#TelegramBot+setMyCommands) ⇒ <code>Promise</code>
* [.getMyCommands([options])](#TelegramBot+getMyCommands) ⇒ <code>Promise</code>
* [.editMessageText(text, [options])](#TelegramBot+editMessageText) ⇒ <code>Promise</code>
* [.editMessageCaption(caption, [options])](#TelegramBot+editMessageCaption) ⇒ <code>Promise</code>
* [.editMessageMedia(media, [options])](#TelegramBot+editMessageMedia) ⇒ <code>Promise</code>
@ -63,6 +67,7 @@ TelegramBot
* [.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>
* [.sendDice(chatId, [options])](#TelegramBot+sendDice) ⇒ <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>
@ -585,6 +590,38 @@ Returns True on success.
| userId | <code>Number</code> | |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+setChatAdministratorCustomTitle"></a>
### telegramBot.setChatAdministratorCustomTitle(chatId, userId, customTitle, [options]) ⇒ <code>Promise</code>
Use this method to set a custom title for an administrator in a supergroup promoted by the bot.
Returns True on success.
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#setchatadministratorcustomtitle
| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Integer or String</code> \| <code>String</code> | Unique identifier for the target chat or username of the target supergroup |
| userId | <code>Integer</code> | Unique identifier of the target user |
| customTitle | <code>String</code> | New custom title for the administrator; 0-16 characters, emoji are not allowed |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+setChatPermissions"></a>
### telegramBot.setChatPermissions(chatId, ChatPermissions, [options]) ⇒ <code>Promise</code>
Use this method to set default chat permissions for all members. The bot must be an administrator
in the group or a supergroup for this to work and must have the can_restrict_members admin rights.
Returns True on success.
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#setchatpermissions
| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Integer or String</code> \| <code>String</code> | Unique identifier for the target chat or username of the target supergroup |
| ChatPermissions | <code>Object</code> | New default chat permissions |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+exportChatInviteLink"></a>
### telegramBot.exportChatInviteLink(chatId, [options]) ⇒ <code>Promise</code>
@ -714,6 +751,33 @@ that are being deprecated.
| callbackQueryId | <code>String</code> | Unique identifier for the query to be answered |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+getMyCommands"></a>
### telegramBot.getMyCommands([options]) ⇒ <code>Promise</code>
Use this method to get the current list of the bot's commands
Returns True on success.Returns Array of BotCommand on success.
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#getmycommands
| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+setMyCommands"></a>
### telegramBot.setMyCommands(commands, [options]) ⇒ <code>Promise</code>
Use this method to change the list of the bot's commands.
Returns True on success.
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#setmycommands
| Param | Type | Description |
| --- | --- | --- |
| commands | <code>Array of BotCommand</code> | A JSON-serialized list of bot commands to be set as the list of the bot's commands. At most 100 commands can be specified. |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+editMessageText"></a>
### telegramBot.editMessageText(text, [options]) ⇒ <code>Promise</code>
@ -919,6 +983,20 @@ Use this method to stop a native poll.
| pollId | <code>Number</code> | Identifier of the original message with the poll |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+sendDice"></a>
### telegramBot.sendDice(chatId, [options]) ⇒ <code>Promise</code>
Send Dice.
Use this method to send a dice.
**Kind**: instance method of [<code>TelegramBot</code>](#TelegramBot)
**See**: https://core.telegram.org/bots/api#senddice
| Param | Type | Description |
| --- | --- | --- |
| chatId | <code>Number</code> \| <code>String</code> | Unique identifier for the group/channel |
| [options] | <code>Object</code> | Additional Telegram query options |
<a name="TelegramBot+getFile"></a>
### telegramBot.getFile(fileId, [options]) ⇒ <code>Promise</code>

View File

@ -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`, `poll`, `migrate_from_chat_id`, `migrate_to_chat_id`,
`game`, `pinned_message`, `poll`, `dice`, `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 }`)

View File

@ -1,6 +1,6 @@
{
"name": "node-telegram-bot-api",
"version": "0.40.0",
"version": "0.50.0",
"description": "Telegram Bot API",
"main": "./index.js",
"directories": {

View File

@ -26,6 +26,7 @@ const _messageTypes = [
'channel_chat_created',
'contact',
'delete_chat_photo',
'dice',
'document',
'game',
'group_chat_created',
@ -588,6 +589,7 @@ class TelegramBot extends EventEmitter {
const shippingQuery = update.shipping_query;
const preCheckoutQuery = update.pre_checkout_query;
const poll = update.poll;
const pollAnswer = update.poll_answer;
if (message) {
debug('Process Update message %j', message);
@ -668,6 +670,9 @@ class TelegramBot extends EventEmitter {
} else if (poll) {
debug('Process Update poll %j', poll);
this.emit('poll', poll);
} else if (pollAnswer) {
debug('Process Update poll_answer %j', pollAnswer);
this.emit('poll_answer', pollAnswer);
}
}
@ -767,6 +772,28 @@ class TelegramBot extends EventEmitter {
return this._request('sendAudio', opts);
}
/**
* Send Dice
* Use this method to send a dice.
* @param {Number|String} chatId Unique identifier for the message recipient
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#senddice
*/
sendDice(chatId, options = {}) {
const opts = {
qs: options,
};
opts.qs.chat_id = chatId;
try {
const sendData = this._formatSendData('dice');
opts.formData = sendData[0];
} catch (ex) {
return Promise.reject(ex);
}
return this._request('sendDice', opts);
}
/**
* Send Document
* @param {Number|String} chatId Unique identifier for the message recipient
@ -1016,6 +1043,42 @@ class TelegramBot extends EventEmitter {
return this._request('promoteChatMember', { form });
}
/**
* Use this method to set a custom title for an administrator in a supergroup promoted by the bot.
* Returns True on success.
*
* @param {Number|String} chatId Unique identifier for the message recipient
* @param {Number} userId Unique identifier of the target user
* @param {String} customTitle New custom title for the administrator; 0-16 characters, emoji are not allowed
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#setchatadministratorcustomtitle
*/
setChatAdministratorCustomTitle(chatId, userId, customTitle, form = {}) {
form.chat_id = chatId;
form.user_id = userId;
form.custom_title = customTitle;
return this._request('setChatAdministratorCustomTitle', { form });
}
/**
* Use this method to set default chat permissions for all members.
* The bot must be an administrator in the group or a supergroup for this to
* work and must have the can_restrict_members admin rights.
* Returns True on success.
*
* @param {Number|String} chatId Unique identifier for the message recipient
* @param {Array} chatPermissions New default chat permissions
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#setchatpermissions
*/
setChatPermissions(chatId, chatPermissions, form = {}) {
form.chat_id = chatId;
form.permissions = JSON.stringify(chatPermissions);
return this._request('setChatPermissions', { form });
}
/**
* Use this method to export an invite link to a supergroup or a channel.
* The bot must be an administrator in the chat for this to work and must have the appropriate admin rights.
@ -1181,6 +1244,29 @@ class TelegramBot extends EventEmitter {
return this._request('answerCallbackQuery', { form });
}
/**
* Returns True on success.
* Use this method to change the list of the bot's commands.
* @param {Array} commands Poll options, between 2-10 options
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#setmycommands
*/
setMyCommands(commands, form = {}) {
form.commands = stringify(commands);
return this._request('setMyCommands', { form });
}
/**
* Returns Array of BotCommand on success.
* @param {Object} [options] Additional Telegram query options
* @return {Promise}
* @see https://core.telegram.org/bots/api#getmycommands
*/
getMyCommands(form = {}) {
return this._request('getMyCommands', { form });
}
/**
* Use this method to edit text messages sent by the bot or via
* the bot (for inline bots). On success, the edited Message is

Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

After

Width:  |  Height:  |  Size: 14 KiB

BIN
test/data/photo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

View File

@ -43,7 +43,7 @@ const cert = `${__dirname}/../examples/ssl/crt.pem`;
const ip = '216.58.210.174'; // Google IP ¯\_(ツ)_/¯
const lat = 47.5351072;
const long = -52.7508537;
const FILE_PATH = `${__dirname}/data/photo.gif`;
const FILE_PATH = `${__dirname}/data/photo.png`;
let FILE_ID;
let GAME_CHAT_ID;
let GAME_MSG_ID;
@ -507,7 +507,7 @@ describe('TelegramBot', function telegramSuite() {
utils.handleRatelimit(bot, 'sendPhoto', this);
});
it('should send a photo from file', function test() {
const photo = `${__dirname}/data/photo.gif`;
const photo = `${__dirname}/data/photo.png`;
return bot.sendPhoto(USERID, photo).then(resp => {
assert.ok(is.object(resp));
assert.ok(is.array(resp.photo));
@ -523,21 +523,21 @@ describe('TelegramBot', function telegramSuite() {
});
});
it('should send a photo from fs.readStream', function test() {
const photo = fs.createReadStream(`${__dirname}/data/photo.gif`);
const photo = fs.createReadStream(`${__dirname}/data/photo.png`);
return bot.sendPhoto(USERID, photo).then(resp => {
assert.ok(is.object(resp));
assert.ok(is.array(resp.photo));
});
});
it('should send a photo from request Stream', function test() {
const photo = request(`${staticUrl}/photo.gif`);
const photo = request(`${staticUrl}/photo.png`);
return bot.sendPhoto(USERID, photo).then(resp => {
assert.ok(is.object(resp));
assert.ok(is.array(resp.photo));
});
});
it('should send a photo from a Buffer', function test() {
const photo = fs.readFileSync(`${__dirname}/data/photo.gif`);
const photo = fs.readFileSync(`${__dirname}/data/photo.png`);
return bot.sendPhoto(USERID, photo).then(resp => {
assert.ok(is.object(resp));
assert.ok(is.array(resp.photo));
@ -827,6 +827,52 @@ describe('TelegramBot', function telegramSuite() {
describe.skip('#answerCallbackQuery', function answerCallbackQuerySuite() { });
describe('#setMyCommands', function setMyCommandsSuite() {
it('should set bot commands', function test() {
const opts = [
{ command: 'eat', description: 'Command for eat' },
{ command: 'run', description: 'Command for run' }
];
return bot.setMyCommands(opts).then(resp => {
assert.ok(is.boolean(resp));
});
});
});
describe('#getMyCommands ', function getMyCommandsSuite() {
it('should get bot commands', function test() {
return bot.getMyCommands().then(resp => {
assert.ok(is.array(resp));
});
});
});
describe.skip('#setChatAdministratorCustomTitle ', function setChatAdministratorCustomTitleSuite() {
it('should set chat permissions', function test() {
return bot.setChatAdministratorCustomTitle(GROUPID, USERID, 'Custom Name').then(resp => {
assert.ok(is.boolean(resp));
});
});
});
describe('#setChatPermissions ', function setChatPermissionsSuite() {
it('should set chat permissions', function test() {
const ChatPermissions = {
can_send_messages: true,
can_send_media_messages: true,
can_send_polls: false,
can_send_other_messages: false,
can_add_web_page_previews: true,
can_change_info: false,
can_invite_users: false,
can_pin_messages: true
};
return bot.setChatPermissions(GROUPID, ChatPermissions).then(resp => {
assert.ok(is.boolean(resp));
});
});
});
describe('#exportChatInviteLink', function exportChatInviteLinkSuite() {
before(function before() {
utils.handleRatelimit(bot, 'exportChatInviteLink', this);
@ -885,7 +931,8 @@ describe('TelegramBot', function telegramSuite() {
utils.handleRatelimit(bot, 'setChatTitle', this);
});
it('should set the chat title', function test() {
return bot.setChatTitle(GROUPID, 'ntba test group').then(resp => {
const random = Math.floor(Math.random() * 1000);
return bot.setChatTitle(GROUPID, `ntba test group (random: ${random})`).then(resp => {
assert.equal(resp, true);
});
});
@ -956,7 +1003,7 @@ describe('TelegramBot', function telegramSuite() {
utils.handleRatelimit(bot, 'editMessageCaption', this);
});
it('should edit a caption sent by the bot', function test() {
const photo = `${__dirname}/data/photo.gif`;
const photo = `${__dirname}/data/photo.png`;
const options = { caption: 'test caption' };
return bot.sendPhoto(USERID, photo, options).then(resp => {
assert.equal(resp.caption, 'test caption');
@ -1128,6 +1175,43 @@ describe('TelegramBot', function telegramSuite() {
});
});
describe('#sendPoll', function sendPollSuite() {
it('should send a Poll', function test() {
const question = '¿Are you okey?';
const answers = ['Yes', 'No'];
const opts = { is_anonymous: true };
return bot.sendPoll(GROUPID, question, answers, opts).then(resp => {
assert.ok(is.object(resp));
});
});
it('should send a Quiz', function test() {
const question = '¿Are you okey?';
const answers = ['Yes', 'No'];
const opts = {
is_anonymous: true,
type: 'quiz',
correct_option_id: 0
};
return bot.sendPoll(GROUPID, question, answers, opts).then(resp => {
assert.ok(is.object(resp));
});
});
});
describe('#sendDice', function sendDiceSuite() {
it('should send a Dice', function test() {
return bot.sendDice(GROUPID).then(resp => {
assert.ok(is.object(resp));
});
});
it('should send a Dart', function test() {
const opts = { emoji: '🎯' };
return bot.sendDice(GROUPID, opts).then(resp => {
assert.ok(is.object(resp));
});
});
});
describe('#getFile', function getFileSuite() {
this.timeout(timeout);
before(function before() {
@ -1416,7 +1500,7 @@ describe('TelegramBot', function telegramSuite() {
return bot.sendMediaGroup(USERID, [
{
type: 'photo',
media: `${__dirname}/data/photo.gif`,
media: `${__dirname}/data/photo.png`,
},
{
type: 'video',