danidoble/laravel-notification-whatsapp

Laravel 的 WhatsApp 通知通道

v2.0.1 2024-06-22 16:56 UTC

This package is auto-updated.

Last update: 2024-09-22 17:27:05 UTC


README

Latest Version on Packagist Software License Build Status StyleCI Quality Score Code Coverage Total Downloads

此包通过 Laravel 使用 WhatsApp Cloud API 发送通知变得简单。

此包使用了 whatsapp-cloud-api 库,它允许您使用 PHP 编写的任何类型的项目和框架通过 WhatsApp Cloud API 发送消息。

分支自

此包是从 netflie/laravel-notification-whatsapp 分支出来的,以添加对 Laravel 11 的支持。

查看 CHANGELOG.md 获取有关更改的更多信息。

要求

  • PHP 8.1 或更高版本
  • Laravel 10.0 或更高版本

内容

安装

您可以通过 composer 安装此包

composer require danidoble/laravel-notification-whatsapp

设置 WhatsApp Cloud API

创建一个新的 Meta 应用并获取您的 WhatsApp 应用令牌电话号码 ID,遵循 "入门"指南。将它们放在您的 .env 文件中,如下所示

WHATSAPP_FROM_PHONE_NUMBER_ID=your-phone-number-id
WHATSAPP_TOKEN=your-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);
Component::location($name, $address, $latitude, $longitude);

支持 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)。请参阅许可文件获取更多信息。