clonixdev/laravel-whatsapp-notification-channel

Laravel的WhatsApp通知通道

v1.0.8 2024-09-04 05:22 UTC

This package is auto-updated.

Last update: 2024-09-04 05:30:36 UTC


README

Latest Version on Packagist Software License Total Downloads

此包通过WPPCONNECT SERVER使Laravel发送WhatsApp通知变得简单。

此包是基于Telegram通知包创建的。

感谢Irfaq Syed为此处使用的代码库做出的贡献。

此包100%免费且开源,如果您有兴趣聘请付费支持、安装或实施,请联系Felipe D. Teodoro

内容

安装

您可以通过composer安装此包

composer require felipedamacenoteodoro/laravel-whatsapp-notification-channel

发布配置文件

发布配置文件

php artisan vendor:publish --tag=whatsapp-notification-channel-config

设置您的WhatsApp会话

设置您的venom会话WPPCONNECT SERVER并配置您的WhatsApp会话

# config/whatsapp-notification-channel/services.php

'whatsapp-bot-api' => [
        'whatsappSessionFieldName' => env('WHATSAPP_API_SESSION_FIELD_NAME', ''), //Session field name
        'whatsappSession' => env('WHATSAPP_API_SESSION', ''), // session value
        'base_uri' => env('WHATSAPP_API_BASE_URL', ''), //  Your venom base url api
        'mapMethods' => [ 
            'sendMessage' => 'sendText',
            'sendDocument' => 'sendFile',
        ], /* If you want to change the methods that will be called in the api, you can map them here, example: sendMessage will be replaced by the sendText method of the api */
    ],

代理或桥接支持

如果您的国家无法访问WhatsApp API,您可能无法发送通知,您可以选择按照以下说明设置代理在此处或通过设置上面的base_uri配置与桥接URI使用Web桥接。

您可以在.env文件中设置HTTPS_PROXY

使用方法

现在您可以在Notification类的via()方法中使用该通道。

文本通知

use NotificationChannels\Whatsapp\WhatsappMessage;
use Illuminate\Notifications\Notification;

class InvoicePaid extends Notification
{
    public function via($notifiable)
    {
        return ["whatsapp"];
    }

    public function toWhatsapp($notifiable)
    {
        $url = url('/invoice/' . $this->invoice->id);

        return WhatsappMessage::create()
            // Optional recipient user id.
            ->to($notifiable->whatsapp_number)
            // Markdown supported.
            ->content("Hello there!\nYour invoice has been *PAID*")

            // (Optional) Blade template for the content.
            // ->view('notification', ['url' => $url])
    }
}

附加音频

public function toWhatsapp($notifiable)
{
    return WhatsappFile::create()
            ->to($notifiable->whatsapp_number) // Optional
            ->content('Audio') // Optional Caption
            ->audio('/path/to/audio.mp3');
}

附加照片

public function toWhatsapp($notifiable)
{
    return WhatsappFile::create()
        ->to($notifiable->whatsapp_number) // Optional
        ->content('Awesome *bold* text')
        ->file('/storage/archive/6029014.jpg', 'photo'); // local photo

        // OR using a helper method with or without a remote file.
        // ->photo('https://file-examples-com.github.io/uploads/2017/10/file_example_JPG_1MB.jpg');
}

附加文档

public function toWhatsapp($notifiable)
{
    return WhatsappFile::create()
        ->to($notifiable->whatsapp_number) // Optional
        ->content('Did you know we can set a custom filename too?')
        ->document('https://file-examples-com.github.io/uploads/2017/10/file-sample_150kB.pdf', 'sample.pdf');
}

附加位置

public function toWhatsapp($notifiable)
{
    return WhatsappLocation::create()
        ->latitude('40.6892494')
        ->longitude('-74.0466891');
}

附加视频

public function toWhatsapp($notifiable)
{
    return WhatsappFile::create()
        ->content('Sample *video* notification!')
        ->video('https://file-examples-com.github.io/uploads/2017/04/file_example_MP4_480_1_5MG.mp4');
}

附加GIF文件

public function toWhatsapp($notifiable)
{
    return WhatsappFile::create()
        ->content('Woot! We can send animated gif notifications too!')
        ->animation('https://sample-videos.com/gif/2.gif');

        // Or local file
        // ->animation('/path/to/some/animated.gif');
}

路由消息

您可以通过向to($whatsapp_number)方法提供收件人的WhatsApp号码来发送通知,如前面的示例所示,或者在你的notifiable模型中添加一个routeNotificationForWhatsapp()方法

/**
 * Route notifications for the Whatsapp channel.
 *
 * @return int
 */
public function routeNotificationForWhatsapp()
{
    return $this->whatsapp_number;
}

处理响应

您可以使用通知事件来处理来自WhatsApp的响应。在成功的情况下,您的监听器将接收到一个Message对象,其中包含适用于通知类型的各种字段。

有关响应字段的完整列表,请参阅Venom WhatsApp API的Message对象文档。

按需通知

有时您可能需要向未作为您的应用程序“用户”存储的人发送通知。使用Notification::route方法,在发送通知之前,您可以指定临时的通知路由信息。有关更多详细信息,您可以查看按需通知文档。

use Illuminate\Support\Facades\Notification;

Notification::route('whatsapp', 'WHATSAPP_SESSION')
            ->notify(new InvoicePaid($invoice));

向多个收件人发送

使用通知外观,您可以一次性向多个收件人发送通知。

如果您要向多个用户发送大量通知,WhatsApp API 不允许每秒发送超过30条消息。为了达到最佳效果,请考虑在8-12小时的大间隔内分批发送通知。

请注意,您的机器人无法向同一群组发送每分钟超过20条消息。

如果超过限制,您将开始收到429错误。更多详细信息,请参考 WhatsApp API FAQ

use Illuminate\Support\Facades\Notification;

// Recipients can be an array of numbers or collection of notifiable entities.
Notification::send($recipients, new InvoicePaid());

可用方法

共享方法

这些方法是可选的,并且适用于所有 API 方法。

  • to(int|string $number):接收者的号码。
  • session(string $session):如果您想覆盖特定通知的默认会话,请指定会话。
  • options(array $options):允许您添加额外的参数或覆盖有效载荷。
  • getPayloadValue(string $key):获取给定键的有效载荷值。

WhatsApp消息方法

有关支持参数的更多信息,请参阅这些文档

  • content(string $content, int $limit = null):通知消息,支持 Markdown。有关支持 Markdown 样式的更多信息,请参阅这些文档
  • view(string $view, array $data = [], array $mergeData = []):(可选)如果想要使用视图文件而不是 content() 方法,请指定带有 WhatsApp 支持的 Markdown 语法内容的 Blade 模板名称。
  • chunk(int $limit = 4096):(可选)发送时按部分发送的消息字符块大小(用于长消息)。注意:分块消息将受到每秒一条消息的限制,以符合 WhatsApp 的速率限制要求。

WhatsApp位置方法

  • latitude(float|string $latitude):位置的纬度。
  • longitude(float|string $longitude):位置的经度。
  • title(string $title):位置的标题。
  • description(string $description):位置的描述。

WhatsApp文件方法

  • content(string $content):(可选)文件标题,支持 Markdown。有关支持 Markdown 样式的更多信息,请参阅这些文档
  • view(string $view, array $data = [], array $mergeData = []):(可选)如果想要使用视图文件而不是 content() 方法,请指定带有 WhatsApp 支持的 HTML 或 Markdown 语法内容的 Blade 模板名称。
  • file(string|resource|StreamInterface $file, string $type, string $filename = null):本地文件路径或远程 URL,文件 $type(例如:photoaudiodocumentvideoanimationvoicevideo_note)以及可选的文件名和扩展名。例如:sample.pdf。您可以使用辅助方法而不是使用此方法,以便更容易地处理文件附件。
  • photo(string $file):辅助方法用于附加照片。
  • audio(string $file):辅助方法用于附加音频文件(MP3文件)。
  • document(string $file, string $filename = null):辅助方法用于附加文档或任何文件作为文档。
  • video(string $file):辅助方法用于附加视频文件。
  • animation(string $file):辅助方法用于附加动画 gif 文件。

WhatsApp联系人方法

  • phoneNumber(string $phoneNumber):联系人电话号码。
  • name(string $name):全名。
  • firstName(string $firstName):(如果使用 name 参数则为可选)联系人的名字。
  • lastName(string $lastName):(可选)联系人的姓氏。

简单 WhatsApp API

对于简单使用,请考虑使用 whatsapp-api

变更日志

请参阅 CHANGELOG 了解最近的变化。

安全性

如果您发现任何安全相关的问题,请通过电子邮件发送至 felipe.devops@gmail.com,而不是使用问题跟踪器。

贡献

有关详细信息,请参阅 CONTRIBUTING

鸣谢

许可协议

MIT 许可证 (MIT)。请参阅 许可文件 以获取更多信息。