ohdearapp/laravel-ohdear-webhooks
Composer 安装命令:
composer require ohdearapp/laravel-ohdear-webhooks
包简介
Handle Oh Dear webhook calls in a Laravel app
README 文档
README
Oh Dear can notify your application of events using webhooks. This package can help you handle those webhooks. Out of the box it will verify the Oh Dear signature of all incoming requests. You can easily define jobs or events that should be dispatched when specific events hit your app.
This package will not handle what should be done after the webhook request has been validated and the right job or event is called.
Installation
$ composer require ohdearapp/laravel-ohdear-webhooks
The service provider will automatically register itself.
You must publish the config file with:
$ php artisan vendor:publish --provider="OhDear\LaravelWebhooks\OhDearWebhooksServiceProvider" --tag="config"
This is the contents of the config file that will be published at config/ohdear-webhooks.php:
return [ /* * Oh dear will sign webhooks using a secret. You can find the secret used at the webhook * configuration settings: <?php echo config('app.url'); ?>/team-settings/notifications#webhooks */ 'signing_secret' => env('OH_DEAR_SIGNING_SECRET'), /* * Here you can define the job that should be run when a certain webhook hits your . * application. * * You can find a list of Oh dear webhook types here: * https://ohdear.app/docs/integrations/webhooks#webhook-events */ 'jobs' => [ // 'uptimeCheckFailed' => \App\Jobs\LaravelWebhooks\HandleFailedUptimeCheck::class, // 'uptimeCheckRecovered' => \App\Jobs\LaravelWebhooks\HandleRecoveredUptimeCheck::class, // ... ], ];
In the signing_secret key of the config file you specify your signing secret. You can find the correct at the team webhooks settings on the notification settings screen.
Finally, take care of the routing: At the Oh Dear notification settings you must configure at what url Oh Dear webhooks should hit your app. In the routes file of your app you must pass that route to Route::ohDearWebhooks:
Route::ohDearWebhooks('webhook-route-configured-at-the-ohdear-dashboard');
Behind the scenes this will register a POST route to a controller provided by this package. Because Oh Dear has no way of getting a csrf-token, you must add that route to the except array of the VerifyCsrfToken middleware:
protected $except = [ 'webhook-route-configured-at-the-ohdear-dashboard', ];
Usage
Oh Dear will send out webhooks for several event types. You can find the full list of events types in the Oh Dear documentation.
Oh Dear will sign all requests hitting the webhook url of your app. This package will automatically verify if the signature is valid. If it is not, the request was probably not sent by Oh Dear.
Unless something wrong, this package will respond with a 200 to webhook requests. Sending a 200 will prevent Oh Dear from resending the same event again.
If the signature is not valid a OhDear\OhDearWebhooks\WebhookFailed exception will be thrown.
There are two ways this package enables you to handle webhook requests: you can opt to queue a job or listen to the events the package will fire.
Handling webhook requests using jobs
If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job:
namespace App\Jobs; use Illuminate\Bus\Queueable; use Illuminate\Queue\SerializesModels; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; use OhDear\LaravelWebhooks\OhDearWebhookCall; class HandleFailedUptimeCheck implements ShouldQueue { use InteractsWithQueue, Queueable, SerializesModels; /** @var \OhDear\LaravelWebhooks\OhDearWebhookCall */ public $webhookCall; public function __construct(OhDearWebhookCall $webhookCall) { $this->webhookCall = $webhookCall; } public function handle() { // do your work here // you can access the payload of the webhook call with $this->webhookCall->payload } }
We highly recommend that you make this job queueable, because this will minimize the response time of the webhook requests. This allows you to handle more oh dear webhook requests and avoid timeouts.
After having created your job you must register it at the jobs array in the ohdear-webhooks.php config file. The key should be the name of the oh dear event type. The value should be the fully qualified classname.
// config/ohdear-webhooks.php 'jobs' => [ 'uptimeCheckFailed' => \App\Jobs\ohdearWebhooks\HandleFailedUptimeCheck::class, 'uptimeCheckRecovered' => \App\Jobs\ohdearWebhooks\HandleRecoveredUptimeCheck::class, ],
Handling webhook requests using events
Instead of queueing jobs to perform some work when a webhook request comes in, you can opt to listen to the events this package will fire. Whenever a valid request hits your app, the package will fire a ohdear-webhooks::<name-of-the-event> event.
The payload of the events will be the instance of OhDearWebhookCall that was created for the incoming request.
Let's take a look at how you can listen for such an event. In the EventServiceProvider you can register listeners.
/** * The event listener mappings for the application. * * @var array */ protected $listen = [ 'ohdear-webhooks::uptimeCheckFailed' => [ App\Listeners\MailOperators::class, ], ];
Here's an example of such a listener:
namespace App\Listeners; use Illuminate\Contracts\Queue\ShouldQueue; use OhDear\LaravelWebhooks\OhDearWebhookCall; class MailOperators implements ShouldQueue { public function handle(OhDearWebhookCall $webhookCall) { // do your work here // you can access the payload of the webhook call with `$webhookCall->payload` } }
We highly recommend that you make the event listener queueable, as this will minimize the response time of the webhook requests. This allows you to handle more Oh Dear webhook requests and avoid timeouts.
The above example is only one way to handle events in Laravel. To learn the other options, read the Laravel documentation on handling events.
Using the OhDearWebhookCall
Like mentioned above your events or jobs will receive an instance of OhDear\LaravelWebhooks\OhDearWebhookCall.
You can access the raw payload by calling:
$ohDearWebhookCall->payload; // returns an array;
Or you can opt to get more specific information:
$ohDearWebhookCall->type(); // returns the type of the webhook (eg: 'uptimeCheckFailed'); $ohDearWebhookCall->monitor(); // returns an array with all the attribute of the monitor; $ohDearWebhookCall->run(); // returns an array with all the attribute of the run; $ohDearWebhookCall->dateTime(); // returns an string with a dateTime (Ymdhis) when Oh Dear generated this webhook call;
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email support@ohdear.app instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
ohdearapp/laravel-ohdear-webhooks 适用场景与选型建议
ohdearapp/laravel-ohdear-webhooks 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.42k 次下载、GitHub Stars 达 22, 最近一次更新时间为 2017 年 11 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「monitoring」 「uptime」 「ohdear」 「laravel-ohdear-webhooks」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 ohdearapp/laravel-ohdear-webhooks 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 ohdearapp/laravel-ohdear-webhooks 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 ohdearapp/laravel-ohdear-webhooks 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Create application health check results for Oh Dear
A Symfony bundle for chameleon-system/sanitycheck
SoftWax Health Check Bundle for Symfony framework
1Pilot client for Symfony
Production-ready Symfony bundle for comprehensive application health checks with database, Redis, S3, and HTTP endpoint monitoring. Security-first design with auto-tagging support.
Statsd (Object Oriented) client library for PHP
统计信息
- 总下载量: 26.42k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 22
- 点击次数: 29
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-02