mnovik/laravel-zadarma

Laravel ^5.6 集成 Jlorente Zadarma 包。

v1.0.4 2024-08-16 12:28 UTC

This package is not auto-updated.

Last update: 2024-09-27 13:43:40 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。