binary-cats/laravel-zoom-webhooks

在Laravel应用程序中处理Zoom.us webhook

2.0.0 2024-01-28 03:22 UTC

This package is auto-updated.

Last update: 2024-09-17 16:22:00 UTC


README

https://github.com/binary-cats/laravel-zoom-webhooks/actions https://github.styleci.io/repos/ https://scrutinizer-ci.com/g/binary-cats/laravel-zoom-webhooks/

Zoom可以通过webhook通知您各种事件。此包可以帮助您处理这些webhook。它将默认验证传入请求的Zoom webhook签名。所有有效调用都将记录到数据库中。您可以轻松定义当特定事件发生时应该分发的作业或事件。

此包不会处理webhook请求验证后应该做什么。您仍然需要自己编写代码(例如,应该发生什么)。更多信息请继续阅读。

在使用此包之前,我们强烈建议您阅读Zoom上的webhook完整文档

此包是基于非常棒的spatie/laravel-stripe-webhooks改编的。

从^1.0升级

如果您是从之前的版本升级,请注意,spatie/laravel-webhook-client已升级到^3.0,这将在webhook表中添加一个额外的字段。有关详细信息,请阅读升级说明

安装

您可以通过composer安装此包

composer require binary-cats/laravel-zoom-webhooks

服务提供程序将自动注册自身。

您必须使用以下命令发布配置文件

php artisan vendor:publish --provider="BinaryCats\ZoomWebhooks\ZoomWebhooksServiceProvider" --tag="config"

以下是将在config/zoom-webhooks.php中发布的配置文件内容

return [

    /*
     * Zoom will sign each webhook using a verification token. You can find the secret used
     * in the  page of your Marketplace app: .
     */
    'signing_secret' => env('ZOOM_WEBHOOK_SECRET'),

    /*
     * You can define the job that should be run when a certain webhook hits your application
     * here. The key is the name of the Zoom event type with the `.` replaced by a `_`.
     *
     * You can find a list of Zoom webhook types here:
     * https://marketplace.zoom.us/docs/api-reference/webhook-reference#events.
     */
    'jobs' => [
        // 'meeting_started' => \BinaryCats\ZoomWebhooks\Jobs\HandleMeetingStarted::class,
    ],

    /*
     * The classname of the model to be used. The class should equal or extend
     * Spatie\WebhookClient\Models\WebhookCall
     */
    'model' => \Spatie\WebhookClient\Models\WebhookCall::class,

    /*
     * The classname of the model to be used. The class should equal or extend
     * BinaryCats\ZoomWebhooks\ProcessZoomWebhookJob
     */
    'process_webhook_job' => \BinaryCats\ZoomWebhooks\ProcessZoomWebhookJob::class,
];

在配置文件的signing_secret键中,您应添加有效的webhook密钥。您可以在HTTP webhook签名密钥中找到该密钥。

如果您已经安装了Spatie\WebhookClient,则可以跳过迁移

接下来,您必须使用以下命令发布迁移

php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="migrations"

迁移发布后,您可以运行迁移以创建webhook_calls

php artisan migrate

路由

最后,处理路由:在Markerplace仪表板中,您必须配置Zoom webhook应击中的URL。在您的应用程序的路由文件中,您必须将此路由传递给Route::zoomWebhooks()

个人喜欢通过调用域名分组webhook功能,因此我建议使用webhooks/zoom(如果您计划有更多的webhook,则特别推荐),但这取决于您的应用程序,由您决定。

# routes\web.php
Route::zoomWebhooks('webhooks/zoom');

在幕后,这将注册一个由此包提供的控制器提供的POST路由。因为Zoom没有获取csrf-token的方法,所以您必须将此路由添加到VerifyCsrfToken中间件的except数组中

protected $except = [
    'webhooks/zoom',
];

替代中间件配置

当您为多个服务定义了多个webhook,并且使用类似Stripe WebhooksMailgun Webhooks的类似包时,定义VerifyCsrfToken中间件可能更容易,如下所示:

protected $except = [
    'webhooks/*',
];

使用

Zoom将为几种事件类型发送webhook。您可以在Zoom文档中找到事件类型的完整列表

Zoom将对所有击中您应用程序webhook URL的请求进行签名。此包将自动验证签名是否有效。如果签名无效,则请求可能不是由Zoom发送的。

除非出现严重错误,否则此包将始终以200响应webhook请求。发送200将防止Zoom反复重发相同的事件。所有带有有效签名的webhook请求都将记录在webhook_calls表中。该表有一个payload列,其中保存了传入webhook的全部有效负载。

如果签名无效,则请求将不会记录在webhook_calls表中,但会抛出BinaryCats\ZoomWebhooks\Exceptions\WebhookFailed异常。如果在webhook请求过程中出现错误,则抛出的异常将保存在exception列中。在这种情况下,控制器将发送500而不是200

此包有两种方式让您处理webhook请求:您可以选择排队一个作业或监听包将触发的活动。

使用作业处理webhook请求

如果您想在特定事件类型到达时执行某些操作,您可以定义一个执行该工作的作业。以下是一个此类作业的示例:

<?php

namespace App\Jobs\ZoomWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class HandleMeetingStarted implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        // do your work here

        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

Spatie强烈建议您将此作业设置为可排队,以最小化webhook请求的响应时间。异步处理还允许您处理更多的Zoom webhook请求,并避免超时。更多信息请参阅队列文档

验证令牌位于authorization请求头中。

创建您的作业后,必须在zoom-webhooks.php配置文件中的jobs数组中注册它。应该是Zoom事件类型的名称,但用.替换为_应该是完全限定的类名。

// config/zoom-webhooks.php

'jobs' => [
    'meeting_started' => \App\Jobs\ZoomWebhooks\HandleMeetingStarted::class,
],

使用事件处理webhook请求

您可以选择监听此包将触发的活动,而不是在webhook请求到达时排队作业以执行某些操作。每次有效请求击中您的应用程序时,包都会触发一个zoom-webhooks::<event-name>事件。

事件的有效负载将是为传入请求创建的WebhookCall实例。

让我们看看如何监听此类事件。您可以在EventServiceProvider中注册您的事件监听器。

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'zoom-webhooks::meeting.started' => [
        App\Listeners\Zoom\MeetingStarted::class,
    ],
];

以下是一个此类监听器的示例:

<?php

namespace App\Listeners\Zoom;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class MeetingStarted implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }
}

Spatie强烈建议您将事件监听器设置为可排队,这样可以最小化webhook请求的响应时间,并允许您在避免超时的情况下消费更多的Zoom webhook请求。

上述示例只是Laravel中处理事件的一种方式。要了解其他选项,请阅读Laravel处理事件的文档

高级用法

重试处理webhook

所有传入的webhook请求都将写入数据库。这在处理webhook调用时出现问题时非常有价值。您可以在调查并修复失败原因后轻松重试处理webhook调用,如下所示:

use Spatie\WebhookClient\Models\WebhookCall;
use BinaryCats\ZoomWebhooks\ProcessZoomWebhookJob;

dispatch(new ProcessZoomWebhookJob(WebhookCall::find($id)));

执行自定义逻辑

您可以通过指定自己的作业类在zoom-webhooks配置文件中的process_webhook_job键中来在排队的作业调度之前和/或之后添加一些自定义逻辑。该类应扩展BinaryCats\ZoomWebhooks\ProcessZoomWebhookJob

以下是一个示例:

use BinaryCats\ZoomWebhooks\ProcessZoomWebhookJob;

class MyCustomZoomWebhookJob extends ProcessZoomWebhookJob
{
    public function handle()
    {
        // do some custom stuff beforehand

        parent::handle();

        // do some custom stuff afterwards
    }
}

处理多个签名密钥

有时你可能希望包能够处理多个端点和密钥。以下是配置该行为的步骤。

如果你正在使用 Route::zoomWebhooks 宏,你可以按如下方式附加 configKey

Route::zoomWebhooks('webhooks/zoom/{configKey}');

或者,如果你正在手动定义路由,你可以这样添加 configKey

Route::post('webhooks/zoom/{configKey}', 'BinaryCats\ZoomWebhooks\ZoomWebhooksController');

如果存在此路由参数,验证中间件将使用不同的配置密钥查找密钥,通过将给定参数值附加到默认配置密钥。例如,如果Zoom将信息发布到 webhooks/zoom/my-special-secret,你需要添加一个名为 signing_secret_my-special-secret 的新配置,如下所示

// secret for when Zoom posts to webhooks/zoom/account
'signing_secret_account' => 'whsec_abcdef',
// secret for when Zoom posts to webhooks/zoom/my-special-secret
'signing_secret_my-special-secret' => 'whsec_123456',

关于Zoom

Zoom Zoom是一款基于网络的视频会议工具,拥有本地桌面客户端和移动应用,允许用户在线会议,视频可选。

更新日志

请参阅更新日志获取最近更改的更多信息。

测试

composer test

贡献

请参阅贡献指南获取详细信息。

安全性

如果你发现任何安全相关的问题,请通过电子邮件cyrill.kalita@gmail.com联系我们,而不是使用问题跟踪器。

Postcardware

你可以自由使用此包,但如果它进入你的生产环境,我们非常感激你从家乡寄给我们一张明信片,说明你正在使用我们哪些包。

鸣谢

Spatie表示敬意,他们的工作给我们带来了巨大的灵感。

支持我们

Binary Cats是一家位于美国伊利诺伊州的网络机构。

许可证

MIT许可证(MIT)。请参阅许可证文件获取更多信息。