Skip to content

Commit

Permalink
feat: Integration with Business Accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
bernard-ng committed Oct 11, 2024
1 parent 36085b2 commit 1189fd4
Show file tree
Hide file tree
Showing 6 changed files with 247 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All Notable changes to `PHP Telegram Bot Api` will be documented in this file

## 3.0.0 - YYYY-MM-DD
- Add `\TelegramBot\Api\BotApi::getBusinessConnection` api method
- Add `\TelegramBot\Api\Types\Update::$deletedBusinessMessages` field
- Add `\TelegramBot\Api\Types\Update::$editedBusinessMessage` field
- Add `\TelegramBot\Api\Types\Update::$businessMessage` field
- Add `\TelegramBot\Api\Types\Update::$businessConnection` field
- Add `\TelegramBot\Api\Types\Update::$myChatMember` field
- Add `\TelegramBot\Api\Types\Update::$chatMember` field
- Add `\TelegramBot\Api\Types\Update::$chatJoinRequest` field
Expand Down
19 changes: 19 additions & 0 deletions src/BotApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use TelegramBot\Api\Http\HttpClientInterface;
use TelegramBot\Api\Types\ArrayOfBotCommand;
use TelegramBot\Api\Types\LinkPreviewOptions;
use TelegramBot\Api\Types\BusinessConnection;
use TelegramBot\Api\Types\ArrayOfReactionType;
use TelegramBot\Api\Types\ArrayOfChatMemberEntity;
use TelegramBot\Api\Types\ArrayOfMessageEntity;
Expand Down Expand Up @@ -3181,6 +3182,24 @@ public function getUserChatBoosts($chatId, $userId)
]));
}

/**
* Use this method to get information about the connection of the bot with a business account.
* Returns a BusinessConnection object on success.
*
* @param string $businessConnectionId Unique identifier for the business connection
*
* @return BusinessConnection
* @throws Exception
*
* @author bernard-ng <bernard@devscast.tech>
*/
public function getBusinessConnection($businessConnectionId)
{
return BusinessConnection::fromResponse($this->call('getBusinessConnection', [
'business_connection_id' => $businessConnectionId
]));
}

/**
* Set an option for a cURL transfer
*
Expand Down
101 changes: 101 additions & 0 deletions src/Types/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ class Update extends BaseType implements TypeInterface
'message_reaction_count' => MessageReactionCountUpdated::class,
'chat_boost' => ChatBoostUpdated::class,
'chat_boost_removed' => ChatBoostRemoved::class,
'business_connection' => BusinessConnection::class,
'business_message' => Message::class,
'edited_business_message' => Message::class,
'deleted_business_messages' => BusinessMessagesDeleted::class
];

/**
Expand Down Expand Up @@ -198,6 +202,35 @@ class Update extends BaseType implements TypeInterface
*/
protected $chatBoostRemoved;

/**
* Optional. The bot was connected to or disconnected from a business account,
* or a user edited an existing connection with the bot
*
* @var BusinessConnection|null
*/
protected $businessConnection;

/**
* Optional. New message from a connected business account
*
* @var Message|null
*/
protected $businessMessage;

/**
* Optional. New version of a message from a connected business account
*
* @var Message|null
*/
protected $editedBusinessMessage;

/**
* Optional. Messages were deleted from a connected business account
*
* @var BusinessMessagesDeleted|null
*/
protected $deletedBusinessMessages;

/**
* @return int
*/
Expand Down Expand Up @@ -540,4 +573,72 @@ public function setChatBoostRemoved($chatBoostRemoved)
{
$this->chatBoostRemoved = $chatBoostRemoved;
}

/**
* @return BusinessConnection|null
*/
public function getBusinessConnection()
{
return $this->businessConnection;
}

/**
* @param BusinessConnection|null $businessConnection
* @return void
*/
public function setBusinessConnection($businessConnection)
{
$this->businessConnection = $businessConnection;
}

/**
* @return Message|null
*/
public function getBusinessMessage()
{
return $this->businessMessage;
}

/**
* @param Message|null $businessMessage
* @return void
*/
public function setBusinessMessage($businessMessage)
{
$this->businessMessage = $businessMessage;
}

/**
* @return Message|null
*/
public function getEditedBusinessMessage()
{
return $this->editedBusinessMessage;
}

/**
* @param Message|null $editedBusinessMessage
* @return void
*/
public function setEditedBusinessMessage($editedBusinessMessage)
{
$this->editedBusinessMessage = $editedBusinessMessage;
}

/**
* @return BusinessMessagesDeleted|null
*/
public function getDeletedBusinessMessages()
{
return $this->deletedBusinessMessages;
}

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

declare(strict_types=1);

namespace TelegramBot\Api\Test\Types;

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

class BusinessConnectionTest extends AbstractTypeTest
{

protected static function getType()
{
return BusinessConnection::class;
}

public static function getMinResponse()
{
return [
'id' => 1,
'user' => UserTest::getMinResponse(),
'user_chat_id' => 1,
'date' => 1682343643,
'can_reply' => true,
'is_enabled' => true
];
}

public static function getFullResponse()
{
return [
'id' => 1,
'user' => UserTest::getMinResponse(),
'user_chat_id' => 1,
'date' => 1682343643,
'can_reply' => true,
'is_enabled' => true
];
}

protected function assertMinItem($item)
{
$this->assertEquals(1, $item->getId());
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
$this->assertEquals(1, $item->getUserChatId());
$this->assertEquals(1682343643, $item->getDate());
$this->assertTrue($item->getCanReply());
$this->assertTrue($item->getIsEnabled());
}

protected function assertFullItem($item)
{
$this->assertEquals(1, $item->getId());
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
$this->assertEquals(1, $item->getUserChatId());
$this->assertEquals(1682343643, $item->getDate());
$this->assertTrue($item->getCanReply());
$this->assertTrue($item->getIsEnabled());
}
}
48 changes: 48 additions & 0 deletions tests/Types/BusinessMessagesDeletedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace TelegramBot\Api\Test\Types;

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

class BusinessMessagesDeletedTest extends AbstractTypeTest
{
protected static function getType()
{
return BusinessMessagesDeleted::class;
}

public static function getMinResponse()
{
return [
'business_connection_id' => 'id',
'chat' => ChatTest::getMinResponse(),
'message_ids' => [1, 2, 3],
];
}

public static function getFullResponse()
{
return [
'business_connection_id' => 'id',
'chat' => ChatTest::getMinResponse(),
'message_ids' => [1, 2, 3],
];
}

protected function assertMinItem($item)
{
$this->assertEquals('id', $item->getBusinessConnectionId());
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals([1, 2, 3], $item->getMessageIds());
}

protected function assertFullItem($item)
{
$this->assertEquals('id', $item->getBusinessConnectionId());
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
$this->assertEquals([1, 2, 3], $item->getMessageIds());
}
}
13 changes: 13 additions & 0 deletions tests/Types/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\BusinessMessagesDeleted;
use TelegramBot\Api\Test\Types\Inline\ChosenInlineResultTest;
use TelegramBot\Api\Test\Types\Inline\InlineQueryTest;
use TelegramBot\Api\Test\Types\Payments\Query\PreCheckoutQueryTest;
Expand Down Expand Up @@ -43,6 +44,10 @@ public static function getFullResponse()
'message_reaction_count' => MessageReactionCountUpdatedTest::getMinResponse(),
'chat_boost' => ChatBoostUpdatedTest::getMinResponse(),
'chat_boost_removed' => ChatBoostRemovedTest::getMinResponse(),
'business_connection' => BusinessConnectionTest::getMinResponse(),
'business_message' => MessageTest::getMinResponse(),
'edited_business_message' => MessageTest::getMinResponse(),
'deleted_business_messages' => BusinessMessagesDeletedTest::getMinResponse(),
];
}

Expand Down Expand Up @@ -71,6 +76,10 @@ protected function assertMinItem($item)
$this->assertNull($item->getMessageReactionCount());
$this->assertNull($item->getChatBoost());
$this->assertNull($item->getChatBoostRemoved());
$this->assertNull($item->getBusinessConnection());
$this->assertNull($item->getBusinessMessage());
$this->assertNull($item->getEditedBusinessMessage());
$this->assertNull($item->getDeletedBusinessMessages());
}

/**
Expand All @@ -96,5 +105,9 @@ protected function assertFullItem($item)
$this->assertEquals(MessageReactionCountUpdatedTest::createMinInstance(), $item->getMessageReactionCount());
$this->assertEquals(ChatBoostUpdatedTest::createMinInstance(), $item->getChatBoost());
$this->assertEquals(ChatBoostRemovedTest::createMinInstance(), $item->getChatBoostRemoved());
$this->assertEquals(BusinessConnectionTest::createMinInstance(), $item->getBusinessConnection());
$this->assertEquals(MessageTest::createMinInstance(), $item->getBusinessMessage());
$this->assertEquals(MessageTest::createMinInstance(), $item->getEditedBusinessMessage());
$this->assertEquals(BusinessMessagesDeletedTest::createMinInstance(), $item->getDeletedBusinessMessages());
}
}

0 comments on commit 1189fd4

Please sign in to comment.