Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 37ec351

Browse filesBrowse files
pan93412OskarStark
andcommitted
[Notifier] Add LINE Bot bridge
Co-authored-by: Oskar Stark <oskarstark@googlemail.com>
1 parent d83167d commit 37ec351
Copy full SHA for 37ec351

17 files changed

+428
-0
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,6 +2839,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
28392839
NotifierBridge\JoliNotif\JoliNotifTransportFactory::class => 'notifier.transport_factory.joli-notif',
28402840
NotifierBridge\KazInfoTeh\KazInfoTehTransportFactory::class => 'notifier.transport_factory.kaz-info-teh',
28412841
NotifierBridge\LightSms\LightSmsTransportFactory::class => 'notifier.transport_factory.light-sms',
2842+
NotifierBridge\LineBot\LineBotTransportFactory::class => 'notifier.transport_factory.line-bot',
28422843
NotifierBridge\LineNotify\LineNotifyTransportFactory::class => 'notifier.transport_factory.line-notify',
28432844
NotifierBridge\LinkedIn\LinkedInTransportFactory::class => 'notifier.transport_factory.linked-in',
28442845
NotifierBridge\Lox24\Lox24TransportFactory::class => 'notifier.transport_factory.lox24',

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
'fake-chat' => Bridge\FakeChat\FakeChatTransportFactory::class,
3333
'firebase' => Bridge\Firebase\FirebaseTransportFactory::class,
3434
'google-chat' => Bridge\GoogleChat\GoogleChatTransportFactory::class,
35+
'line-bot' => Bridge\LineBot\LineBotTransportFactory::class,
3536
'line-notify' => Bridge\LineNotify\LineNotifyTransportFactory::class,
3637
'linked-in' => Bridge\LinkedIn\LinkedInTransportFactory::class,
3738
'mastodon' => Bridge\Mastodon\MastodonTransportFactory::class,
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.git* export-ignore

‎src/Symfony/Component/Notifier/Bridge/LineBot/.github/PULL_REQUEST_TEMPLATE.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/LineBot/.github/PULL_REQUEST_TEMPLATE.md
+8Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/Symfony/Component/Notifier/Bridge/LineBot/.github/workflows/close-pull-request.yml

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/Bridge/LineBot/.github/workflows/close-pull-request.yml
+20Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
7.2
5+
---
6+
7+
* Add LINE Bot bridge
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2024-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
+91Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LineBot;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\ChatMessage;
17+
use Symfony\Component\Notifier\Message\MessageInterface;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
23+
/**
24+
* @author Yi-Jyun Pan <me@pan93.com>
25+
*/
26+
final class LineBotTransport extends AbstractTransport
27+
{
28+
protected const HOST = 'api.line.me';
29+
30+
public function __construct(
31+
#[\SensitiveParameter] private readonly string $channelAccessToken,
32+
private readonly string $receiver,
33+
?HttpClientInterface $client = null,
34+
?EventDispatcherInterface $dispatcher = null,
35+
) {
36+
parent::__construct($client, $dispatcher);
37+
}
38+
39+
protected function doSend(MessageInterface $message): SentMessage
40+
{
41+
if (!$message instanceof ChatMessage) {
42+
throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message);
43+
}
44+
45+
$response = $this->client->request(
46+
'POST',
47+
"https://{$this->getEndpoint()}/v2/bot/message/push",
48+
[
49+
'headers' => [
50+
'Authorization' => "Bearer $this->channelAccessToken",
51+
],
52+
'json' => [
53+
'to' => $this->receiver,
54+
'messages' => [
55+
[
56+
'type' => 'text',
57+
'text' => $message->getSubject(),
58+
],
59+
],
60+
],
61+
],
62+
);
63+
64+
try {
65+
$statusCode = $response->getStatusCode();
66+
} catch (\Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface $e) {
67+
throw new TransportException('Could not reach the remote LINE server.', $response, 0, $e);
68+
}
69+
70+
if (200 !== $statusCode) {
71+
$originalContent = $message->getSubject();
72+
73+
$result = $response->toArray(false) ?: ['message' => ''];
74+
$errorMessage = trim($result['message']);
75+
76+
throw new TransportException("Unable to post the LINE message: \"$originalContent\" ($statusCode: \"$errorMessage\").", $response);
77+
}
78+
79+
return new SentMessage($message, (string) $this);
80+
}
81+
82+
public function supports(MessageInterface $message): bool
83+
{
84+
return $message instanceof ChatMessage;
85+
}
86+
87+
public function __toString(): string
88+
{
89+
return "linebot://{$this->getEndpoint()}?receiver=$this->receiver";
90+
}
91+
}
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LineBot;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
18+
/**
19+
@author Yi-Jyun Pan <me@pan93.com>
20+
*/
21+
final class LineBotTransportFactory extends AbstractTransportFactory
22+
{
23+
private const SCHEME = 'linebot';
24+
25+
protected function getSupportedSchemes(): array
26+
{
27+
return [self::SCHEME];
28+
}
29+
30+
public function create(Dsn $dsn): LineBotTransport
31+
{
32+
if (self::SCHEME !== $dsn->getScheme()) {
33+
throw new UnsupportedSchemeException($dsn, self::SCHEME, $this->getSupportedSchemes());
34+
}
35+
36+
$accessToken = $dsn->getUser();
37+
$receiver = $dsn->getRequiredOption('receiver');
38+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
39+
$port = $dsn->getPort();
40+
41+
return (new LineBotTransport($accessToken, $receiver, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
42+
}
43+
}
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
LINE Bot Bridge
2+
=============
3+
4+
Provides [LINE Bot (Push Message)](https://developers.line.biz/en/reference/messaging-api/#send-push-message) integration for Symfony Notifier.
5+
6+
DSN example
7+
-----------
8+
9+
```
10+
linebot://TOKEN@default?receiver=RECEIVER
11+
```
12+
13+
`RECEIVER` can be retrieved from <https://developers.line.biz/en/docs/messaging-api/getting-user-ids/#getting-user-ids>.
14+
15+
`TOKEN` should be encoded in URL format.
16+
17+
Resources
18+
---------
19+
20+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
21+
* [Report issues](https://github.com/symfony/symfony/issues) and
22+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
23+
in the [main Symfony repository](https://github.com/symfony/symfony)
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LineBot\Tests;
13+
14+
use Symfony\Component\Notifier\Bridge\LineBot\LineBotTransportFactory;
15+
use Symfony\Component\Notifier\Test\AbstractTransportFactoryTestCase;
16+
use Symfony\Component\Notifier\Test\IncompleteDsnTestTrait;
17+
use Symfony\Component\Notifier\Transport\Dsn;
18+
19+
/**
20+
* @author Yi-Jyun Pan <me@pan93.com>
21+
*/
22+
final class LineBotTransportFactoryTest extends AbstractTransportFactoryTestCase
23+
{
24+
use IncompleteDsnTestTrait;
25+
26+
private const MOCK_TOKEN = 'eyJhbGciOiJIUzI1NiJ9.eyJSb2xlIjoiQWRtaW4iL+CJJc3N1ZXIiOiJJc3N1ZXIiLCJVc2VybmFtZSI6IkphdmFJblVzZSIsImV4cCI6MTcyODU1MjA3OSwiaW+F0IjoxNzI4NTUyMDc5fQ.SPKpGKwsXBay2uXDh7tATW20S2vZpw9qcmYjNp46Ir/AB/12345677=';
27+
28+
public function createFactory(): LineBotTransportFactory
29+
{
30+
return new LineBotTransportFactory();
31+
}
32+
33+
public static function supportsProvider(): iterable
34+
{
35+
yield [true, 'linebot://host?receiver=abc&token=token'];
36+
yield [true, 'linebot://host'];
37+
yield [false, 'somethingElse://host'];
38+
}
39+
40+
public static function createProvider(): iterable
41+
{
42+
$encodedToken = urlencode(self::MOCK_TOKEN);
43+
44+
yield [
45+
'linebot://api.line.me?receiver=test',
46+
'linebot://'.$encodedToken.'@default?receiver=test',
47+
];
48+
}
49+
50+
public static function incompleteDsnProvider(): iterable
51+
{
52+
yield 'missing token' => ['linebot://host.test'];
53+
}
54+
55+
public static function unsupportedSchemeProvider(): iterable
56+
{
57+
yield ['somethingElse://token@host'];
58+
yield ['somethingElse://token@host?receiver=abc&token=token'];
59+
}
60+
61+
public function testDsnToken()
62+
{
63+
$encodedToken = urlencode(self::MOCK_TOKEN);
64+
65+
$uri = "linebot://$encodedToken@default?receiver=test";
66+
$dsn = new Dsn($uri);
67+
68+
$this->assertSame(self::MOCK_TOKEN, $dsn->getUser());
69+
}
70+
}
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Bridge\LineBot\Tests;
13+
14+
use Symfony\Component\HttpClient\MockHttpClient;
15+
use Symfony\Component\Notifier\Bridge\LineBot\LineBotTransport;
16+
use Symfony\Component\Notifier\Exception\TransportException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Message\SmsMessage;
19+
use Symfony\Component\Notifier\Test\TransportTestCase;
20+
use Symfony\Component\Notifier\Tests\Transport\DummyMessage;
21+
use Symfony\Contracts\HttpClient\HttpClientInterface;
22+
use Symfony\Contracts\HttpClient\ResponseInterface;
23+
24+
/**
25+
* @author Yi-Jyun Pan <me@pan93.com>
26+
*/
27+
final class LineBotTransportTest extends TransportTestCase
28+
{
29+
public static function createTransport(?HttpClientInterface $client = null): LineBotTransport
30+
{
31+
return (new LineBotTransport('testToken', 'testReceiver', $client ?? new MockHttpClient()))->setHost('host.test');
32+
}
33+
34+
public static function toStringProvider(): iterable
35+
{
36+
yield ['linebot://host.test?receiver=testReceiver', self::createTransport()];
37+
}
38+
39+
public static function supportedMessagesProvider(): iterable
40+
{
41+
yield [new ChatMessage('Hello!')];
42+
}
43+
44+
public static function unsupportedMessagesProvider(): iterable
45+
{
46+
yield [new SmsMessage('0611223344', 'Hello!')];
47+
yield [new DummyMessage()];
48+
}
49+
50+
public function testSendWithErrorResponseThrows()
51+
{
52+
$response = $this->createMock(ResponseInterface::class);
53+
$response->expects($this->exactly(2))
54+
->method('getStatusCode')
55+
->willReturn(400);
56+
$response->expects($this->once())
57+
->method('getContent')
58+
->willReturn(json_encode(['message' => 'testDescription']));
59+
60+
$client = new MockHttpClient(static fn (): ResponseInterface => $response);
61+
62+
$transport = $this->createTransport($client);
63+
64+
$this->expectException(TransportException::class);
65+
$this->expectExceptionMessageMatches('/testMessage.+400: "testDescription"/');
66+
67+
$transport->send(new ChatMessage('testMessage'));
68+
}
69+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.