felipedamacenoteodoro/laravel-whatsapp-notification-channel

v1.0.3 2023-05-16 16:04 UTC

This package is auto-updated.

Last update: 2024-09-16 19:06:36 UTC


README

Latest Version on Packagist Software License Total Downloads

此包通过WPPCONNECT SERVER使用Laravel轻松发送WhatsApp通知。

此包基于电报通知包创建。

感谢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配置使用网桥。

您可以在.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');
}

消息路由

您可以通过提供收件人的WhatsApp号码来发送通知,就像前面的示例中一样,或者在你的可通知模型中添加routeNotificationForWhatsapp()方法。

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

处理响应

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

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

按需通知

有时您可能需要向未存储为您的应用程序“用户”的人发送通知。使用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 常见问题解答

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,文件类型(例如: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

变更日志

请参阅变更日志以获取有关最近更改的更多信息。

安全

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

贡献

请参阅CONTRIBUTING获取详细信息。

致谢

许可

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