-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add reply message and join one room
- Loading branch information
1 parent
6a54849
commit 8a04ead
Showing
12 changed files
with
281 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class ReplyLineMessage | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
public string $replyToken; | ||
public array $messages; | ||
|
||
/** | ||
* | ||
* @param string $replyToken | ||
* @param array $messages | ||
* @return void | ||
*/ | ||
public function __construct(string $replyToken, array $messages) | ||
{ | ||
$this->replyToken = $replyToken; | ||
$this->messages = $messages; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\ReplyLineMessage; | ||
use App\Services\LineBotService; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Queue\InteractsWithQueue; | ||
|
||
class SendLineMessage | ||
{ | ||
protected LineBotService $service; | ||
|
||
/** | ||
* | ||
* @param LineBotService $service | ||
* @return void | ||
*/ | ||
public function __construct(LineBotService $service) | ||
{ | ||
$this->service = $service; | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
* | ||
* @param ReplyLineMessage $event | ||
* @return void | ||
*/ | ||
public function reply(ReplyLineMessage $event) | ||
{ | ||
$this->service->replyMessage($event->replyToken, $event->messages); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
/** | ||
* | ||
* @property integer $id | ||
* @property string $group_id | ||
*/ | ||
class Group extends Model | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
namespace App\Services; | ||
|
||
use App\Events\ReplyLineMessage; | ||
use App\Models\Group; | ||
use Exception; | ||
use Illuminate\Contracts\Container\BindingResolutionException; | ||
use Illuminate\Support\Facades\Http; | ||
use InvalidArgumentException; | ||
|
||
class LineBotService | ||
{ | ||
/** | ||
* | ||
* @param array $events | ||
* @return void | ||
*/ | ||
public function router(array $events) | ||
{ | ||
foreach ($events as $value) | ||
{ | ||
switch ($value['type']) | ||
{ | ||
case "join": | ||
return $this->join($value); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param array $event | ||
* @return void | ||
* @throws InvalidArgumentException | ||
* @throws BindingResolutionException | ||
*/ | ||
public function join(array $event) | ||
{ | ||
$type = $event['source']['type']; | ||
if ($type !== 'group') return; | ||
$replyToken = $event['replyToken']; | ||
$text = '我是值日生提醒小精靈,請到這裡 -> https://duty-web.ballfish.io 設定值日生'; | ||
$group = new Group(); | ||
$group->group_id = $event['source']['groupId']; | ||
$group->save(); | ||
ReplyLineMessage::dispatch($replyToken, [ | ||
[ | ||
'type' => 'text', | ||
'text' => $text | ||
] | ||
]); | ||
} | ||
|
||
/** | ||
* | ||
* @param string $replyToken | ||
* @param array $messages | ||
* @return void | ||
* @throws InvalidArgumentException | ||
* @throws Exception | ||
*/ | ||
public function replyMessage(string $replyToken, array $messages) | ||
{ | ||
$token = env('LINE_BOT_CHANNEL_ACCESS_TOKEN', ''); | ||
Http::withToken($token)->post('https://api.line.me/v2/bot/message/reply', [ | ||
'replyToken' => $replyToken, | ||
'messages' => $messages | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
database/migrations/2021_01_08_090324_create_groups_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateGroupsTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create('groups', function (Blueprint $table) { | ||
$table->id(); | ||
$table->timestamps(); | ||
$table->string('group_id'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists('groups'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Tests\Feature\Services; | ||
|
||
use App\Events\ReplyLineMessage; | ||
use App\Models\Group; | ||
use App\Services\LineBotService; | ||
use Illuminate\Http\Client\Request; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Support\Facades\Event; | ||
use Illuminate\Support\Facades\Http; | ||
use Tests\TestCase; | ||
|
||
use function PHPUnit\Framework\assertEquals; | ||
|
||
class LineBotTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testJoin() | ||
{ | ||
Event::fake(); | ||
$service = new LineBotService(); | ||
$service->join([ | ||
'replyToken' => 'test', | ||
'type' => 'join', | ||
'source' => [ | ||
'type' => 'group', | ||
'groupId' => 'test' | ||
] | ||
]); | ||
$count = Group::where('group_id', 'test')->get()->count(); | ||
assertEquals(1, $count); | ||
Event::assertDispatched(function (ReplyLineMessage $event) | ||
{ | ||
return $event->replyToken === 'test' && | ||
$event->messages[0]['text'] === '我是值日生提醒小精靈,請到這裡 -> https://duty-web.ballfish.io 設定值日生'; | ||
}); | ||
} | ||
|
||
public function testNotJoin() | ||
{ | ||
Event::fake(); | ||
$service = new LineBotService(); | ||
$service->join([ | ||
'replyToken' => 'test', | ||
'type' => 'join', | ||
'source' => [ | ||
'type' => 'room', | ||
'groupId' => 'test' | ||
] | ||
]); | ||
$count = Group::where('group_id', 'test')->get()->count(); | ||
assertEquals(0, $count); | ||
Event::assertNotDispatched(ReplyLineMessage::class); | ||
} | ||
|
||
public function testReplyMessage() | ||
{ | ||
Http::fake(); | ||
$service = new LineBotService(); | ||
$service->replyMessage('test', [ | ||
[ | ||
"type" => "text", | ||
"text" => "test" | ||
] | ||
]); | ||
Http::assertSent(function (Request $request) | ||
{ | ||
$token = env('LINE_BOT_CHANNEL_ACCESS_TOKEN', ''); | ||
return $request->hasHeader('Authorization', 'Bearer '.$token) && | ||
$request->url() === 'https://api.line.me/v2/bot/message/reply' && | ||
$request->method() === 'POST' && | ||
$request['replyToken'] === 'test' && | ||
is_array($request['messages']); | ||
}); | ||
} | ||
} |