Skip to content

Commit

Permalink
Merge pull request #322 from TelegramBot/updateMethods
Browse files Browse the repository at this point in the history
Update sendVoice & sendDice methods
  • Loading branch information
MyZik authored Mar 9, 2021
2 parents dafad7c + 804ee2a commit 9a69f82
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 12 deletions.
29 changes: 23 additions & 6 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -812,14 +812,17 @@ public function sendAnimation(
* On success, the sent Message is returned.
* Bots can currently send voice messages of up to 50 MB in size, this limit may be changed in the future.
*
* @param int|string $chatId chat_id or @channel_name
* @param int|string $chatId chat_id or @channel_name
* @param \CURLFile|string $voice
* @param int|null $duration
* @param int|null $replyToMessageId
* @param string $caption Voice message caption, 0-1024 characters after entities parsing
* @param int|null $duration
* @param int|null $replyToMessageId
* @param Types\ReplyKeyboardMarkup|Types\ReplyKeyboardHide|Types\ForceReply|
* Types\ReplyKeyboardRemove|null $replyMarkup
* @param bool $disableNotification
* @param string|null $parseMode
* @param bool $disableNotification
* @param bool $allowSendingWithoutReply Pass True, if the message should be sent even if the specified
* replied-to message is not found
* @param string|null $parseMode
*
* @return \TelegramBot\Api\Types\Message
* @throws \TelegramBot\Api\InvalidArgumentException
Expand All @@ -828,19 +831,23 @@ public function sendAnimation(
public function sendVoice(
$chatId,
$voice,
$caption = null,
$duration = null,
$replyToMessageId = null,
$replyMarkup = null,
$disableNotification = false,
$allowSendingWithoutReply = false,
$parseMode = null
) {
return Message::fromResponse($this->call('sendVoice', [
'chat_id' => $chatId,
'voice' => $voice,
'caption' => $caption,
'duration' => $duration,
'reply_to_message_id' => $replyToMessageId,
'reply_markup' => is_null($replyMarkup) ? $replyMarkup : $replyMarkup->toJson(),
'disable_notification' => (bool)$disableNotification,
'allow_sending_without_reply' => $allowSendingWithoutReply,
'parse_mode' => $parseMode
]));
}
Expand Down Expand Up @@ -1943,28 +1950,38 @@ public function sendPoll(
* On success, the sent Message is returned. (Yes, we're aware of the “proper” singular of die.
* But it's awkward, and we decided to help it change. One dice at a time!)
*
* @param $chatId string|int Unique identifier for the target chat or username of the target channel
* @param $chatId string|int Unique identifier for the target chat or username of the target channel
* (in the format @channelusername)
* @param $emoji string Emoji on which the dice throw animation is based. Currently, must be one of “🎲”,
* “🎯”, “🏀”, “⚽”, or “🎰”. Dice can have values 1-6 for “🎲” and “🎯”, values 1-5 for “🏀” and “⚽”, and
* values 1-64 for “🎰”. Defaults to “🎲
* @param bool $disableNotification Sends the message silently. Users will receive a notification with no sound.
* @param null $replyToMessageId If the message is a reply, ID of the original message
* @param bool $$allowSendingWithoutReply Pass True, if the message should be sent even if the specified replied-to
* message is not found,
* @param null $replyMarkup Additional interface options. A JSON-serialized object for an inline keyboard,
* custom reply keyboard, instructions to remove reply
* keyboard or to force a reply from the user.
*
* @return bool|Message
* @throws Exception
* @throws HttpException
* @throws InvalidJsonException
*/
public function sendDice(
$chatId,
$emoji,
$disableNotification = false,
$replyToMessageId = null,
$allowSendingWithoutReply = false,
$replyMarkup = null
) {
return Message::fromResponse($this->call('sendDice', [
'chat_id' => $chatId,
'emoji' => $emoji,
'disable_notification' => (bool) $disableNotification,
'reply_to_message_id' => (int) $replyToMessageId,
'allow_sending_without_reply' => (bool) $allowSendingWithoutReply,
'reply_markup' => $replyMarkup === null ? $replyMarkup : $replyMarkup->toJson(),
]));
}
Expand Down
37 changes: 31 additions & 6 deletions src/Types/Voice.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,36 @@ class Voice extends BaseType implements TypeInterface
*
* @var array
*/
static protected $requiredParams = ['file_id', 'duration'];
static protected $requiredParams = ['file_id', 'file_unique_id', 'duration'];

/**
* {@inheritdoc}
*
* @var array
*/
static protected $map = [
'file_id' => true,
'duration' => true,
'mime_type' => true,
'file_size' => true
'file_id' => true,
'file_unique_id' => true,
'duration' => true,
'mime_type' => true,
'file_size' => true,
];

/**
* Unique identifier for this file
* Identifier for this file, which can be used to download or reuse the file
*
* @var string
*/
protected $fileId;

/**
* Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be
* used to download or reuse the file.
*
* @var string
*/
protected $fileUniqueId;

/**
* Duration of the audio in seconds as defined by sender
*
Expand Down Expand Up @@ -77,6 +86,22 @@ public function setFileId($fileId)
$this->fileId = $fileId;
}

/**
* @return string
*/
public function getFileUniqueId()
{
return $this->fileUniqueId;
}

/**
* @param string $fileUniqueId
*/
public function setFileUniqueId($fileUniqueId)
{
$this->fileUniqueId = $fileUniqueId;
}

/**
* @return int
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TelegramBot\Api\Test;

use TelegramBot\Api\Types\Chat;
use TelegramBot\Api\Types\Dice;
use TelegramBot\Api\Types\Document;
use TelegramBot\Api\Types\Location;
use TelegramBot\Api\Types\Audio;
Expand Down Expand Up @@ -304,6 +305,7 @@ public function testSetVoice()
$item = new Message();
$voice = Voice::fromResponse(array(
'file_id' => 'testFileId1',
'file_unique_id' => 'testUniqueFileId',
'duration' => 1,
'mime_type' => 'audio/mp3',
'file_size' => 3
Expand All @@ -317,6 +319,7 @@ public function testGetVoice()
$item = new Message();
$voice = Voice::fromResponse(array(
'file_id' => 'testFileId1',
'file_unique_id' => 'testUniqueFileId',
'duration' => 1,
'mime_type' => 'audio/mp3',
'file_size' => 3
Expand Down Expand Up @@ -369,6 +372,29 @@ public function testGetVideo()
$this->assertInstanceOf('\TelegramBot\Api\Types\Video', $item->getVideo());
}

public function testSetDice()
{
$item = new Message();
$dice = Dice::fromResponse(array(
'emoji' => '🎲',
'value' => 3
));
$item->setDice($dice);
$this->assertAttributeEquals($dice, 'dice', $item);
}

public function testGetDice()
{
$item = new Message();
$dice = Dice::fromResponse(array(
'emoji' => '🎲',
'value' => 3
));
$item->setDice($dice);
$this->assertEquals($dice, $item->getDice());
$this->assertInstanceOf('\TelegramBot\Api\Types\Dice', $item->getDice());
}

public function testSetSticker()
{
$item = new Message();
Expand Down
16 changes: 16 additions & 0 deletions tests/VoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ public function testGetFileId()
$this->assertEquals('testfileId', $item->getFileId());
}

public function testSetUniqueFileId()
{
$item = new Voice();
$item->setFileUniqueId('fileUniqueId');
$this->assertAttributeEquals('fileUniqueId', 'fileUniqueId', $item);
}

public function testGetUniqueFileId()
{
$item = new Voice();
$item->setFileUniqueId('fileUniqueId');
$this->assertEquals('fileUniqueId', $item->getFileUniqueId());
}

public function testSetDuration()
{
$item = new Voice();
Expand Down Expand Up @@ -65,12 +79,14 @@ public function testFromResponse()
{
$item = Voice::fromResponse(array(
'file_id' => 'testFileId1',
'file_unique_id' => 'testFileUniqueId1',
'duration' => 1,
'mime_type' => 'audio/mp3',
'file_size' => 3
));
$this->assertInstanceOf('\TelegramBot\Api\Types\Voice', $item);
$this->assertAttributeEquals('testFileId1', 'fileId', $item);
$this->assertAttributeEquals('testFileUniqueId1', 'fileUniqueId', $item);
$this->assertAttributeEquals(1, 'duration', $item);
$this->assertAttributeEquals('audio/mp3', 'mimeType', $item);
$this->assertAttributeEquals(3, 'fileSize', $item);
Expand Down

0 comments on commit 9a69f82

Please sign in to comment.