homedoctor-es/laravel-instasent

Laravel 5.8 对 Instasent SDK 的集成。

1.0.1 2022-02-22 17:01 UTC

This package is auto-updated.

Last update: 2024-09-22 22:47:55 UTC


README

Instasent SDK 的 Laravel 集成,包括一个通知通道。

安装

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

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

$ php composer.phar require homedoctor-es/laravel-instasent

或添加

...
    "require": {
        "homedoctor-es/laravel-instasent": "*"
    }

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

配置

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

config/app.php

return [
    //other stuff
    'providers' => [
        //other stuff
        \HomedoctorEs\Laravel\Instasent\InstasentServiceProvider::class,
    ];
];
  1. 如果您愿意,可以将以下外观添加到 $aliases 部分。

config/app.php

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

config/instasent.php

return [
    'api_token' => '', // your account api token
    'default_from' => 'Laravel', // optional name of the sender
    'dry_run' => false, // only for the notification channel, if true, no sms's will be sent
    'throw_exception_on_error' => true // This will throw up the Instasent sdk exception if an exception is thrown by the dispatchService on the InstasentSmsChannel
];

或 .env

//other configurations
INSTASENT_API_TOKEN=<YOUR_API_TOKEN>

使用

您可以使用 facade 别名 Instasent 来执行 instasent sdk 的服务。认证参数将被自动注入。

Instasent::clientSms()->sendSms(
    $sender
    , $phone
    , $text
);

您可以在 此页面 查看instasent sdk 服务的完整列表。

通知通道

此包包含一个通知通道,允许您将 Instasent 服务与 Laravel 通知集成。

格式化通知

如果通知支持通过 Instasent 发送短信,您应该在通知类上定义 toInstasent 方法。此方法将接收一个 $notifiable 实体,并应返回一个 HomedoctorEs\Laravel\Instasent\Notifications\Messages\InstasentMessage 实例或包含要发送的消息的字符串

/**
 * Get the Instasent / SMS representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \HomedoctorEs\Laravel\Instasent\Notifications\Messages\InstasentMessage|string
 */
public function toInstasent($notifiable)
{
    return (new InstasentMessage)
                ->content('Your SMS message content');
}

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

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

自定义发送者名称

如果您想用与 config/services.php 文件中指定的名称不同的发送者名称发送一些通知,您可以在 InstasentMessage 实例上使用 from 方法

/**
 * Get the Instasent / SMS representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \HomedoctorEs\Laravel\Instasent\Notifications\Messages\InstasentMessage|string
 */
public function toInstasent($notifiable)
{
    return (new InstasentMessage)
                ->content('Your SMS message content')
                ->from('Popilio');
}

路由通知

通过 instasent 通道发送通知时,通知系统将自动在 notifiable 实体上查找 phone_number 属性。如果您想自定义通知发送到的电话号码,请在实体上定义 routeNotificationForInstasent 方法

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

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

您可以在 此页面 找到有关 Laravel 通知的更多信息。

许可证

版权所有 © 2022 Homedoctor Smart Medicine S.L. pepe@homedoctor.es.

根据 BSD 3-Clause 许可证许可。有关详细信息,请参阅 LICENSE.txt。