8000 [Notifier] Add options to Telegram Bridge · symfony/symfony@9021ed6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9021ed6

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

File tree

5 files changed

+178
-5
lines changed

5 files changed

+178
-5
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 to 'sendPhoto' api method
89

910
5.3
1011
---

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,34 @@ $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+
->photoUrl('https://symfony.com/favicons/android-chrome-192x192.png');
71+
72+
// Add the custom options to the chat message and send the message
73+
$chatMessage->options($telegramOptions);
74+
75+
$chatter->send($chatMessage);
76+
```
77+
5078
Updating Messages
5179
-----------------
5280

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 photoUrl is not null
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 photoUrl(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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,22 @@ protected function doSend(MessageInterface $message): SentMessage
7777
$options['chat_id'] = $message->getRecipientId() ?: $this->chatChannel;
7878
}
7979

80-
$options['text'] = $message->getSubject();
80+
$subject = $message->getSubject();
8181

8282
if (!isset($options['parse_mode']) || TelegramOptions::PARSE_MODE_MARKDOWN_V2 === $options['parse_mode']) {
8383
$options['parse_mode'] = TelegramOptions::PARSE_MODE_MARKDOWN_V2;
84-
$options['text'] = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $message->getSubject());
84+
85+
$subject = preg_replace('/([_*\[\]()~`>#+\-=|{}.!])/', '\\\\$1', $subject);
8586
}
8687

88+
$optionKey = 'text';
89+
90+
if (isset($options['photo'])) {
91+
$optionKey = 'caption';
92+
}
93+
94+
$options[$optionKey] = $subject;
95+
8796
$endpoint = sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $this->getPath($options));
8897

8998
$response = $this->client->request('POST', $endpoint, [
@@ -117,6 +126,7 @@ private function getPath(array $options): string
117126
return match (true) {
118127
isset($options['message_id']) => 'editMessageText',
119128
isset($options['callback_query_id']) => 'answerCallbackQuery',
129+
isset($options['photo']) => 'sendPhoto',
120130
default => 'sendMessage',
121131
};
122132
}

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

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ public function testSendWithOptions()
123123

124124
$expectedBody = [
125125
'chat_id' => 'testChannel',
126-
'text' => 'testMessage',
127126
'parse_mode' => 'MarkdownV2',
127+
'text' => 'testMessage',
128128
];
129129

130130
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
@@ -258,8 +258,8 @@ public function testSendWithChannelOverride()
258258

259259
$expectedBody = [
260260
'chat_id' => $channelOverride,
261-
'text' => 'testMessage',
262261
'parse_mode' => 'MarkdownV2',
262+
'text' => 'testMessage',
263263
];
264264

265265
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
@@ -316,8 +316,8 @@ public function testSendWithMarkdownShouldEscapeSpecialCharacters()
316316

317317
$expectedBody = [
318318
'chat_id' => 'testChannel',
319-
'text' => 'I contain special characters \_ \* \[ \] \( \) \~ \` \> \# \+ \- \= \| \{ \} \. \! to send\.',
320319
'parse_mode' => 'MarkdownV2',
320+
'text' => 'I contain special characters \_ \* \[ \] \( \) \~ \` \> \# \+ \- \= \| \{ \} \. \! to send\.',
321321
];
322322

323323
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
@@ -330,4 +330,106 @@ 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+
"file_id": "ABCDEF",
377+
"file_unique_id" : "ABCDEF3",
378+
"file_size": 81498,
379+
"width": 800,
380+
"height": 450
381+
},
382+
{
383+
"file_id": "ABCDEF",
384+
"file_unique_id" : "ABCDEF4",
385+
"file_size": 161261,
386+
"width": 1280,
387+
"height": 720
388+
},
389+
{
390+
"file_id": "ABCDEF",
391+
"file_unique_id" : "ABCDEF5",
392+
"file_size": 397069,
393+
"width": 2560,
394+
"height": 1440
395+
}
396+
],
397+
"caption": "Hello from Bot!"
398+
}
399+
}
400+
JSON;
401+
402+
$response->expects($this->once())
403+
->method('getContent')
404+
->willReturn($content)
405+
;
406+
407+
$expectedBody = [
408+
'photo' => 'https://image.ur.l/',
409+
'has_spoiler' => true,
410+
'chat_id' => 'testChannel',
411+
'parse_mode' => 'MarkdownV2',
412+
'caption' => 'testMessage',
413+
];
414+
415+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
416+
$this->assertStringEndsWith('/sendPhoto', $url);
417+
$this->assertSame($expectedBody, json_decode($options['body'], true));
418+
419+
return $response;
420+
});
421+
422+
$transport = self::createTransport($client, 'testChannel');
423+
424+
$messageOptions = new TelegramOptions();
425+
$messageOptions
426+
->photoUrl('https://image.ur.l/')
427+
->hasSpoiler(true)
428+
;
429+
430+
$sentMessage = $transport->send(new ChatMessage('testMessage', $messageOptions));
431+
432+
$this->assertEquals(1, $sentMessage->getMessageId());
433+
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
434+
}
333435
}

0 commit comments

Comments
 (0)
0