Skip to content

[Notifier] Add options to Telegram Bridge for sending Location #51716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 5 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.4
---

* Add support for `sendLocation` API method

6.3
---

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

Adding Location 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('');

// Create Telegram options
$telegramOptions = (new TelegramOptions())
->chatId('@symfonynotifierdev')
->parseMode('MarkdownV2')
->location(48.8566, 2.3522);

// 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 @@ -156,4 +156,15 @@ public function answerCallbackQuery(string $callbackQueryId, bool $showAlert = f

return $this;
}

/**
* @return $this
*/
public function location(float $latitude, float $longitude): static
{
$this->options['latitude'] = $latitude;
$this->options['longitude'] = $longitude;

return $this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Notifier\Bridge\Telegram;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\ChatMessage;
Expand Down Expand Up @@ -116,6 +115,7 @@ private function getPath(array $options): string
isset($options['message_id']) => 'editMessageText',
isset($options['callback_query_id']) => 'answerCallbackQuery',
isset($options['photo']) => 'sendPhoto',
(isset($options['longitude']) && isset($options['latitude'])) => 'sendLocation',
default => 'sendMessage',
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,70 @@ public function testSendPhotoWithOptions()
$this->assertEquals(1, $sentMessage->getMessageId());
$this->assertEquals('telegram://api.telegram.org?channel=testChannel', $sentMessage->getTransport());
}

public function testSendLocationWithOptions()
{
$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,
"location": {
"latitude": 48.8566,
"longitude": 2.3522
}
}
}
JSON;

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

$expectedBody = [
'latitude' => 48.8566,
'longitude' => 2.3522,
'chat_id' => 'testChannel',
'parse_mode' => 'MarkdownV2',
];

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

return $response;
});

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

$messageOptions = new TelegramOptions();
$messageOptions
->location(48.8566, 2.3522)
;

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

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