Skip to content

Commit

Permalink
feat: #465 add Link Preview Customization support 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 8b75285 commit ced9262
Show file tree
Hide file tree
Showing 8 changed files with 369 additions and 66 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ All Notable changes to `PHP Telegram Bot Api` will be documented in this file
- Add `\TelegramBot\Api\Types\Update::$messageReactionCount` field
- Add `\TelegramBot\Api\BotApi::setMessageReaction` 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

### Added
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('Validation failed. Missing required parameters: %s', $missingParams));
}

/**
Expand Down
368 changes: 315 additions & 53 deletions src/BotApi.php

Large diffs are not rendered by default.

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;
}
}
8 changes: 4 additions & 4 deletions tests/Types/ArrayOfReactionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ public function testFromResponse()
]);

$expected = [
ReactionTypeEmoji::fromResponse([
'emoji' => '👍',
'type' => 'emoji'
]),
ReactionTypeEmoji::fromResponse([
'emoji' => '👍',
'type' => 'emoji'
]),
ReactionTypeCustomEmoji::fromResponse([
'custom_emoji_id' => 'custom_emoji_123',
'type' => 'custom_emoji'
Expand Down
1 change: 0 additions & 1 deletion tests/Types/MessageReactionCountUpdatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public static function getFullResponse()
];
}


protected function assertMinItem($item)
{
$this->assertEquals(ChatTest::createMinInstance(), $item->getChat());
Expand Down
6 changes: 3 additions & 3 deletions tests/Types/MessageReactionUpdatedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static function getFullResponse()
ReactionTypeEmojiTest::getMinResponse()
],
'new_reaction' => [
ReactionTypeCustomEmojiTest::getMinResponse()
ReactionTypeCustomEmojiTest::getMinResponse()
]
];
}
Expand All @@ -50,7 +50,7 @@ protected function assertMinItem($item)
$this->assertEquals(1, $item->getMessageId());
$this->assertEquals(1682343644, $item->getDate());
$this->assertEquals([ReactionTypeEmojiTest::createMinInstance()], $item->getOldReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()],$item->getNewReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()], $item->getNewReaction());

$this->assertNull($item->getUser());
$this->assertNull($item->getActorChat());
Expand All @@ -63,6 +63,6 @@ protected function assertFullItem($item)
$this->assertEquals(UserTest::createMinInstance(), $item->getUser());
$this->assertEquals(ChatTest::createMinInstance(), $item->getActorChat());
$this->assertEquals([ReactionTypeEmojiTest::createMinInstance()], $item->getOldReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()],$item->getNewReaction());
$this->assertEquals([ReactionTypeCustomEmojiTest::createMinInstance()], $item->getNewReaction());
}
}
2 changes: 0 additions & 2 deletions tests/Types/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace TelegramBot\Api\Test\Types;

use TelegramBot\Api\Test\AbstractTypeTest;
use TelegramBot\Api\Types\MessageReactionUpdated;
use TelegramBot\Api\Types\MessageReactionCountUpdated;
use TelegramBot\Api\Test\Types\Inline\ChosenInlineResultTest;
use TelegramBot\Api\Test\Types\Inline\InlineQueryTest;
use TelegramBot\Api\Test\Types\Payments\Query\PreCheckoutQueryTest;
Expand Down

0 comments on commit ced9262

Please sign in to comment.