roomies/vonage-voice-channel

通过电话发送 Laravel 通知

6.1.0 2024-03-07 04:56 UTC

This package is auto-updated.

Last update: 2024-09-15 10:29:22 UTC


README

Latest Version on Packagist GitHub Workflow Status Total Downloads

此包为 Laravel 框架提供了一种通知通道,该通道与 Vonage 的语音 API 一起工作,允许语音电话。它还提供了一个流畅的接口来构建您的消息内容。

安装

您可以通过 Composer 安装此包

composer require roomies/vonage-voice-channel

底层我们使用 laravel/vonage-notification-channel 来配置 Vonage,因此请确保您已经使用 Vonage 环境变量正确配置了它。但是,为了进行语音电话,您需要在您的环境中提供额外的凭证。请注意,私钥可以是字符串或密钥文件的路径。

VONAGE_APPLICATION_ID=
VONAGE_PRIVATE_KEY=

然后,将您的电话号码和语音添加到 config/services.php 中的 vonage 键下。您可以查看 Vonage 文档中的可用语音

'vonage' => [
    'call_from' => env('VONAGE_CALL_FROM'),
    'call_language' => env('VONAGE_CALL_LANGUAGE', 'en-US'),
    'call_style' => env('VONAGE_CALL_STYLE', 0),
],

用法

简单地将通知通过 VoiceChannel 路由,并从 toVoice 方法返回一个格式化的消息。您可以使用包含您自己的 语音合成标记语言 (SSML) 的字符串,或者使用包含的包装器 API 来构建您的消息。

use Roomies\VonageVoiceChannel\Markup\Message;
use Roomies\VonageVoiceChannel\Markup\SayAs;
use Roomies\VonageVoiceChannel\Markup\Sentence;
use Roomies\VonageVoiceChannel\VonageVoiceChannel;

/**
 * Get the notification's delivery channels.
 *
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return [VonageVoiceChannel::class];
}

/**
 * Get the voice representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return \Roomies\VonageVoiceChannel\Markup\Message
 */
public function toVoice($notifiable)
{
    return new Message([
        new Sentence('Hi, thanks for joining Roomies.'),
        new Sentence([
            'Your verification code is',
            (new SayAs('ABC123'))->interpretAs('spell-out')
        ]),
    ]);
}

标记

有几种不同的标记类型可供使用,以获取您想要的消息。以下是一些额外的示例,否则请浏览 src/Markup 以查看所有可用选项。

new Paragraph([
    new Sentence('This is the first sentence of a paragraph.'),
]);

new Sentence([
    'Hey!',
    (new Pause)->time('1s'),
    (new Prosody('Wake up!'))->volume('loud'),
    (new Substitution(
        (new SayAs('US'))->interpretAs('spell-out'),
    ))->alias('United States'),
])

自定义

或者,您可以自由返回自己的 SSML 标记字符串。如果您需要更定制的内容或具有更复杂的要求,这将给您提供完全的控制权。

/**
 * Get the voice representation of the notification.
 *
 * @param  mixed  $notifiable
 * @return string
 */
public function toVoice($notifiable)
{
    return '<speak>
        <s>Hi, thanks for joining Roomies</s>
        <s>Your verification code is <say-as interpret-as="spell-out">ABC123</say-as></s>
    <speak>';
}

许可

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