Skip to content

Commit c77c4e1

Browse files
committed
[Notifier] Add options to Telegram Bridge
1 parent 1f4a1bc commit c77c4e1

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed

src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add support to answer callback queries
8+
* Add support for `sendPhoto` API method
89

910
5.3
1011
---

src/Symfony/Component/Notifier/Bridge/Telegram/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,35 @@ $chatMessage->options($telegramOptions);
4747
$chatter->send($chatMessage);
4848
```
4949

50+
Adding Photo to a Message
51+
-------------------------
52+
53+
With a Telegram message, you can use the `TelegramOptions` class to add
54+
[message options](https://core.telegram.org/bots/api).
55+
56+
```php
57+
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
58+
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
59+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
60+
use Symfony\Component\Notifier\Message\ChatMessage;
61+
62+
$chatMessage = new ChatMessage('Photo Caption');
63+
64+
// Create Telegram options
65+
$telegramOptions = (new TelegramOptions())
66+
->chatId('@symfonynotifierdev')
67+
->parseMode('MarkdownV2')
68+
->disableWebPagePreview(true)
69+
->hasSpoiler(true)
70+
->protectContent(true)
71+
->photoUrl('https://symfony.com/favicons/android-chrome-192x192.png');
72+
73+
// Add the custom options to the chat message and send the message
74+
$chatMessage->options($telegramOptions);
75+
76+
$chatter->send($chatMessage);
77+
```
78+
5079
Updating Messages
5180
-----------------
5281

src/Symfony/Component/Notifier/Bridge/Telegram/TelegramOptions.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,38 @@ public function disableNotification(bool $bool): static
8080
return $this;
8181
}
8282

83+
/**
84+
* @return $this
85+
*/
86+
public function protectContent(bool $bool): static
87+
{
88+
$this->options['protect_content'] = $bool;
89+
90+
return $this;
91+
}
92+
93+
/**
94+
* Work only when photo option is defined.
95+
*
96+
* @return $this
97+
*/
98+
public function hasSpoiler(bool $bool): static
99+
{
100+
$this->options['has_spoiler'] = $bool;
101+
102+
return $this;
103+
}
104+
105+
/**
106+
* @return $this
107+
*/
108+
public function photo(string $url): static
109+
{
110+
$this->options['photo'] = $url;
111+
112+
return $this;
113+
}
114+
83115
/**
84116
* @return $this
85117
*/

src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ protected function doSend(MessageInterface $message): SentMessage
8484
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
8585
}
8686

87+
if (isset($options['photo'])) {
88+
$options['caption'] = $options['text'];
89+
unset($options['text']);
90+
}
91+
8792
$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $this->getPath($options));
8893

8994
$response = $this->client->request('POST', $endpoint, [
@@ -117,6 +122,7 @@ private function getPath(array $options): string
117122
return match (true) {
118123
isset($options['message_id']) => 'editMessageText',
119124
isset($options['callback_query_id']) => 'answerCallbackQuery',
125+
isset($options['photo']) => 'sendPhoto',
120126
default => 'sendMessage',
121127
};
122128
}

src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,4 +330,85 @@ public function testSendWithMarkdownShouldEscapeSpecialCharacters()
330330

331331
$transport->send(new ChatMessage('I contain special characters _ * [ ] ( ) ~ ` > # + - = | { } . ! to send.'));
332332
}
333+
334+
public function testSendPhotoWithOptions()
335+
{
336+
$response = $this->createMock(ResponseInterface::class);
337+
$response->expects($this->exactly(2))
338+
->method('getStatusCode')
339+
->willReturn(200);
340+
341+
$content = <<<JSON
342+
{
343+
"ok": true,
344+
"result": {
345+
"message_id": 1,
346+
"from": {
347+
"id": 12345678,
348+
"is_bot": true,
349+
"first_name": "YourBot",
350+
"username": "YourBot"
351+
},
352+
"chat": {
353+
"id": 1234567890,
354+
"first_name": "John",
355+
"last_name": "Doe",
356+
"username": "JohnDoe",
357+
"type": "private"
358+
},
359+
"date": 1459958199,
360+
"photo": [
361+
{
362+
"file_id": "ABCDEF",
363+
"file_unique_id" : "ABCDEF1",
364+
"file_size": 1378,
365+
"width": 90,
366+
"height": 51
367+
},
368+
{
369+
"file_id": "ABCDEF",
370+
"file_unique_id" : "ABCDEF2",
371+
"file_size": 19987,
372+
"width": 320,
373+
"height": 180
374+
}
375+
],
376+
"caption": "Hello from Bot!"
377+
}
378+
}
379+
JSON;
380+
381+
$response->expects($this->once())
382+
->method('getContent')
383+
->willReturn($content)
384+
;
385+
386+
$expectedBody = [
387+
'photo' => 'https://image.ur.l/',
388+
'has_spoiler' => true,
389+
'chat_id' => 'testChannel',
390+
'parse_mode' => 'MarkdownV2',
391+
'caption' => 'testMessage',
392+
];
393+
394+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
395+
$this->assertStringEndsWith('/sendPhoto', $url);
396+
$this->assertSame($expectedBody, json_decode($options['body'], true));
397+
398+
return $response;
399+
});
400+
401+
$transport = self::createTransport($client, 'testChannel');
402+
403+
$messageOptions = new TelegramOptions();
404+
$messageOptions
405+
->photo('https://image.ur.l/')
406+
->hasSpoiler(true)
407+
;
408+
409+
$sentMessage = $transport->send(new ChatMessage('testMessage', $messageOptions));
410+
411+
$this->assertEquals(1, $sentMessage->getMessageId());
412+
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
413+
}
333414
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy