Webhook
이 패키지는 Webhook 라우팅과 컨트롤러를 제공합니다.
Webhook URL
기본 Webhook URL은 다음과 같습니다.
https://example.com/line/webhook
.env에서 경로를 변경할 수 있습니다.
LINE_BOT_WEBHOOK_PATH=webhook
Laravel 이벤트 시스템과의 연동
Webhook 이벤트를 수신하면 Laravel 이벤트가 디스패치됩니다. 이벤트 디스커버리는 기본적으로 활성화되어 있습니다.
프로덕션 환경에서는 php artisan event:cache를 실행하세요.
기본 Listener 게시
php artisan vendor:publish --tag=line-listeners
app/Listeners/Line/에 MessageListener가 생성됩니다.
namespace App\Listeners\Line;
use LINE\Clients\MessagingApi\ApiException;
use LINE\Webhook\Model\MessageEvent;
use LINE\Webhook\Model\StickerMessageContent;
use LINE\Webhook\Model\TextMessageContent;
use Revolution\Line\Facades\Bot;
class MessageListener
{
protected string $token;
public function handle(MessageEvent $event): void
{
$message = $event->getMessage();
$this->token = $event->getReplyToken();
match ($message::class) {
TextMessageContent::class => $this->text($message),
StickerMessageContent::class => $this->sticker($message),
};
}
protected function text(TextMessageContent $message): void
{
Bot::reply($this->token)->text($message->getText());
}
protected function sticker(StickerMessageContent $message): void
{
Bot::reply($this->token)->sticker(
$message->getPackageId(),
$message->getStickerId()
);
}
}
Bot 파사드
Revolution\Line\Facades\Bot은 공식 SDK MessagingApiApi 클래스의 모든 메서드에 위임합니다.
use Revolution\Line\Facades\Bot;
Bot::replyMessage();
Bot::pushMessage();
reply 메서드
Bot::reply()를 사용하면 리플라이 토큰을 넘겨 메시지를 답신할 수 있습니다.
Bot::pushMessage()를 이용한 푸시 발송은 요금 플랜별로 발송 수 제한이 있습니다. 반면, 사용자의 메시지에 대한 답신(Bot::reply())은 발송 수 제한이 없으며 무료로 이용할 수 있습니다.
use Revolution\Line\Facades\Bot;
// 텍스트로 답신
Bot::reply($token)->text('text');
// 발신자 이름을 바꿔 여러 텍스트를 답신
Bot::reply($token)->withSender('alt-name')->text('text1', 'text2');
// 스티커로 답신
Bot::reply($token)->sticker(package: 1, sticker: 1);
커스터마이징
Bot 매크로
Bot은 Macroable을 구현하므로 임의의 메서드를 추가할 수 있습니다.
AppServiceProvider@boot에서 등록합니다.
use Revolution\Line\Facades\Bot;
public function boot(): void
{
Bot::macro('foo', function () {
return $this->bot()->...;
});
}
MessagingApiApi 인스턴스 교체
Bot::bot()은 MessagingApiApi 인스턴스를 반환합니다. Bot::botUsing()으로 인스턴스를 교체할 수 있습니다.
$bot = new MyBot();
Bot::botUsing($bot);
Callable도 받을 수 있습니다.
Bot::botUsing(function () {
return new MyBot();
});
WebhookHandler 교체
Laravel 이벤트 시스템을 사용하지 않고 직접 Webhook 처리를 작성하려면, WebhookHandler 인터페이스를 구현하여 교체합니다.
app/Actions/LineWebhook.php를 만듭니다.
<?php
namespace App\Actions;
use Illuminate\Http\Request;
use LINE\Webhook\Model\MessageEvent;
use Revolution\Line\Contracts\WebhookHandler;
use Revolution\Line\Facades\Bot;
class LineWebhook implements WebhookHandler
{
public function __invoke(Request $request): mixed
{
Bot::parseEvent($request)->each(function ($event) {
if ($event instanceof MessageEvent) {
//
}
});
return response('OK');
}
}
AppServiceProvider@register에서 등록합니다.
use App\Actions\LineWebhook;
use Revolution\Line\Contracts\WebhookHandler;
public function register(): void
{
$this->app->scoped(WebhookHandler::class, LineWebhook::class);
}
기본 라우트 미들웨어
기본으로 throttle 미들웨어가 활성화되어 있습니다. .env에서 변경할 수 있습니다.
# 비활성화
LINE_BOT_WEBHOOK_MIDDLEWARE=null
# throttle 설정 변경
LINE_BOT_WEBHOOK_MIDDLEWARE=throttle:120,1
Http::line()
이 패키지는 Http 클래스를 확장하고 있으므로 Bot 파사드를 사용하지 않고 직접 API 요청을 보낼 수도 있습니다.
use Illuminate\Support\Facades\Http;
$response = Http::line()->post('/v2/bot/channel/webhook/test', [
'endpoint' => '',
]);