jlorente/laravel-zadarma

Laravel ^5.6 集成 Jlorente Zadarma 包。

1.0.2 2020-09-22 14:55 UTC

This package is auto-updated.

Last update: 2024-09-22 23:49:52 UTC


README

Laravel 对 Zadarma SDK 的集成,包括两个通知渠道。

安装

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

安装 composer 后,可以使用以下命令安装扩展

$ php composer.phar require jlorente/laravel-zadarma

或者添加

...
    "require": {
        "jlorente/laravel-zadarma": "*"
    }

到您的 composer.json 文件的 require 部分。

配置

  1. 在您的 config/app.php 服务提供者列表中注册 ServiceProvider。

config/app.php

return [
    //other stuff
    'providers' => [
        //other stuff
        \Jlorente\Laravel\Zadarma\ZadarmaServiceProvider::class,
    ];
];
  1. 在 $aliases 部分添加以下外观。

config/app.php

return [
    //other stuff
    'aliases' => [
        //other stuff
        'Zadarma' => \Jlorente\Laravel\Zadarma\Facades\Zadarma::class,
    ];
];
  1. 发布包配置文件。
$ php artisan vendor:publish --provider='Jlorente\Laravel\Zadarma\ZadarmaServiceProvider'
  1. 在 config/zadarma.php 文件中设置 api_key 和 api_secret,或使用预定义的 env 变量。

config/zadarma.php

return [
    'api_key' => 'YOUR_API_KEY',
    'api_secret' => 'YOUR_API_SECRET',
    //other configuration
];

或 .env

//other configurations
ZADARMA_API_KEY=<YOUR_API_KEY>
ZADARMA_API_SECRET=<YOUR_API_SECRET>

使用方法

您可以使用外观别名 Zadarma 执行 API 调用。认证参数将自动注入。

Zadarma::api()->getBalance();

通知渠道

此包包含两个通知渠道,允许您将 Zadarma 发送短信服务和电话呼叫请求回调与 Laravel 通知集成。

有关 Laravel 通知的更多信息,请参阅 此页面

ZadarmaSmsChannel

如果您想通过 Zadarma 发送短信,应在通知类中定义 toZadarmaSms 方法。此方法将接收一个 $notifiable 实体,并应返回一个字符串,该字符串将在短信中发送

/**
 * Get the SMS message.
 *
 * @param  mixed  $notifiable
 * @return string
 */
public function toZadarmaSms($notifiable)
{
    return 'Hello, this is an SMS sent through Zadarma API';
}

完成后,必须将通知渠道添加到通知的 via() 方法的数组中

/**
 * Get the notification channels.
 *
 * @param  mixed  $notifiable
 * @return array|string
 */
public function via($notifiable)
{
    return [ZadarmaSmsChannel::class];
}

ZadarmaPhoneCallChannel

如果您想通过 Zadarma API 通过请求回调建立电话呼叫,应在通知类中定义 toZadarmaPhoneCall 方法。此方法将接收一个 $notifiable 实体,并应返回一个电话/SIP 号码、PBX 分机号码或 PBX 场景,回调将进行到该场景

/**
 * Gets a phone/SIP number, a PBX extension number or a PBX scenario to which the 
 * phone callback will be made.
 *
 * @param mixed $notifiable
 * @return string
 */
public function toZadarmaPhoneCall($notifiable)
{
    return 100;
}

完成后,必须将通知渠道添加到通知的 via() 方法的数组中

/**
 * Get the notification channels.
 *
 * @param mixed $notifiable
 * @return array|string
 */
public function via($notifiable)
{
    return [ZadarmaPhoneCallChannel::class];
}

路由通知

通过 Zadarma 通道发送通知时,通知系统将自动在 notifiable 实体上查找 phone_number 属性。如果您想自定义号码,应在实体上定义 routeNotificationForZadarma 方法

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * Route notifications for the Zadarma channels.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForZadarma($notification)
    {
        return $this->phone;
    }
}

许可证

版权所有 © 2019 José Lorente Martín jose.lorente.martin@gmail.com

在 BSD 3-Clause 许可证下授权。有关详细信息,请参阅 LICENSE.txt。