mamadali/yii2-webhook

Yii2 发送创建和变更到 webhook

安装: 209

依赖: 0

建议者: 0

安全性: 0

星标: 1

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

v1.51 2022-05-29 10:11 UTC

This package is auto-updated.

Last update: 2024-09-29 06:14:46 UTC


README

帮助您将模型变更发送到 webhook URL

Latest Stable Version Total Downloads

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一

composer require --prefer-dist mamadali/yii2-webhook "*"

或者

"mamadali/yii2-webhook": "*"

将以下内容添加到您的 composer.json 文件的 require 部分。

然后运行迁移

php yii migrate/up --migrationPath=@vendor/mamadali/yii2-webhook/migrations

##基本用法首先添加到 config.php 或如果使用高级项目则添加到 common/config/main.php

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
        ],
        ...
    ];

要将模型变更发送到 webhook,您需要在您的模型中添加以下内容

public function behaviors()
{
    return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
		],
	];
}

当您的模型有任何更改时,将数据发送到 webhook,如下所示

{
    "model_name": "ExampleModel",
    "action": "insert", // or update or delete
    "model_id": 4, // id of model
    "data": { // all data of your model
        "id": 4,
        "title": "Example title",
        "status": 1,
        "created_at": 1633153382,
        "updated_at": 1645517769
    }
}

##高级用法

您可以为您的 webhook URL 添加身份验证,如下所示

用于基本认证

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'authToken' => base64_encode("$username:$password"), // change username and password
        ],
        ...
    ];

用于 Bearer 认证

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'authMethod' => 'Bearer',
            'authToken' => $token, // your token here
        ],
        ...
    ];

###您可以根据需要更改 URL 和身份验证来发送 webhook,如下所示

public function behaviors()
{
	return [
		[
			'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'url' => 'https://example.com/webhook/example-model',
		],
	];
}

您可以将数据仅发送到特定场景或排除场景的 webhook,如下所示

public function behaviors()
{
	return [
		[
			'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'scenarios' => ['insert', 'update'], // send data only on these scenarios
			'except' => ['delete'], // Send data except in these scenarios
		],
	];
}

您可以为 webhook 定制要发送的属性,如下所示

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'attributes' => [
				'title' => function (self $model) {
					return $model->getFullTitle();
				},
				'email',
				'created_at' => function (self $model) {
                    return date('Y-m-d H:i:s', $model->created_at);
                },
			],
		],
	];
}

您可以设置排除属性,如果设置,则除了此数组中的属性外,将发送所有属性

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'exceptAttributes' => [
				'status',
				'password'
			],
		],
	];
}

您可以使用 'when' 属性仅在函数返回 true 时发送数据,如下所示

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'when' => function(self $model) {
                            return $model->status == 1;
			},
		],
	];
}

####您可以使用队列发送 webhook,要使用队列,您需要首先从 Yii2 Queue 文档 配置队列

然后您可以在您的模型中使用队列,如下所示

public function behaviors()
{
	return [
		[
		    'class' => 'mamadali\webhook\WebhookBehavior',
			'modelName' => 'ExampleModel', // your model name, required. send model name in webhook data
			'sendToQueue' => true,
		],
	];
}

##高级用法队列您可以在您的配置中覆盖作业

    'modules' => [
        ...
        'webhook' => [
            'class' => 'mamadali\webhook\Module',
            'url' => 'https://example.com/webhook',
            'jobNamespace' => 'console\job',
        ],
        ...
    ];

并在您的命名空间中创建 WebhookJob,如下所示

<?php

namespace console\job;

use mamadali\webhook\Module;
use Yii;
use yii\base\BaseObject;
use yii\queue\RetryableJobInterface;

class WebhookJob extends BaseObject implements RetryableJobInterface
{
	/**
	 * @var integer the webhook id
	 */
	public $webhook_id;

	public function execute($queue)
	{
		/**
		 * @var Module
		 */
		$module = Yii::$app->getModule('webhook');
		$module->send($this->webhook_id);
	}


	public function getTtr()
	{
		return 60;
	}

	public function canRetry($attempt, $error)
	{
		return $attempt < 5;
	}
}