nexmo / laravel-notification

通过 Nexmo 发送 SMS、WhatsApp、Viber 和 Facebook Messenger 消息

0.2.1 2018-12-12 15:51 UTC

This package is auto-updated.

Last update: 2024-08-29 04:59:47 UTC


README

Nexmo is now known as Vonage

虽然Nexmo作为默认的SMS提供商已在Laravel中使用 https://laravel.net.cn/docs/5.8/notifications#sms-notifications,但下一代Nexmo API将提供更多通信渠道。此包增加了通过Nexmo发送通知到WhatsApp、Facebook Messenger和Viber的功能。

注意:此包使用的 消息API 尚未发布稳定版本,因此API和库都可能发生变化。

使用方法

要使用此包,请运行 composer require nexmo/laravel-notification。完成安装后,您可以在通知上实现以下方法:

  • toNexmoWhatsApp
  • toNexmoFacebook
  • toNexmoViberServiceMessage
  • toNexmoSms

请参阅 examples/Notification/MerryChristmas.php 以获取完整示例。

要发送通知,请指定您想使用的通道

// To a user
$user->notify(new \App\Notifications\MerryChristmas());

// To any person
Notification::route(
    'nexmo-whatsapp',
    'YOUR_NUMBER'
)->notify(new \App\Notifications\MerryChristmas());

可用的通道有:

  • nexmo-sms
  • nexmo-whatsapp
  • nexmo-facebook
  • nexmo-viber_service_msg

每个通知都会接收到一个 $notifiable(通常是用户),它可以决定如何最好地路由信息。在这种情况下,它会检查用户的 via_whatsapp 属性,如果为真,则通过WhatsApp发送。否则,将回退到电子邮件。

public function via($notifiable)
{
    return $notifiable->via_whatsapp ? ['nexmo-whatsapp'] : ['mail'];
}

消息类型

Nexmo支持多种消息类型,具体取决于您发送到的通道。如果您想将消息发送到所有通道,则 Text 类型是最安全的。

public function toNexmoWhatsApp($notifiable)
{
    return (new \Nexmo\Notifications\Message\Text)
        ->content('This is a message being sent to WhatsApp');
}

注意事项

由于垃圾邮件控制规则,在发送自由文本消息之前,您可能需要发送模板化消息。以下是一个用于双因素认证目的的预批准模板的示例:

public function toNexmoWhatsApp($notifiable)
{
    return (new \Nexmo\Notifications\Message\Template)
        ->name("whatsapp:hsm:technology:nexmo:verify")
        ->parameters([
            ["default" => "Your Brand"],
            ["default" => "64873"],
            ["default" => "10"],
        ]);
}

如果收件人回复您的消息,您可以通过发送 Text 类型消息而不会出现任何问题。

配置

身份验证

此通知包是在 nexmo/laravel 的基础上构建的,并使用其中的Nexmo客户端。

要使其正常工作,您需要在 .env 文件中设置您的应用程序ID和私钥路径。

NEXMO_APPLICATION_ID=my_application_id
NEXMO_PRIVATE_KEY=./private.key

设置 from 地址

您可以通过 .env 文件设置 from 地址。此包将查找特定提供者条目,然后再回退到 NEXMO_FROM

NEXMO_FROM_SMS=""
NEXMO_FROM_WHATSAPP=""
NEXMO_FROM_MESSENGER=""
NEXMO_FROM_VIBER_SERVICE_MSG=""
NEXMO_FROM="" # This is the default if any of the above aren't set

或者,您可以通过在消息上调用 ->from() 方法为单个通知设置 from 地址。

public function toNexmoViberServiceMessage($notifiable)
{
    return (new Text)->content('Merry Christmas Viber!')->from("YOUR_ID");
}