Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

* Add support to answer callback queries
* Add support for `sendPhoto` API method

5.3
---
Expand Down
29 changes: 29 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telegram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,35 @@ $chatMessage->options($telegramOptions);
$chatter->send($chatMessage);
```

Adding Photo to a Message
-------------------------

With a Telegram message, you can use the `TelegramOptions` class to add
[message options](https://core.telegram.org/bots/api).

```php
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;

$chatMessage = new ChatMessage('Photo Caption');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
->chatId('@symfonynotifierdev')
->parseMode('MarkdownV2')
->disableWebPagePreview(true)
->hasSpoiler(true)
->protectContent(true)
->photo('https://symfony.com/favicons/android-chrome-192x192.png');

// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);

$chatter->send($chatMessage);
```

Updating Messages
-----------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ public function disableNotification(bool $bool): static
return $this;
}

/**
* @return $this
*/
public function protectContent(bool $bool): static
{
$this->options['protect_content'] = $bool;

return $this;
}

/**
* Work only when photo option is defined.
*
* @return $this
*/
public function hasSpoiler(bool $bool): static
{
$this->options['has_spoiler'] = $bool;

return $this;
}

/**
* @return $this
*/
public function photo(string $url): static
{
$this->options['photo'] = $url;

return $this;
}

/**
* @return $this
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ protected function doSend(MessageInterface $message): SentMessage
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
}

if (isset($options['photo'])) {
$options['caption'] = $options['text'];
unset($options['text']);
}

$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $this->getPath($options));

$response = $this->client->request('POST', $endpoint, [
Expand Down Expand Up @@ -117,6 +122,7 @@ private function getPath(array $options): string
return match (true) {
isset($options['message_id']) => 'editMessageText',
isset($options['callback_query_id']) => 'answerCallbackQuery',
isset($options['photo']) => 'sendPhoto',
default => 'sendMessage',
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,85 @@ public function testSendWithMarkdownShouldEscapeSpecialCharacters()

$transport->send(new ChatMessage('I contain special characters _ * [ ] ( ) ~ ` > # + - = | { } . ! to send.'));
}

public function testSendPhotoWithOptions()
{
$response = $this->createMock(ResponseInterface::class);
$response->expects($this->exactly(2))
->method('getStatusCode')
->willReturn(200);

$content = <<<JSON
{
"ok": true,
"result": {
"message_id": 1,
"from": {
"id": 12345678,
"is_bot": true,
"first_name": "YourBot",
"username": "YourBot"
},
"chat": {
"id": 1234567890,
"first_name": "John",
"last_name": "Doe",
"username": "JohnDoe",
"type": "private"
},
"date": 1459958199,
"photo": [
{
"file_id": "ABCDEF",
"file_unique_id" : "ABCDEF1",
"file_size": 1378,
"width": 90,
"height": 51
},
{
"file_id": "ABCDEF",
"file_unique_id" : "ABCDEF2",
"file_size": 19987,
"width": 320,
"height": 180
}
],
"caption": "Hello from Bot!"
}
}
JSON;

$response->expects($this->once())
->method('getContent')
->willReturn($content)
;

$expectedBody = [
'photo' => 'https://image.ur.l/',
'has_spoiler' => true,
'chat_id' => 'testChannel',
'parse_mode' => 'MarkdownV2',
'caption' => 'testMessage',
];

$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
$this->assertStringEndsWith('/sendPhoto', $url);
$this->assertSame($expectedBody, json_decode($options['body'], true));

return $response;
});

$transport = self::createTransport($client, 'testChannel');

$messageOptions = new TelegramOptions();
$messageOptions
->photo('https://image.ur.l/')
->hasSpoiler(true)
;

$sentMessage = $transport->send(new ChatMessage('testMessage', $messageOptions));

$this->assertEquals(1, $sentMessage->getMessageId());
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
}
}