Skip to content

Commit 8c8ccde

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

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
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: 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function getPath(array $options): string
116116
isset($options['message_id']) => 'editMessageText',
117117
isset($options['callback_query_id']) => 'answerCallbackQuery',
118118
isset($options['photo']) => 'sendPhoto',
119+
(isset($options['longitude']) && isset($options['latitude'])) => 'sendLocation',
119120
default => 'sendMessage',
120121
};
121122
}

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

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

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