coreproc/laravel-notification-channel-telerivet

此包使用 Laravel 5.5+ 简化通过 Telerivet 发送通知

2.1.1 2020-09-24 17:04 UTC

This package is auto-updated.

Last update: 2024-08-25 01:53:41 UTC


README

Latest Version on Packagist Software License StyleCI Quality Score Total Downloads

此包使用 Laravel 5.5+ 和 6.0 简化通过 Telerivet 发送通知

内容

从 v1.x 升级到 v2.x

在 v2.x 中,我们将 Telerivet 的配置设置从 config/broadcasting.php 移动到 config/telerivet.php

要迁移到 v2.x,只需运行以下命令以获取配置文件

php artisan vendor:publish --provider="CoreProc\NotificationChannels\Telerivet\TelerivetServiceProvider"

安装

使用 Composer 安装此包

composer require coreproc/laravel-notification-channel-telerivet

在您的 config/app.php 中注册 ServiceProvider(如果您使用的是 Laravel 5.5,请跳过此步骤)

CoreProc\NotificationChannels\Telerivet\TelerivetServiceProvider::class,

设置 Telerivet 服务

您需要在此处注册 API 密钥和用于外发短信的号码:https://telerivet.com

一旦注册并设置好项目和号码,通过运行以下命令获取配置文件

php artisan vendor:publish --provider="CoreProc\NotificationChannels\Telerivet\TelerivetServiceProvider"

将 API 密钥和项目 ID 添加到您的 config/telerivet.php 配置中。您可以使用以下变量在 .env 文件中设置凭据:TELERIVET_API_KEYTELERIVET_PROJECT_ID

可选地,您还可以通过在您的 TelerivetMessage 对象中调用 setApiKey()setProjectId() 方法来覆盖这些配置。

使用

现在您可以通过创建一个 TelerivetMessage 来通过 Telerivet 发送短信

use CoreProc\NotificationChannels\Telerivet\TelerivetChannel;
use CoreProc\NotificationChannels\Telerivet\TelerivetMessage;
use Illuminate\Notifications\Notification;

class AccountActivated extends Notification
{
    public function via($notifiable)
    {
        return [TelerivetChannel::class];
    }

    public function toTelerivet($notifiable)
    {
        return (new TelerivetMessage())
            ->setContent('Hello this is a test message');
    }
}

您必须在您的可通知模型中设置一个 routeNotificationForTelerivet() 方法。例如

class User extends Authenticatable
{
    use Notifiable;

    ....

    /**
     * Specifies the user's mobile number for use in Telerivet
     *
     * @return string
     */
    public function routeNotificationForTelerivet()
    {
        return $this->mobile_number;
    }
}

一旦设置好,您就可以简单地通过以下方式向用户发送短信通知

$user->notify(new AccountActivated);

事件

在发送 Telerivet SMS 消息时,您可以监听这些事件

在发送短信之前

TelerivetSmsSending::class

当短信发送时(这意味着对 Telerivet 的 API 调用成功)

TelerivetSmsSent::class

当短信发送失败时(这意味着对 Telerivet 的 API 调用失败)

TelerivetSmsFailed::class

可用的消息方法

可以应用于 Telerivet 消息的所有参数都可以通过 TelerivetMessage 对象应用。Telerivet 的文档可以在这里找到 这里

setMessageType(?string $messageType)

[可选] 要发送的消息类型。如果文本,将使用所选路由的默认文本消息类型。

可能值:sms、mms、ussd、call、text

默认值:text

setContent(?string $content)

[发送 SMS 消息时必填] 要发送的消息内容(如果消息类型为 call,文本将在语音通话中播放)

setToNumber(?string $toNumber)

[如果未设置 contact_id 时必填] 要发送消息的电话号码。如果您已定义了可通知对象中的 routeNotificationForTelerivet() 方法,则会自动设置。

setContactId(?string $contactId)

[如果未设置 to_number 时必填] 要发送消息的联系人 ID。如果您已定义了可通知对象中的 routeNotificationForTelerivetContactId() 方法,则会自动设置。

setRouteId(?string $routeId)

[可选] 发送消息的电话或路由的 ID

默认值:您的项目默认发送者路由 ID

setStatusUrl(?string $statusUrl)

[可选] 当消息状态变化时通知的Webhook回调URL

setStatusSecret(?string $statusSecret)

[可选] 传递给status_url的POST参数'secret'

setIsTemplate(?bool $isTemplate)

[可选] 设置为true以在消息内容中评估如[[contact.name]]之类的变量。

(查看可用变量 这里)

默认:false

setTrackClicks(?bool $trackClicks)

[可选] 如果为true,消息内容中的URL将自动替换为唯一的短URL。

默认:false

setMediaUrls(?array $mediaUrls)

[可选] 要附加到文本消息的媒体文件的URL。如果message_type是sms,则每个媒体的短链接

URL将附加到内容末尾(由新行分隔)。

setLabelIds(?array $labelIds)

[可选] 标签的字符串ID数组

要添加到此消息的标签ID列表

setVars(?object $vars)

[可选] 与消息一起存储的自定义变量

setPriority(?int $priority)

[可选] 消息的优先级。Telerivet将尝试首先发送具有更高优先级编号的消息(例如,您可以优先考虑自动回复而不是向大型组发送的批量消息)。

可能的值:1, 2

默认:1

setSimulated(?bool $simulated)

[可选] 设置为true以测试Telerivet API而不实际上从路由发送消息

默认:false

setServiceId(?string $serviceId)

[可选] 服务的字符串ID

定义语音调用流程(当message_type是call时)的服务

setAudioUrl(?string $audioUrl)

[可选] 当联系人接听电话时播放的MP3文件的URL(当message_type是call时)。

如果提供了audio_url,则不会使用文本到语音的语音来说明内容,尽管您可以选择使用内容来指示音频的脚本。

为了获得最佳效果,请使用只包含语音的MP3文件。不建议使用音乐,因为通过电话线路播放时音频质量会很低。

setTtsLang(?string $ttsLang)

[可选] 文本到语音的语音语言(当message_type是call时)

可能的值:en-US, en-GB, en-GB-WLS, en-AU, en-IN, da-DK, nl-NL, fr-FR, fr-CA, de-DE, is-IS, it-IT, pl-PL, pt-BR, pt-PT, ru-RU, es-ES, es-US, sv-SE

默认:en-US

setTtsVoice(?string $ttsVoice)

[可选] 文本到语音的语音名称(当message_type=call时)

可能的值:female, male

默认:female

变更日志

请参阅变更日志了解最近更改了什么。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件发送到chris.bautista@coreproc.ph,而不是使用问题跟踪器。

贡献

请参阅贡献指南以获取详细信息。

致谢

许可

MIT许可证(MIT)。有关更多信息,请参阅许可文件