Skip to content

Commit

Permalink
feat: #465 add Chat Boost as defined in api 7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Jul 30, 2024
1 parent 742afb8 commit d9d4fe5
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
- 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.
Expand Down
2 changes: 1 addition & 1 deletion src/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function validate($data)
}

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

/**
Expand Down
21 changes: 21 additions & 0 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TelegramBot\Api;

use TelegramBot\Api\Http\CurlHttpClient;
use TelegramBot\Api\Types\UserChatBoosts;
use TelegramBot\Api\Types\ReplyParameters;
use TelegramBot\Api\Http\HttpClientInterface;
use TelegramBot\Api\Types\ArrayOfBotCommand;
Expand Down Expand Up @@ -3160,6 +3161,26 @@ public function copyMessages(
]);
}

/**
* Use this method to get the list of boosts added to a chat by a user. Requires administrator rights in the chat.
* Returns a UserChatBoosts object.
*
* @param string|int $chatId Unique identifier for the chat or username of the channel (in the format @channelusername)
* @param int $userId Unique identifier of the target user
*
* @return UserChatBoosts
* @throws Exception
*
* @author bernard-ng <[email protected]>
*/
public function getUserChatBoosts($chatId, $userId)
{
return UserChatBoosts::fromResponse($this->call('getUserChatBoosts', [
'chat_id' => $chatId,
'user_id' => $userId
]));
}

/**
* Set an option for a cURL transfer
*
Expand Down
54 changes: 53 additions & 1 deletion src/Types/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ class Update extends BaseType implements TypeInterface
'chat_member' => ChatMemberUpdated::class,
'chat_join_request' => ChatJoinRequest::class,
'message_reaction' => MessageReactionUpdated::class,
'message_reaction_count' => MessageReactionCountUpdated::class
'message_reaction_count' => MessageReactionCountUpdated::class,
'chat_boost' => ChatBoostUpdated::class,
'chat_boost_removed' => ChatBoostRemoved::class,
];

/**
Expand Down Expand Up @@ -180,6 +182,22 @@ class Update extends BaseType implements TypeInterface
*/
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 @@ -488,4 +506,38 @@ public function setMessageReactionCount(?MessageReactionCountUpdated $messageRea
{
$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;
}
}
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());
}
}
42 changes: 42 additions & 0 deletions tests/Types/ChatBoostSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace TelegramBot\Api\Test\Types;

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

class ChatBoostSourceTest extends AbstractTypeTest
{
protected static function getType()
{
return ChatBoostSource::class;
}

public static function getMinResponse()
{
return [
'source' => 'premium',
'user' => UserTest::getMinResponse(),
];
}

public static function getFullResponse()
{
return [
'source' => 'premium',
'user' => UserTest::getMinResponse(),
];
}

protected function assertMinItem($item)
{
$this->assertEquals('premium', $item->getSource());
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
}

protected function assertFullItem($item)
{
$this->assertEquals('premium', $item->getSource());
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
}
}
50 changes: 50 additions & 0 deletions tests/Types/ChatBoostTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TelegramBot\Api\Test\Types;

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

class ChatBoostTest extends AbstractTypeTest
{
protected static function getType()
{
return ChatBoost::class;
}

public static function getMinResponse()
{
return [
'boost_id' => 1,
'add_date' => 1682343643,
'expiration_date' => 1725042370,
'source' => ChatBoostSourceTest::getMinResponse()
];
}

public static function getFullResponse()
{
return [
'boost_id' => 1,
'add_date' => 1682343643,
'expiration_date' => 1725042370,
'source' => ChatBoostSourceTest::getMinResponse()
];
}

protected function assertMinItem($item)
{
$this->assertEquals(1, $item->getBoostId());
$this->assertEquals(1682343643, $item->getAddDate());
$this->assertEquals(1725042370, $item->getExpirationDate());
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
}

protected function assertFullItem($item)
{
$this->assertEquals(1, $item->getBoostId());
$this->assertEquals(1682343643, $item->getAddDate());
$this->assertEquals(1725042370, $item->getExpirationDate());
$this->assertEquals(ChatBoostSourceTest::createMinInstance(), $item->getSource());
}
}
42 changes: 42 additions & 0 deletions tests/Types/ChatBoostUpdatedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace TelegramBot\Api\Test\Types;

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

class ChatBoostUpdatedTest extends AbstractTypeTest
{
protected static function getType()
{
return ChatBoostUpdated::class;
}

public static function getMinResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'boost' => ChatBoostTest::getMinResponse(),
];
}

public static function getFullResponse()
{
return [
'chat' => ChatTest::getMinResponse(),
'boost' => ChatBoostTest::getMinResponse(),
];
}

protected function assertMinItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(ChatBoostTest::createMinInstance(), $item->getBoost());
}

protected function assertFullItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals(ChatBoostTest::createMinInstance(), $item->getBoost());
}
}
6 changes: 6 additions & 0 deletions tests/Types/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public static function getFullResponse()
'chat_join_request' => ChatJoinRequestTest::getMinResponse(),
'message_reaction' => MessageReactionUpdatedTest::getMinResponse(),
'message_reaction_count' => MessageReactionCountUpdatedTest::getMinResponse(),
'chat_boost' => ChatBoostUpdatedTest::getMinResponse(),
'chat_boost_removed' => ChatBoostRemovedTest::getMinResponse(),
];
}

Expand All @@ -67,6 +69,8 @@ protected function assertMinItem($item)
$this->assertNull($item->getChatJoinRequest());
$this->assertNull($item->getMessageReaction());
$this->assertNull($item->getMessageReactionCount());
$this->assertNull($item->getChatBoost());
$this->assertNull($item->getChatBoostRemoved());
}

/**
Expand All @@ -90,5 +94,7 @@ protected function assertFullItem($item)
$this->assertEquals(ChatJoinRequestTest::createMinInstance(), $item->getChatJoinRequest());
$this->assertEquals(MessageReactionUpdatedTest::createMinInstance(), $item->getMessageReaction());
$this->assertEquals(MessageReactionCountUpdatedTest::createMinInstance(), $item->getMessageReactionCount());
$this->assertEquals(ChatBoostUpdatedTest::createMinInstance(), $item->getChatBoost());
$this->assertEquals(ChatBoostRemovedTest::createMinInstance(), $item->getChatBoostRemoved());
}
}

0 comments on commit d9d4fe5

Please sign in to comment.