diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md b/src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md index 749784f093a7e..d93bebaa286fd 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/CHANGELOG.md @@ -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 --- diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/README.md b/src/Symfony/Component/Notifier/Bridge/Telegram/README.md index f2bf849a66e2b..60099b7f274e6 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/README.md +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/README.md @@ -14,6 +14,27 @@ where: - `TOKEN` is your Telegram token - `CHAT_ID` is your Telegram chat id +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 +``` + +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 -------------------------------- diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php index c1b38211d44f2..3128d6c5e8364 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransport.php @@ -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 @@ -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 @@ -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), diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransportFactory.php index 610146dc8312f..c7e5606ede14a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/TelegramTransportFactory.php @@ -23,9 +23,7 @@ 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()); } @@ -33,8 +31,9 @@ public function create(Dsn $dsn): TelegramTransport $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 diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportFactoryTest.php index 278a315c65dba..ccd8a463d7d91 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportFactoryTest.php @@ -26,10 +26,16 @@ public function createFactory(): TelegramTransportFactory public static function createProvider(): iterable { - yield [ - 'telegram://host.test?channel=testChannel', - 'telegram://user:password@host.test?channel=testChannel', - ]; + yield ['telegram://host.test?channel=testChannel', 'telegram://user:password@host.test?channel=testChannel']; + + // Tests for `disable_https` option + yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:password@host.test?channel=testChannel&disable_https=1']; + yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:password@host.test?channel=testChannel&disable_https=yes']; + yield ['telegram://host.test?channel=testChannel&disable_https=1', 'telegram://user:password@host.test?channel=testChannel&disable_https=on']; + yield ['telegram://host.test?channel=testChannel', 'telegram://user:password@host.test?channel=testChannel&disable_https=0']; + yield ['telegram://host.test?channel=testChannel', 'telegram://user:password@host.test?channel=testChannel&disable_https=no']; + yield ['telegram://host.test?channel=testChannel', 'telegram://user:password@host.test?channel=testChannel&disable_https=off']; + yield ['telegram://host.test?channel=testChannel', 'telegram://user:password@host.test?channel=testChannel&disable_https=random-string']; } public static function supportsProvider(): iterable diff --git a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php index 5b798e5fca70d..ff57547fb67c5 100644 --- a/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Telegram/Tests/TelegramTransportTest.php @@ -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
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: