netflie / laravel-notification-whatsapp
Laravel 通知驱动程序,用于 WhatsApp
Requires
- php: >=7.4
- illuminate/notifications: ^8.0 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
- netflie/whatsapp-cloud-api: ^2.2.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-01 21:10:23 UTC
README
此包使您能够通过 Laravel 使用 WhatsApp Cloud API 发送通知。
此包使用 whatsapp-cloud-api 库,该库允许您从任何类型的项目和框架(使用 PHP 编写)发送通过 WhatsApp Cloud API 的消息。
内容
安装
您可以通过 composer 安装此包
composer require netflie/laravel-notification-whatsapp
设置 WhatsApp Cloud API
按照 "入门指南" 创建一个新的 Meta 应用程序,获取您的 WhatsApp 应用令牌
和 电话号码 ID
。将它们放入您的 .env
文件中。要加载它们,请将以下内容添加到您的 config/services.php
文件中
<?php return [ /* |-------------------------------------------------------------------------- | Third Party Services |-------------------------------------------------------------------------- | | This file is for storing the credentials for third party services such | as Mailgun, Postmark, AWS and more. This file provides the de facto | location for this type of information, allowing packages to have | a conventional file to locate the various service credentials. | */ // Other third-party config... 'whatsapp' => [ 'from-phone-number-id' => env('WHATSAPP_FROM_PHONE_NUMBER_ID'), 'token' => env('WHATSAPP_TOKEN'), ], ];
用法
WhatsApp API 只允许您在发送模板消息时开始对话。这意味着您只能从本包发送模板通知。
WhatsApp 要求您在使用模板之前配置它们。您可以通过遵循 Meta 的官方指南 "如何创建模板" 来学习如何配置模板。
WhatsApp 模板部分
模板分为 4 个部分:标题、正文、页脚和按钮。标题、正文和按钮接受变量。页脚不接受变量。您只能从本包发送变量用于标题和正文。
组件
您可以使用组件工厂创建多个组件,以向模板添加上下文(变量)。
<?php use NotificationChannels\WhatsApp\Component; Component::currency($amount, $code = 'EUR'); Component::dateTime($dateTime, $format = 'Y-m-d H:i:s'); Component::document($link); Component::image($link); Component::video($link); Component::text($text); Component::urlButton($array_of_urls); Component::quickReplyButton($array_of_payloads); Component::flowButton($flow_token, $array_of_data);
WhatsApp 模板部分支持的组件
- 标题:图片、视频、文档和文本(文本接受货币、日期和时间变量)
- 正文:货币、日期和时间
- 按钮:URL 和快速回复
从模板发送通知
要使用此包,您需要在 Laravel 应用程序中创建一个通知类,如下面的示例中的 MovieTicketPaid
。请确保查看 Laravel 的文档 了解此过程。
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use NotificationChannels\WhatsApp\Component; use NotificationChannels\WhatsApp\WhatsAppChannel; use NotificationChannels\WhatsApp\WhatsAppTemplate; class MovieTicketPaid extends Notification { /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [WhatsAppChannel::class]; } public function toWhatsapp($notifiable) { return WhatsAppTemplate::create() ->name('sample_movie_ticket_confirmation') // Name of your configured template ->header(Component::image('https://lumiere-a.akamaihd.net/v1/images/image_c671e2ee.jpeg')) ->body(Component::text('Star Wars')) ->body(Component::dateTime(new \DateTimeImmutable)) ->body(Component::text('Star Wars')) ->body(Component::text('5')) ->buttons(Component::quickReplyButton(['Thanks for your reply!'])) ->buttons(Component::urlButton(['reply/01234'])) // List of url suffixes ->to('34676010101'); } }
发送文本消息
只有在发送模板并用户做出响应后,您才能发送文本消息。
<?php namespace App\Notifications; use Illuminate\Notifications\Notification; use NotificationChannels\WhatsApp\Component; use NotificationChannels\WhatsApp\WhatsAppChannel; use NotificationChannels\WhatsApp\WhatsAppTextMessage; class MovieTicketPaid extends Notification { /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [WhatsAppChannel::class]; } public function toWhatsapp($notifiable) { return WhatsAppTextMessage::create() ->message('Hello, this is a test message') ->to('34676010101'); } }
变更日志
请参阅 CHANGELOG 了解最近更改的信息。
测试
$ composer test
安全性
如果您发现任何与安全相关的问题,请通过电子邮件 hola@netflie.es 而不是使用问题跟踪器。
贡献
请参阅 CONTRIBUTING 了解详细信息。
致谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。