rauny-henrique/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-09-26 03:17:10 UTC


README

Nexmo is now known as Vonage

虽然 Nexmo 作为默认的 SMS 提供商在 Laravel 中可用(见 Laravel 文档),但下一代 Nexmo API 将提供更多通信渠道。此包增加了通过 Nexmo 向 WhatsApp、Facebook Messenger 和 Viber 发送通知的功能。

注意,此包使用的 Messages 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");
}