定制 ka4ivan/laravel-notification-channel-whatsapp 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ka4ivan/laravel-notification-channel-whatsapp

Composer 安装命令:

composer require ka4ivan/laravel-notification-channel-whatsapp

包简介

Whatsapp Notifications Channel for Laravel

README 文档

README

License Build Status Latest Stable Version Total Downloads

This package makes it easy to send notifications using the Whatsapp Messenger with Laravel.

Contents

Installation

You can install the package via composer:

composer require ka4ivan/laravel-notification-channel-whatsapp

Setting up your Whatsapp Bot

This document describes the steps you must take to become a Tech Provider Whatsapp

Set config

Next we need to add tokens to our Laravel configurations. Create a new Whatsapp section inside config/services.php and place the page token there:

// config/services.php
'whatsapp' => [
    'access_token' => env('WHATSAPP_ACCESS_TOKEN', ''),
    'number_id' => env('WHATSAPP_NUMBER_ID', ''),
    'api_version' => env('WHATSAPP_API_VERSION', '22.0'),
],

Usage

You can now use the Whatsapp channel in your via() method, inside the InvoicePaid class. The to($recipientId) Whatsapp ID (Phone Number) method defines the Whatsapp user, you want to send the notification to.

use NotificationChannels\Whatsapp\WhatsappChannel;
use NotificationChannels\Whatsapp\WhatsappMessage;

use Illuminate\Notifications\Notification;

class ChannelConnected extends Notification
{
    public function via($notifiable)
    {
        return [WhatsappChannel::class];
    }

    public function toWhatsapp($notifiable)
    {
        $apiVersion = config('services.whatsapp.api_version');
        $accessToken = config('services.whatsapp.access_token');
        $numberId = config('services.whatsapp.number_id');

        return WhatsappMessage::create()
            ->to($notifiable->whatsapp_id)  // Optional
            ->setApiVersion($apiVersion)    // Optional
            ->setAccessToken($accessToken)  // Optional
            ->setNumberId($numberId)        // Optional
            ->previewUrl(false)             // Optional
            ->text('Congratulations, the communication channel is connected');
    }
}

The notification will be sent from your Whatsapp page, whose page token you have configured earlier. Here's a screenshot preview of the notification inside the chat window.

image

Message Examples

Basic Text Message
return WhatsappMessage::create('You have just paid your monthly fee! Thanks');

Routing a message

You can either send the notification by providing with the page-scoped user id of the recipient to the to($recipientId) Whatsapp ID (Phone Number) method like shown in the above example or add a routeNotificationForWhatsapp() method in your notifiable model:

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

Types of messages

Audio Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • mediaId(''): (string) Whatsapp media ID (Only if using uploaded media)
  • link(''): (string) Media link (Only if using hosted media (not recommended))
WhatsappAudioMessage::create()
    ->link('audio url');

Call-To-Action (CTA URL) Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • body(''): (string) Message body text.
  • header(null): (Header) Message header object. (ButtonCtaUrlImageHeader/ButtonCtaUrlTitleHeader)
  • footer(''): (string) Message footer text.
  • button(): (ButtonCtaUrl) Message button object.
WhatsappCtaUrlMessage::create()
    ->header(ButtonCtaUrlTitleHeader::create()->title('header text'))
    ->body('body text')
    ->footer('footer text')
    ->button(ButtonCtaUrl::create()->displayText('button text')->url('button url'));

Document Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • caption(''): (string) Notification caption.
  • filename(''): (string) Document filename, with extension. The WhatsApp client will use an appropriate file type icon based on the extension.
  • mediaId(''): (string) Whatsapp media ID (Only if using uploaded media)
  • link(''): (string) Whatsapp media link (Only if using hosted media (not recommended))
WhatsappDocumentMessage::create()
    ->caption('file caption')
    ->filename('file name')
    ->link('file url');

Image Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • caption(''): (string) Notification caption.
  • mediaId(''): (string) Whatsapp media ID (Only if using uploaded media)
  • link(''): (string) Whatsapp media link (Only if using hosted media (not recommended))
WhatsappImageMessage::create()
    ->caption('image caption')
    ->link('image url');

Location Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • latitude(''): (string) The geographic latitude of the location (e.g., '50.4501').
  • longitude(''): (string) The geographic longitude of the location (e.g., '30.5234').
  • name(''): (string) The name or label of the location (e.g., 'Independence Square').
  • address(''): (string) The full address (optional), displayed under the name (e.g., 'Khreshchatyk St, Kyiv, Ukraine').
WhatsappLocationMessage::create('latitude', 'longitude')
    ->address('address text')
    ->name('name text');

Location Request Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • body(''): (string) Message body text.
WhatsappLocationRequestMessage::create('Location, pls');

Text Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • text(''): (string) Notification message.
  • previewUrl(true): (boolean) Link Preview.
  • setApiVersion($apiVersion): (string) Set Default Graph API Version.
  • setAccessToken($accessToken): (string) Set the access token used for authenticating API requests.
  • setNumberId($numberId): (string) Set the Whatsapp number ID for API requests.
WhatsappMessage::create('Your order has been confirmed!');

Reaction

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • emoji(''): (string) Emoji reaction. Unicode escape sequence example: \uD83D\uDE00. Emoji example: 😀
  • messageId(''): (string) Whatsapp Message ID
WhatsappReaction::create()
    ->emoji('😊')
    ->messageId('Whatsapp message ID');

Reply Buttons Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • body(''): (string) Message body text.
  • header(null): (Header) Message header object. (ButtonCtaUrlImageHeader/ButtonCtaUrlTitleHeader)
  • footer(''): (string) Message footer text.
  • buttons([]): (array) Message buttons array [ButtonReply, ButtonReply, ButtonReply].
WhatsappButtonReplyMessage::create()
    ->header(ButtonReplyTitleHeader::create()->title('header text'))
    ->body('body text')
    ->footer('footer text')
    ->buttons([
        ButtonReply::create()->id('button id 1')->title('button title 1'),
        ButtonReply::create()->id('button id 2')->title('button title 2'),
        ButtonReply::create()->id('button id 3')->title('button title 3'),
    ]);

Video Message

  • to($recipientId): (string) User (recipient) Whatsapp ID (Phone Number).
  • caption(''): (string) Notification caption.
  • mediaId(''): (string) Whatsapp media ID (Only if using uploaded media)
  • link(''): (string) Whatsapp media link (Only if using hosted media (not recommended))
WhatsappVideoMessage::create()
    ->caption('video caption')
    ->link('video url');

Reply to a message (context)

All message types support reply threading via context(). Pass the WhatsApp message ID (wamid) of the message you want to reply to — the recipient will see it as a quoted reply.

  • context(string $messageId): WhatsApp message ID (wamid) to quote
WhatsappMessage::create('Got it, thanks!')
    ->context('wamid.HBgNxxxxxxxxxxxx');

// Works on any message type
WhatsappImageMessage::create('Here is the photo')
    ->link('https://example.com/photo.jpg')
    ->context('wamid.HBgNxxxxxxxxxxxx');

Sending multiple messages

If you need to send multiple files (regardless of the message type)

/**
 * @param $notifiable
 * @return \NotificationChannels\Whatsapp\Message|array
 * @throws \NotificationChannels\Whatsapp\Exceptions\CouldNotCreateMessage
 */
public function toWhatsApp($notifiable)
{
    $text = 'text';

    $files = $this->getFiles();

    if (!empty($files)) {
        $messages = [];

        $first = true;
        foreach ($files as $url => $name) {
            $message = WhatsappDocumentMessage::create()
                ->link($url)
                ->filename(Str::substr($name, -32));

            if ($first) {
                $message->caption($text);
                $first = false;
            }

            $messages[] = $message;
        }

        return $messages;
    }

    return WhatsappMessage::create()->text($text);
}

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information.

ka4ivan/laravel-notification-channel-whatsapp 适用场景与选型建议

ka4ivan/laravel-notification-channel-whatsapp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 656 次下载、GitHub Stars 达 5, 最近一次更新时间为 2025 年 05 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「whatsapp」 「WhatsApp API」 「laravel notification」 「laravel message」 「laravel notification channel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ka4ivan/laravel-notification-channel-whatsapp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ka4ivan/laravel-notification-channel-whatsapp 我们能提供哪些服务?
定制开发 / 二次开发

基于 ka4ivan/laravel-notification-channel-whatsapp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 656
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-16