yamakadi / laravel-line-webhooks
在Laravel应用程序中处理Line webhooks
Requires
- php: ^7.0
- illuminate/support: ~5.5.0|~5.6.0
- yamakadi/linebot: dev-master
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0
- phpunit/phpunit: ^6.4|^7.0
This package is auto-updated.
Last update: 2024-09-29 04:42:57 UTC
README
Line可以通过webhooks通知您的应用程序事件。此包可以帮助您处理这些webhooks。默认情况下,它会验证所有传入请求的Line签名。所有有效的调用都将记录到数据库中。您可以在应用程序中轻松定义当特定事件发生时应分发的作业或事件。
此包不会处理在验证webhook请求并调用正确的作业或事件之后应该做什么。您仍需自己编写任何工作(例如,有关消息的工作)的代码。
在使用此包之前,我们强烈建议阅读Line上的整个消息API文档。
安装
您可以通过composer安装此包
composer require yamakadi/laravel-line-webhooks
服务提供程序将自动注册自身。
您必须使用以下命令发布配置文件
php artisan vendor:publish --provider="Yamakadi\LineWebhooks\LineWebhooksServiceProvider" --tag="config"
这是将发布到config/line-webhooks.php
的配置文件内容
return [ /* * You need to define your channel secret and access token in your environment variables */ 'channel_id' => env('LINEBOT_CHANNEL_ID'), 'channel_secret' => env('LINEBOT_CHANNEL_SECRET'), 'channel_access_token' => env('LINEBOT_CHANNEL_ACCESS_TOKEN'), /* * You can define the job that should be run when a certain webhook hits your application * here. The key is the name of the Line event in snake_case. * * You can find a list of Line webhook types here: * https://developers.line.me/en/docs/messaging-api/reference/#webhook-event-objects */ 'jobs' => [ // 'message' => \App\Jobs\LineWebhooks\HandleIncomingMessage::class, // 'beacon' => \App\Jobs\LineWebhooks\HandleBeaconSignal::class, ], /* * The classname of the model to be used. The class should equal or extend * Yamakadi\LineWebhooks\LineWebhookCall. */ 'model' => Yamakadi\LineWebhooks\LineWebhookCall::class, ];
接下来,您必须使用以下命令发布迁移
php artisan vendor:publish --provider="Yamakadi\LineWebhooks\LineWebhooksServiceProvider" --tag="migrations"
在迁移发布后,您可以通过运行迁移来创建line_webhook_calls
表
php artisan migrate
最后,注意路由:在Line仪表板中,您必须配置Line webhooks应该击中您的应用程序的URL。在您的应用程序的路由文件中,您必须将此路由传递给Route::lineWebhooks
Route::lineWebhooks('webhook-route-configured-at-the-line-dashboard');
幕后,这将注册一个由此包提供的控制器提供的POST
路由。因为Line没有获取csrf-token的方法,所以您必须将该路由添加到VerifyCsrfToken
中间件的except
数组中
protected $except = [ 'webhook-route-configured-at-the-line-dashboard', ];
用法
Line将为几种事件类型发送webhooks。您可以在Line文档中找到事件类型的完整列表。
Line将为击中您的应用程序webhook URL的所有请求签名。此包将自动验证签名是否有效。如果签名无效,则请求可能不是由Line发送的。
除非发生严重错误,否则此包将始终以200
响应webhook请求。发送200
将防止Line反复重新发送相同的事件。所有具有有效签名的webhook请求都将记录在line_webhook_calls
表中。该表有一个payload
列,其中保存了传入webhook的整个有效负载。
如果签名无效,则请求将不会记录在line_webhook_calls
表中,但会抛出Yamakadi\LineWebhooks\WebhookFailed
异常。如果在webhook请求期间发生错误,则抛出的异常将保存到exception
列中。在这种情况下,控制器将发送500
而不是200
。
此包以两种方式使您能够处理webhook请求:您可以选择排队作业或监听包将引发的事件。
使用作业处理webhook请求
当特定事件类型到来时,如果您想执行某些操作,可以定义一个执行该工作的作业。以下是一个此类作业的示例
<?php namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use Yamakadi\LineBot\Events\Event; use Yamakadi\LineWebhooks\LineWebhookCall; class HandleIncomingText implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** @var \Yamakadi\LineBot\Events\Event */ public $event; /** @var \Yamakadi\LineWebhooks\LineWebhookCall */ public $webhookCall; public function __construct(Event $event, LineWebhookCall $webhookCall) { $this->event = $event; $this->webhookCall = $webhookCall; } public function handle() { // do your work here // you can access the payload of the webhook call with `$this->webhookCall->payload` } }
我们强烈建议您将此作业设置为可排队,因为这将最小化webhook请求的响应时间。这允许您处理更多行webhook请求并避免超时。
创建您的作业后,必须在line-webhooks.php
配置文件中的jobs
数组中注册它。键应该是蛇形写法的line事件类型的名称。值应该是完全限定的类名。
// config/line-webhooks.php 'jobs' => [ 'message' => \App\Jobs\LineWebhooks\HandleIncomingMessage::class, ],
使用事件处理webhook请求
您可以选择监听此包将引发的事件,而不是将作业排队以在webhook请求到来时执行某些工作。每当有效请求击中您的应用程序时,该包将触发一个line-webhooks::<event-name>
事件。
事件的有效负载将是为传入请求创建的LineWebhookCall
实例以及来自Line SDK的事件对象。
让我们看看您如何监听此类事件。在EventServiceProvider
中,您可以注册监听器。
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'line-webhooks::message' => [ App\Jobs\LineWebhooks\HandleIncomingMessage::class, ], ];
以下是一个此类监听器的示例
<?php namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use Yamakadi\LineBot\Events\Event; use Yamakadi\LineWebhooks\LineWebhookCall; class ReplyWithQuote implements ShouldQueue { public function handle(Event $event, LineWebhookCall $webhookCall) { // do your work here // you can access the payload of the webhook call with `$webhookCall->payload` } }
我们强烈建议您将事件监听器设置为可排队,因为这将最小化webhook请求的响应时间。这允许您处理更多Line webhook请求并避免超时。
上述示例只是Laravel中处理事件的一种方式。要了解其他选项,请阅读Laravel处理事件的文档。
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
测试
composer test
贡献
有关详细信息,请参阅CONTRIBUTING。
安全
如果您发现任何与安全相关的问题,请通过电子邮件me@kakirigi.com联系我,而不是使用问题跟踪器。
鸣谢
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。