8000 [Notifier] Add options to Telegram Bridge for sending Location · symfony/symfony@b02d5c5 · GitHub
[go: up one dir, main page]

Skip to content

Commit b02d5c5

Browse files
committed
[Notifier] Add options to Telegram Bridge for sending Location
1 parent 7d310a3 commit b02d5c5

File tree

5 files changed

+109
-1
lines changed

5 files changed

+109
-1
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
6.4
5+
---
6+
7+
* Add support for `sendLocation` API method
8+
49
6.3
510
---
611

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ $chatMessage->options($telegramOptions);
7676
$chatter->send($chatMessage);
7777
```
7878

79+
Adding Location to a Message
80+
----------------------------
81+
82+
With a Telegram message, you can use the `TelegramOptions` class to add
83+
[message options](https://core.telegram.org/bots/api).
84+
85+
```php
86+
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
87+
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
88+
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
89+
use Symfony\Component\Notifier\Message\ChatMessage;
90+
91+
$chatMessage = new ChatMessage('');
92+
93+
// Create Telegram options
94+
$telegramOptions = (new TelegramOptions())
95+
->chatId('@symfonynotifierdev')
96+
->parseMode('MarkdownV2')
97+
->location(48.8566, 2.3522);
98+
99+
// Add the custom options to the chat message and send the message
100+
$chatMessage->options($telegramOptions);
101+
102+
$chatter->send($chatMessage);
103+
```
104+
79105
Updating Messages
80106
-----------------
81107

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

Lines changed: 10000 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,15 @@ public function answerCallbackQuery(string $callbackQueryId, bool $showAlert = f
156156

157157
return $this;
158158
}
159+
160+
/**
161+
* @return $this
162+
*/
163+
public function location(float $latitude, float $longitude): static
164+
{
165+
$this->options['latitude'] = $latitude;
166+
$this->options['longitude'] = $longitude;
167+
168+
return $this;
169+
}
159170
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Notifier\Bridge\Telegram;
1313

14-
use Symfony\Component\Notifier\Exception\LogicException;
1514
use Symfony\Component\Notifier\Exception\TransportException;
1615
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
1716
use Symfony\Component\Notifier\Message\ChatMessage;
@@ -116,6 +115,7 @@ private function getPath(array $options): string
116115
isset($options['message_id']) => 'editMessageText',
117116
isset($options['callback_query_id']) => 'answerCallbackQuery',
118117
isset($options['photo']) => 'sendPhoto',
118+
(isset($options['longitude']) && isset($options['latitude'])) => 'sendLocation',
119119
default => 'sendMessage',
120120
};
121121
}

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

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,70 @@ public function testSendPhotoWithOptions()
411411
$this->assertEquals(1, $sentMessage->getMessageId());
412412
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
413413
}
414+
415+
public function testSendLocationWithOptions()
416+
{
417+
$response = $this->createMock(ResponseInterface::class);
418+
$response->expects($this->exactly(2))
419+
->method('getStatusCode')
420+
->willReturn(200);
421+
422+
$content = <<<JSON
423+
{
424+
"ok": true,
425+
"result": {
426+
"message_id": 1,
427+
"from": {
428+
"id": 12345678,
429+
"is_bot": true,
430+
"first_name": "YourBot",
431+
"username": "YourBot"
432+
},
433+
"chat": {
434+
"id": 1234567890,
435+
"first_name": "John",
436+
"last_name": "Doe",
437+
"username": "JohnDoe",
438+
"type": "private"
439+
},
440+
"date": 1459958199,
441+
"location": {
442+
"latitude": 48.8566,
443+
"longitude": 2.3522
444+
}
445+
}
446+
}
447+
JSON;
448+
449+
$response->expects($this->once())
450+
->method('getContent')
451+
->willReturn($content)
452+
;
453+
454+
$expectedBody = [
455+
'latitude' => 48.8566,
456+
'longitude' => 2.3522,
457+
'chat_id' => 'testChannel',
458+
'parse_mode' => 'MarkdownV2',
459+
];
460+
461+
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface {
462+
$this->assertStringEndsWith('/sendLocation', $url);
463+
$this->assertSame($expectedBody, json_decode($options['body'], true));
464+
465+
return $response;
466+
});
467+
468+
$transport = self::createTransport($client, 'testChannel');
469+
470+
$messageOptions = new TelegramOptions();
471+
$messageOptions
472+
->location(48.8566, 2.3522)
473+
;
474+
475+
$sentMessage = $transport->send(new ChatMessage('', $messageOptions));
476+
477+
$this->assertEquals(1, $sentMessage->getMessageId());
478+
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
479+
}
414480
}

0 commit comments

Comments
 (0)
0