Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Notifier] [Telegram] Add support for local API server #57769

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

Open
wants to merge 1 commit into
base: 7.4
Choose a base branch
from
Open
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
=========

7.3
---

* Add a DSN option/flag `disableHttps` to allow the disabling of bridge's default behavior of using `https` protocol

6.4
---

Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Notifier/Bridge/Telegram/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ where:
- `TOKEN` is your Telegram token
- `CHAT_ID` is your Telegram chat id
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


Interacting with local API server instead of official Telegram API
------------------------------------------------------------------

If such a case is needed, you can replace the `default` keyword in the DSN
with the desired domain/IP address of your local API server. You may also want to
disable the bridge's default behavior of using `https` protocol as local API servers
can only accept `http` traffic.

Example:
```
TELEGRAM_DSN=telegram://TOKEN@localhost:5001?channel=CHAT_ID&disable_https=1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In AmazonSns, this option is named sslmode. I don't know if this name is better, but I think it's better to be consistent between adapters.

$protocol = 'disable' === $dsn->getOption('sslmode') ? 'http' : 'https';

This would become:

Suggested change
TELEGRAM_DSN=telegram://TOKEN@localhost:5001?channel=CHAT_ID&disable_https=1
TELEGRAM_DSN=telegram://TOKEN@localhost:5001?channel=CHAT_ID&sslmode=disable

Copy link
Member

@GromNaN GromNaN Feb 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you've already changed the name in response to our request #57769 (comment), but perhaps the consistency with other bridges hadn't been examined.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like sslmode 👍

```

Caution: Disabling the use of the `https` protocol can pose a security risk.
You should only do this if your local API server is hosted somehow internally
and the traffic will remain within a secure environment.

Otherwise, you may want to implement a TLS-termination proxy in front of
your server for handling the encryption and decryption of the traffic,
So you can continue using it normally over `https` protocol.

Adding Interactions to a Message
--------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,25 @@ public function __construct(
private ?string $chatChannel = null,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
private readonly bool $disableHttps = false,
) {
parent::__construct($client, $dispatcher);
}

public function __toString(): string
{
if (null === $this->chatChannel) {
return \sprintf('telegram://%s', $this->getEndpoint());
$toString = \sprintf('telegram://%s', $this->getEndpoint());

$formattedOptions = http_build_query([
'channel' => $this->chatChannel,
'disable_https' => $this->disableHttps ?: null,
], '', '&');

if ('' !== $formattedOptions) {
$toString .= \sprintf('?%s', $formattedOptions);
}

return \sprintf('telegram://%s?channel=%s', $this->getEndpoint(), $this->chatChannel);
return $toString;
}

public function supports(MessageInterface $message): bool
Expand Down Expand Up @@ -94,7 +102,7 @@ protected function doSend(MessageInterface $message): SentMessage
* - __underlined text__
* - various notations of images, f. ex. [title](url)
* - `code samples`.
*
*
* These formats should be taken care of when the message is constructed.
*
* @see https://core.telegram.org/bots/api#markdownv2-style
Expand All @@ -117,8 +125,9 @@ protected function doSend(MessageInterface $message): SentMessage
$method = $this->getPath($options);
$this->ensureExclusiveOptionsNotDuplicated($options);
$options = $this->expandOptions($options, 'contact', 'location', 'venue');
$protocolSchema = $this->disableHttps ? 'http' : 'https';

$endpoint = \sprintf('https://%s/bot%s/%s', $this->getEndpoint(), $this->token, $method);
$endpoint = \sprintf('%s://%s/bot%s/%s', $protocolSchema, $this->getEndpoint(), $this->token, $method);

$response = $this->client->request('POST', $endpoint, [
$optionsContainer => array_filter($options),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,17 @@ final class TelegramTransportFactory extends AbstractTransportFactory
{
public function create(Dsn $dsn): TelegramTransport
{
$scheme = $dsn->getScheme();

if ('telegram' !== $scheme) {
if ('telegram' !== $dsn->getScheme()) {
throw new UnsupportedSchemeException($dsn, 'telegram', $this->getSupportedSchemes());
}

$token = $this->getToken($dsn);
$channel = $dsn->getOption('channel');
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
$port = $dsn->getPort();
$disableHttps = filter_var($dsn->getOption('disable_https'), \FILTER_VALIDATE_BOOLEAN);

return (new TelegramTransport($token, $channel, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
return (new TelegramTransport($token, $channel, $this->client, $this->dispatcher, $disableHttps))->setHost($host)->setPort($port);
}

protected function getSupportedSchemes(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ public function createFactory(): TelegramTransportFactory

public static function createProvider(): iterable
{
yield [
'telegram://host.test?channel=testChannel',
'telegram://user:[email protected]?channel=testChannel',
];
yield ['telegram://host.test?channel=testChannel', 'telegram://user:[email protected]?channel=testChannel'];

// Tests for `disable_https` option
yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:[email protected]?channel=testChannel&disable_https=1'];
yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:[email protected]?channel=testChannel&disable_https=yes'];
yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:[email protected]?channel=testChannel&disable_https=on'];
yield ['telegram://host.test?channel=testChannel', 'telegram://user:[email protected]?channel=testChannel&disable_https=0'];
yield ['telegram://host.test?channel=testChannel', 'telegram://user:[email protected]?channel=testChannel&disable_https=no'];
yield ['telegram://host.test?channel=testChannel', 'telegram://user:[email protected]?channel=testChannel&disable_https=off'];
yield ['telegram://host.test?channel=testChannel', 'telegram://user:[email protected]?channel=testChannel&disable_https=random-string'];
}

public static function supportsProvider(): iterable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,17 @@ final class TelegramTransportTest extends TransportTestCase
{
private const FIXTURE_FILE = __DIR__.'/Fixtures/image.png';

public static function createTransport(?HttpClientInterface $client = null, ?string $channel = null): TelegramTransport
public static function createTransport(?HttpClientInterface $client = null, ?string $channel = null, bool $disableHttps = false): TelegramTransport
{
return new TelegramTransport('token', $channel, $client ?? new MockHttpClient());
return new TelegramTransport('token', $channel, $client ?? new MockHttpClient(), disableHttps: $disableHttps);
}

public static function toStringProvider(): iterable
{
yield ['telegram://api.telegram.org', self::createTransport()];
yield ['telegram://api.telegram.org?channel=testChannel', self::createTransport(null, 'testChannel')];
yield ['telegram://api.telegram.org?disable_https=1', self::createTransport(null, null, true)];
yield ['telegram://api.telegram.org?channel=testChannel&disable_https=1', self::createTransport(null, 'testChannel', true)];
}

public static function supportedMessagesProvider(): iterable
Expand Down