Skip to content

Commit ae151c2

Browse files
authored
#465 add full support for bot api 7.0 (#480)
* 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
1 parent 3b44cb2 commit ae151c2

20 files changed

+1242
-57
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,17 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
1616
- Add method `\TelegramBot\Api\BotApi::validateWebAppData` to validate `window.Telegram.WebApp.initData`
1717
- Add `\TelegramBot\Api\Types\Message::$videoNote` field
1818
- Drop php < 8.1
19+
- Add `\TelegramBot\Api\Types\Update::$messageReaction` field
20+
- Add `\TelegramBot\Api\Types\Update::$messageReactionCount` field
21+
- Add `\TelegramBot\Api\BotApi::setMessageReaction` api method
22+
- Add `\TelegramBot\Api\BotApi::deleteMessages` api method
23+
- Add `\TelegramBot\Api\BotApi::copyMessages` api method
24+
- Add `\TelegramBot\Api\BotApi::forwardMessages` api method
25+
- Add `\TelegramBot\Api\BotApi::getUserChatBoosts` api method
26+
27+
### Deprecated
28+
- Deprecate `reply_to_message_id` and `allow_sending_without_reply` parameters to `\TelegramBot\Api\BotApi` methods. Use `reply_parameters` instead.
29+
- Deprecate `disable_web_page_preview` parameter to `\TelegramBot\Api\BotApi` methods. Use `link_preview_options` instead.
1930

2031
## 2.5.0 - 2023-08-09
2132

src/BaseType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ public static function validate($data)
3939
return true;
4040
}
4141

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

4546
/**

src/BotApi.php

Lines changed: 463 additions & 53 deletions
Large diffs are not rendered by default.

src/Types/ArrayOfReactionType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function fromResponse($data)
1515
{
1616
$arrayOfReactionTypes = [];
1717
foreach ($data as $reactionTypeData) {
18-
// В зависимости от типа реакции, создаем соответствующий объект
18+
// Depending on the type of reaction, create an appropriate object
1919
if ($reactionTypeData['type'] === 'emoji') {
2020
$arrayOfReactionTypes[] = ReactionTypeEmoji::fromResponse($reactionTypeData);
2121
} elseif ($reactionTypeData['type'] === 'custom_emoji') {

src/Types/GiveawayCreated.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
use TelegramBot\Api\BaseType;
66
use TelegramBot\Api\TypeInterface;
77

8+
/**
9+
* Class GiveawayCreated.
10+
* This object represents a service message about the creation of a scheduled giveaway.
11+
* Currently holds no information.
12+
*/
813
class GiveawayCreated extends BaseType implements TypeInterface
914
{
1015
}

src/Types/Inline/InputMessageContent/Text.php

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
namespace TelegramBot\Api\Types\Inline\InputMessageContent;
1010

1111
use TelegramBot\Api\TypeInterface;
12+
use TelegramBot\Api\Types\LinkPreviewOptions;
13+
use TelegramBot\Api\Types\ArrayOfMessageEntity;
1214
use TelegramBot\Api\Types\Inline\InputMessageContent;
1315

1416
/**
@@ -35,7 +37,9 @@ class Text extends InputMessageContent implements TypeInterface
3537
protected static $map = [
3638
'message_text' => true,
3739
'parse_mode' => true,
38-
'disable_web_page_preview' => true,
40+
'entities' => ArrayOfMessageEntity::class,
41+
'disable_web_page_preview' => true, // @todo: remove as deprecated with bot api 7.0
42+
'link_preview_options' => LinkPreviewOptions::class
3943
];
4044

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

5660
/**
61+
* @deprecated use $linkPreviewOptions instead
5762
* Optional. Disables link previews for links in the sent message
5863
*
5964
* @var bool|null
6065
*/
6166
protected $disableWebPagePreview;
6267

68+
/**
69+
* Link preview generation options for the message
70+
*
71+
* @var LinkPreviewOptions|null
72+
*/
73+
protected $linkPreviewOptions;
74+
6375
/**
6476
* Text constructor.
77+
*
6578
* @param string $messageText
6679
* @param string|null $parseMode
6780
* @param bool $disableWebPagePreview
81+
* @param LinkPreviewOptions|null $linkPreviewOptions Link preview generation options for the message.
6882
*/
69-
public function __construct($messageText, $parseMode = null, $disableWebPagePreview = false)
83+
public function __construct($messageText, $parseMode = null, $disableWebPagePreview = false, $linkPreviewOptions = null)
7084
{
7185
$this->messageText = $messageText;
7286
$this->parseMode = $parseMode;
7387
$this->disableWebPagePreview = $disableWebPagePreview;
88+
89+
if (null === $linkPreviewOptions && false !== $disableWebPagePreview) {
90+
@trigger_error('setting $disableWebPagePreview is now deprecated use $linkPreviewOptions instead', E_USER_DEPRECATED);
91+
92+
$this->linkPreviewOptions = new LinkPreviewOptions();
93+
$this->linkPreviewOptions->map([
94+
'is_disabled' => $disableWebPagePreview
95+
]);
96+
}
7497
}
7598

7699
/**
@@ -126,4 +149,21 @@ public function setDisableWebPagePreview($disableWebPagePreview)
126149
{
127150
$this->disableWebPagePreview = $disableWebPagePreview;
128151
}
152+
153+
/**
154+
* @return LinkPreviewOptions|null
155+
*/
156+
public function getLinkPreviewOptions()
157+
{
158+
return $this->linkPreviewOptions;
159+
}
160+
161+
/**
162+
* @param LinkPreviewOptions|null $linkPreviewOptions
163+
* @return void
164+
*/
165+
public function setLinkPreviewOptions($linkPreviewOptions)
166+
{
167+
$this->linkPreviewOptions = $linkPreviewOptions;
168+
}
129169
}

src/Types/Update.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ class Update extends BaseType implements TypeInterface
4646
'my_chat_member' => ChatMemberUpdated::class,
4747
'chat_member' => ChatMemberUpdated::class,
4848
'chat_join_request' => ChatJoinRequest::class,
49+
'message_reaction' => MessageReactionUpdated::class,
50+
'message_reaction_count' => MessageReactionCountUpdated::class,
51+
'chat_boost' => ChatBoostUpdated::class,
52+
'chat_boost_removed' => ChatBoostRemoved::class,
4953
];
5054

5155
/**
@@ -159,6 +163,41 @@ class Update extends BaseType implements TypeInterface
159163
*/
160164
protected $chatJoinRequest;
161165

166+
/**
167+
* Optional. A reaction to a message was changed by a user.
168+
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction'
169+
* in the list of allowed_updates to receive these updates. The update isn't received for reactions set by bots.
170+
*
171+
* @var MessageReactionUpdated|null
172+
*/
173+
protected $messageReaction;
174+
175+
/**
176+
* Optional. Reactions to a message with anonymous reactions were changed.
177+
* The bot must be an administrator in the chat and must explicitly specify 'message_reaction_count'
178+
* in the list of allowed_updates to receive these updates.
179+
* The updates are grouped and can be sent with delay up to a few minutes.
180+
*
181+
* @var MessageReactionCountUpdated|null
182+
*/
183+
protected $messageReactionCount;
184+
185+
/**
186+
* Optional. A chat boost was added or changed.
187+
* The bot must be an administrator in the chat to receive these updates.
188+
*
189+
* @var ChatBoostUpdated|null
190+
*/
191+
protected $chatBoost;
192+
193+
/**
194+
* Optional. A boost was removed from a chat.
195+
* The bot must be an administrator in the chat to receive these updates.
196+
*
197+
* @var ChatBoostRemoved|null
198+
*/
199+
protected $removedChatBoost;
200+
162201
/**
163202
* @return int
164203
*/
@@ -433,4 +472,72 @@ public function setChatJoinRequest($chatJoinRequest)
433472
{
434473
$this->chatJoinRequest = $chatJoinRequest;
435474
}
475+
476+
/**
477+
* @return MessageReactionUpdated|null
478+
*/
479+
public function getMessageReaction()
480+
{
481+
return $this->messageReaction;
482+
}
483+
484+
/**
485+
* @param MessageReactionUpdated|null $messageReaction
486+
* @return void
487+
*/
488+
public function setMessageReaction(?MessageReactionUpdated $messageReaction)
489+
{
490+
$this->messageReaction = $messageReaction;
491+
}
492+
493+
/**
494+
* @return MessageReactionCountUpdated|null
495+
*/
496+
public function getMessageReactionCount()
497+
{
498+
return $this->messageReactionCount;
499+
}
500+
501+
/**
502+
* @param MessageReactionCountUpdated|null $messageReactionCount
503+
* @return void
504+
*/
505+
public function setMessageReactionCount(?MessageReactionCountUpdated $messageReactionCount)
506+
{
507+
$this->messageReactionCount = $messageReactionCount;
508+
}
509+
510+
/**
511+
* @return ChatBoostUpdated|null
512+
*/
513+
public function getChatBoost()
514+
{
515+
return $this->chatBoost;
516+
}
517+
518+
/**
519+
* @param ChatBoostUpdated|null $chatBoost
520+
* @return void
521+
*/
522+
public function setChatBoost($chatBoost)
523+
{
524+
$this->chatBoost = $chatBoost;
525+
}
526+
527+
/**
528+
* @return ChatBoostRemoved|null
529+
*/
530+
public function getChatBoostRemoved()
531+
{
532+
return $this->removedChatBoost;
533+
}
534+
535+
/**
536+
* @param ChatBoostRemoved|null $removedChatBoost
537+
* @return void
538+
*/
539+
public function setChatBoostRemoved($removedChatBoost)
540+
{
541+
$this->removedChatBoost = $removedChatBoost;
542+
}
436543
}

tests/Types/ArrayOfChatTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Test\Types;
4+
5+
use TelegramBot\Api\Types\Chat;
6+
use TelegramBot\Api\Types\ArrayOfChat;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ArrayOfChatTest extends TestCase
10+
{
11+
public function testFromResponse()
12+
{
13+
$items = ArrayOfChat::fromResponse([
14+
[
15+
'id' => 123456789,
16+
'type' => 'group',
17+
],
18+
[
19+
'id' => 123456788,
20+
'type' => 'private',
21+
]
22+
]);
23+
24+
$expected = [
25+
Chat::fromResponse([
26+
'id' => 123456789,
27+
'type' => 'group',
28+
]),
29+
Chat::fromResponse([
30+
'id' => 123456788,
31+
'type' => 'private',
32+
])
33+
];
34+
35+
foreach ($items as $key => $item) {
36+
$this->assertEquals($expected[$key], $item);
37+
}
38+
}
39+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Test\Types;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use TelegramBot\Api\Types\ReactionTypeEmoji;
7+
use TelegramBot\Api\Types\ArrayOfReactionType;
8+
use TelegramBot\Api\Types\ReactionTypeCustomEmoji;
9+
10+
class ArrayOfReactionTypeTest extends TestCase
11+
{
12+
public function testFromResponse()
13+
{
14+
$items = ArrayOfReactionType::fromResponse([
15+
[
16+
'emoji' => '👍',
17+
'type' => 'emoji'
18+
],
19+
[
20+
'custom_emoji_id' => 'custom_emoji_123',
21+
'type' => 'custom_emoji'
22+
]
23+
]);
24+
25+
$expected = [
26+
ReactionTypeEmoji::fromResponse([
27+
'emoji' => '👍',
28+
'type' => 'emoji'
29+
]),
30+
ReactionTypeCustomEmoji::fromResponse([
31+
'custom_emoji_id' => 'custom_emoji_123',
32+
'type' => 'custom_emoji'
33+
])
34+
];
35+
36+
foreach ($items as $key => $item) {
37+
$this->assertEquals($expected[$key], $item);
38+
}
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace TelegramBot\Api\Test\Types;
4+
5+
use TelegramBot\Api\Test\AbstractTypeTest;
6+
use TelegramBot\Api\Types\ChatBoostRemoved;
7+
8+
class ChatBoostRemovedTest extends AbstractTypeTest
9+
{
10+
protected static function getType()
11+
{
12+
return ChatBoostRemoved::class;
13+
}
14+
15+
public static function getMinResponse()
16+
{
17+
return [
18+
'chat' => ChatTest::getMinResponse(),
19+
'boost_id' => 1,
20+
'remove_date' => 1682343643,
21+
'source' => ChatBoostSourceTest::getMinResponse()
22+
];
23+
}
24+
25+
public static function getFullResponse()
26+
{
27+
return [
28+
'chat' => ChatTest::getMinResponse(),
29+
'boost_id' => 1,
30+
'remove_date' => 1682343643,
31+
'source' => ChatBoostSourceTest::getMinResponse()
32+
];
33+
}
34+
35+
protected function assertMinItem($item)
36+
{
37+
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
38+
$this->assertEquals(1, $item->getBoostId());
39+
$this->assertEquals(1682343643, $item->getRemoveDate());
40+
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
41+
}
42+
43+
protected function assertFullItem($item)
44+
{
45+
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
46+
$this->assertEquals(1, $item->getBoostId());
47+
$this->assertEquals(1682343643, $item->getRemoveDate());
48+
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
49+
}
50+
}

0 commit comments

Comments
 (0)