Skip to content

Commit

Permalink
#465 add full support for bot api 7.0 (#480)
Browse files Browse the repository at this point in the history
* feat: #465 add full reactions support as defined in api 7.0

* feat: #465 add Link Preview Customization support as defined in api 7.0

* feat: #465 add Multiple Message Actions support as defined in api 7.0

* feat: #465 add Chat Boost as defined in api 7.0

* feat: #465 add Giveaway as defined in api 7.0
  • Loading branch information
bernard-ng authored Aug 3, 2024
1 parent 3b44cb2 commit ae151c2
Show file tree
Hide file tree
Showing 20 changed files with 1,242 additions and 57 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
- Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData`
- Add `\TelegramBot\Api\Types\Message::$videoNote` field
- Drop php < 8.1
- Add `\TelegramBot\Api\Types\Update::$messageReaction` field
- Add `\TelegramBot\Api\Types\Update::$messageReactionCount` field
- Add `\TelegramBot\Api\BotApi::setMessageReaction` api method
- Add `\TelegramBot\Api\BotApi::deleteMessages` api method
- Add `\TelegramBot\Api\BotApi::copyMessages` api method
- Add `\TelegramBot\Api\BotApi::forwardMessages` api method
- Add `\TelegramBot\Api\BotApi::getUserChatBoosts` api method

### Deprecated
- Deprecate `reply_to_message_id` and `allow_sending_without_reply` parameters to `\TelegramBot\Api\BotApi` methods. Use `reply_parameters` instead.
- Deprecate `disable_web_page_preview` parameter to `\TelegramBot\Api\BotApi` methods. Use `link_preview_options` instead.

## 2.5.0 - 2023-08-09

Expand Down
3 changes: 2 additions & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static function validate($data)
return true;
}

throw new InvalidArgumentException();
$missingParams = implode(', ', array_diff(static::$requiredParams, array_keys($data)));
throw new InvalidArgumentException(sprintf('%s Validation failed. Missing required parameters: %s', static::class, $missingParams));
}

/**
Expand Down
516 changes: 463 additions & 53 deletions src/BotApi.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Types/ArrayOfReactionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static function fromResponse($data)
{
$arrayOfReactionTypes = [];
foreach ($data as $reactionTypeData) {
// В зависимости от типа реакции, создаем соответствующий объект
// Depending on the type of reaction, create an appropriate object
if ($reactionTypeData['type'] === 'emoji') {
$arrayOfReactionTypes[] = ReactionTypeEmoji::fromResponse($reactionTypeData);
} elseif ($reactionTypeData['type'] === 'custom_emoji') {
Expand Down
5 changes: 5 additions & 0 deletions src/Types/GiveawayCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use TelegramBot\Api\BaseType;
use TelegramBot\Api\TypeInterface;

/**
* Class GiveawayCreated.
* This object represents a service message about the creation of a scheduled giveaway.
* Currently holds no information.
*/
class GiveawayCreated extends BaseType implements TypeInterface
{
}
44 changes: 42 additions & 2 deletions src/Types/Inline/InputMessageContent/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace TelegramBot\Api\Types\Inline\InputMessageContent;

use TelegramBot\Api\TypeInterface;
use TelegramBot\Api\Types\LinkPreviewOptions;
use TelegramBot\Api\Types\ArrayOfMessageEntity;
use TelegramBot\Api\Types\Inline\InputMessageContent;

/**
Expand All @@ -35,7 +37,9 @@ class Text extends InputMessageContent implements TypeInterface
protected static $map = [
'message_text' => true,
'parse_mode' => true,
'disable_web_page_preview' => true,
'entities' => ArrayOfMessageEntity::class,
'disable_web_page_preview' => true, // @todo: remove as deprecated with bot api 7.0
'link_preview_options' => LinkPreviewOptions::class
];

/**
Expand All @@ -54,23 +58,42 @@ class Text extends InputMessageContent implements TypeInterface
protected $parseMode;

/**
* @deprecated use $linkPreviewOptions instead
* Optional. Disables link previews for links in the sent message
*
* @var bool|null
*/
protected $disableWebPagePreview;

/**
* Link preview generation options for the message
*
* @var LinkPreviewOptions|null
*/
protected $linkPreviewOptions;

/**
* Text constructor.
*
* @param string $messageText
* @param string|null $parseMode
* @param bool $disableWebPagePreview
* @param LinkPreviewOptions|null $linkPreviewOptions Link preview generation options for the message.
*/
public function __construct($messageText, $parseMode = null, $disableWebPagePreview = false)
public function __construct($messageText, $parseMode = null, $disableWebPagePreview = false, $linkPreviewOptions = null)
{
$this->messageText = $messageText;
$this->parseMode = $parseMode;
$this->disableWebPagePreview = $disableWebPagePreview;

if (null === $linkPreviewOptions && false !== $disableWebPagePreview) {
@trigger_error('setting $disableWebPagePreview is now deprecated use $linkPreviewOptions instead', E_USER_DEPRECATED);

$this->linkPreviewOptions = new LinkPreviewOptions();
$this->linkPreviewOptions->map([
'is_disabled' => $disableWebPagePreview
]);
}
}

/**
Expand Down Expand Up @@ -126,4 +149,21 @@ public function setDisableWebPagePreview($disableWebPagePreview)
{
$this->disableWebPagePreview = $disableWebPagePreview;
}

/**
* @return LinkPreviewOptions|null
*/
public function getLinkPreviewOptions()
{
return $this->linkPreviewOptions;
}

/**
* @param LinkPreviewOptions|null $linkPreviewOptions
* @return void
*/
public function setLinkPreviewOptions($linkPreviewOptions)
{
$this->linkPreviewOptions = $linkPreviewOptions;
}
}
107 changes: 107 additions & 0 deletions src/Types/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ class Update extends BaseType implements TypeInterface
'my_chat_member' => ChatMemberUpdated::class,
'chat_member' => ChatMemberUpdated::class,
'chat_join_request' => ChatJoinRequest::class,
'message_reaction' => MessageReactionUpdated::class,
'message_reaction_count' => MessageReactionCountUpdated::class,
'chat_boost' => ChatBoostUpdated::class,
'chat_boost_removed' => ChatBoostRemoved::class,
];

/**
Expand Down Expand Up @@ -159,6 +163,41 @@ class Update extends BaseType implements TypeInterface
*/
protected $chatJoinRequest;

/**
* Optional. A reaction to a message was changed by a user.
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction'
* in the list of allowed_updates to receive these updates. The update isn't received for reactions set by bots.
*
* @var MessageReactionUpdated|null
*/
protected $messageReaction;

/**
* Optional. Reactions to a message with anonymous reactions were changed.
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction_count'
* in the list of allowed_updates to receive these updates.
* The updates are grouped and can be sent with delay up to a few minutes.
*
* @var MessageReactionCountUpdated|null
*/
protected $messageReactionCount;

/**
* Optional. A chat boost was added or changed.
* The bot must be an administrator in the chat to receive these updates.
*
* @var ChatBoostUpdated|null
*/
protected $chatBoost;

/**
* Optional. A boost was removed from a chat.
* The bot must be an administrator in the chat to receive these updates.
*
* @var ChatBoostRemoved|null
*/
protected $removedChatBoost;

/**
* @return int
*/
Expand Down Expand Up @@ -433,4 +472,72 @@ public function setChatJoinRequest($chatJoinRequest)
{
$this->chatJoinRequest = $chatJoinRequest;
}

/**
* @return MessageReactionUpdated|null
*/
public function getMessageReaction()
{
return $this->messageReaction;
}

/**
* @param MessageReactionUpdated|null $messageReaction
* @return void
*/
public function setMessageReaction(?MessageReactionUpdated $messageReaction)
{
$this->messageReaction = $messageReaction;
}

/**
* @return MessageReactionCountUpdated|null
*/
public function getMessageReactionCount()
{
return $this->messageReactionCount;
}

/**
* @param MessageReactionCountUpdated|null $messageReactionCount
* @return void
*/
public function setMessageReactionCount(?MessageReactionCountUpdated $messageReactionCount)
{
$this->messageReactionCount = $messageReactionCount;
}

/**
* @return ChatBoostUpdated|null
*/
public function getChatBoost()
{
return $this->chatBoost;
}

/**
* @param ChatBoostUpdated|null $chatBoost
* @return void
*/
public function setChatBoost($chatBoost)
{
$this->chatBoost = $chatBoost;
}

/**
* @return ChatBoostRemoved|null
*/
public function getChatBoostRemoved()
{
return $this->removedChatBoost;
}

/**
* @param ChatBoostRemoved|null $removedChatBoost
* @return void
*/
public function setChatBoostRemoved($removedChatBoost)
{
$this->removedChatBoost = $removedChatBoost;
}
}
39 changes: 39 additions & 0 deletions tests/Types/ArrayOfChatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Types\Chat;
use TelegramBot\Api\Types\ArrayOfChat;
use PHPUnit\Framework\TestCase;

class ArrayOfChatTest extends TestCase
{
public function testFromResponse()
{
$items = ArrayOfChat::fromResponse([
[
'id' => 123456789,
'type' => 'group',
],
[
'id' => 123456788,
'type' => 'private',
]
]);

$expected = [
Chat::fromResponse([
'id' => 123456789,
'type' => 'group',
]),
Chat::fromResponse([
'id' => 123456788,
'type' => 'private',
])
];

foreach ($items as $key => $item) {
$this->assertEquals($expected[$key], $item);
}
}
}
40 changes: 40 additions & 0 deletions tests/Types/ArrayOfReactionTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace TelegramBot\Api\Test\Types;

use PHPUnit\Framework\TestCase;
use TelegramBot\Api\Types\ReactionTypeEmoji;
use TelegramBot\Api\Types\ArrayOfReactionType;
use TelegramBot\Api\Types\ReactionTypeCustomEmoji;

class ArrayOfReactionTypeTest extends TestCase
{
public function testFromResponse()
{
$items = ArrayOfReactionType::fromResponse([
[
'emoji' => '👍',
'type' => 'emoji'
],
[
'custom_emoji_id' => 'custom_emoji_123',
'type' => 'custom_emoji'
]
]);

$expected = [
ReactionTypeEmoji::fromResponse([
'emoji' => '👍',
'type' => 'emoji'
]),
ReactionTypeCustomEmoji::fromResponse([
'custom_emoji_id' => 'custom_emoji_123',
'type' => 'custom_emoji'
])
];

foreach ($items as $key => $item) {
$this->assertEquals($expected[$key], $item);
}
}
}
50 changes: 50 additions & 0 deletions tests/Types/ChatBoostRemovedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\ChatBoostRemoved;

class ChatBoostRemovedTest extends AbstractTypeTest
{
protected static function getType()
{
return ChatBoostRemoved::class;
}

public static function getMinResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'boost_id' => 1,
'remove_date' => 1682343643,
'source' => ChatBoostSourceTest::getMinResponse()
];
}

public static function getFullResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'boost_id' => 1,
'remove_date' => 1682343643,
'source' => ChatBoostSourceTest::getMinResponse()
];
}

protected function assertMinItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(1, $item->getBoostId());
$this->assertEquals(1682343643, $item->getRemoveDate());
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
}

protected function assertFullItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(1, $item->getBoostId());
$this->assertEquals(1682343643, $item->getRemoveDate());
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
}
}
Loading

0 comments on commit ae151c2

Please sign in to comment.