emotality / panacea-laravel

用于通过 PanaceaMobile 发送交易短信的 Laravel 扩展包。

2.1.0 2024-04-04 06:41 UTC

This package is auto-updated.

Last update: 2024-09-04 07:33:41 UTC


README

License Latest Version Total Downloads

用于通过 PanaceaMobile 发送交易短信的 Laravel 扩展包。

要求

  • PHP 7.2+
  • Laravel 7.0+

安装

  1. composer require emotality/panacea-laravel
  2. php artisan vendor:publish --provider="Emotality\Panacea\PanaceaMobileServiceProvider"
  3. 将以下行添加到您的 .env 文件中
PANACEA_USERNAME="<panacea_username>"
PANACEA_PASSWORD="<panacea_password_or_api_key>"
PANACEA_FROM="<from_name>" // Optional

使用方法

向单个收件人发送短信

\PanaceaMobile::sms('+27820000001', "1st Line\n2nd Line\n3rd Line");
// or
\PanaceaMobile::sms('+27820000001', "1st Line\n2nd Line\n3rd Line", 'From Name');

响应将是一个 bool,成功时为 true,失败时为 false

向多个收件人发送短信

\PanaceaMobile::smsMany(['+27820000001', '+27820000002'], "1st Line\n2nd Line\n3rd Line");
// or
\PanaceaMobile::smsMany(['+27820000001', '+27820000002'], "1st Line\n2nd Line\n3rd Line", 'From Name');

响应将是一个数组,键是收件人的电话号码,值是布尔值

array:2 [▼
  "+27820000001" => true
  "+27820000002" => false
]

通过通知发送短信

namespace App\Notifications;

use Emotality\Panacea\PanaceaMobileSms;
use Emotality\Panacea\PanaceaMobileSmsChannel;
use Illuminate\Notifications\Notification;

class ExampleNotification extends Notification
{
    // Notification channels
    public function via($notifiable)
    {
        return [PanaceaMobileSmsChannel::class];
    }
    
    // Send SMS
    public function toSms($notifiable) // Can also use toPanacea($notifiable)
    {
        // Send SMS without "to", this value will automatically be fetched from
        // the "routeNotificationForPanacea" method in your notifiable entity.
        // See the "Routing SMS Notifications" section below.
        return (new PanaceaMobileSms())->message("1st Line\n2nd Line\n3rd Line");
        
        // or send SMS to a single recipient, specifying the "to" value
        return (new PanaceaMobileSms())
            ->to($notifiable->mobile) // Assuming $user->mobile is their mobile number
            ->from('From Name') // Optional. Will override config's "from" value.
            ->message("1st Line\n2nd Line\n3rd Line");
            
        // or send SMS to multiple recipients
        return (new PanaceaMobileSms())
            ->toMany(['+27820000001', '+27820000002'])
            ->from('From Name') // Optional. Will override config's "from" value.
            ->message("1st Line\n2nd Line\n3rd Line");
    }
}

短信通知路由

为了将 Panacea 通知路由到正确的电话号码,在您的可通知实体上定义一个 routeNotificationForPanacea 方法

namespace App\Models;
 
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
 
class User extends Authenticatable
{
    use Notifiable;
 
    /**
     * Route notifications for the Panacea channel.
     *
     * @param  \Illuminate\Notifications\Notification  $notification
     * @return string
     */
    public function routeNotificationForPanacea($notification)
    {
        return $this->mobile;
    }
}

许可协议

panacea-laravel 在 MIT 许可协议下发布。有关详细信息,请参阅 LICENSE