定制 missael-anda/laravel-whatsapp 二次开发

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

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

missael-anda/laravel-whatsapp

Composer 安装命令:

composer require missael-anda/laravel-whatsapp

包简介

A Whatsapp Business Cloud API wrapper for Laravel.

README 文档

README

A Laravel package for the Whatsapp Business Cloud API.

Installation

composer require missael-anda/laravel-whatsapp

Configuration

You will need to set the WHATSAPP_TOKEN and WHATSAPP_NUMBER_ID values in your .env.

For further configuration, please see config/whatsapp.php. You can modify the configuration by copying it to your local config directory or by defining the environment variables used in the config file:

php artisan vendor:publish --provider="MissaelAnda\Whatsapp\WhatsappServiceProvider" --tag=config

Usage

You can send a messages to a single or multiple clients.

use MissaelAnda\Whatsapp\Messages;

Whatsapp::send('13333333333', Messages\TemplateMessage::create()
    ->name('one_time_password')
    ->language('en_US')
    ->body(Messages\Components\Body::create([
        Messages\Components\Parameters\Text::create('000000'),
    ])));

You can also respond to other messages in the conversation with any message type (except reaction message):

use MissaelAnda\Whatsapp\Messages\TextMessage;

Whatsapp::send('13333333333', TextMessage::create('Answer to your message')->respondTo('wamid.91n23...'));

and you can mark messages as read:

Whatsapp::markRead('wamid.91n23...');

By default the messages will be sent from the default_number_id, if you want to use other you can use Whatsapp::numberId() or add the alias to the config's numbers list and use Whatsapp::numberName(). Also you can set the token manually with Whatsapp::token() or you can set both the token and numberId you can use Whatsapp::client()

Supported messages

  • Text Message
  • Media Message
  • Template Message
  • Reaction Message
  • Contacts Message
  • Interactive Message
  • Location Message

Media

You can also manage media with:

Whatsapp::uploadMedia(string $file, string $type = null, bool $retrieveAllData = true): \MissaelAnda\Whatsapp\WhatsappMedia|string
Whatsapp::getMedia(string $mediaId, bool $download = false): \MissaelAnda\Whatsapp\WhatsappMedia
Whatsapp::deleteMedia(\MissaelAnda\Whatsapp\WhatsappMedia|string $id): bool
Whatsapp::downloadMedia(string|\MissaelAnda\Whatsapp\WhatsappMedia $media): \MissaelAnda\Whatsapp\WhatsappMedia

Business Profile

There are also two ways to manage the number's business profile:

Whatsapp::getProfile(): \MissaelAnda\Whatsapp\BusinessProfile
Whatsapp::updateProfile(\MissaelAnda\Whatsapp\BusinessProfile|array $data): bool

Webhooks

Whatsapp allows you to subscribe to webhooks to receive notifications and most importantly messages from your customers. Both subscriptions and notifications are handled out of the box in the route whatsapp/webhook (you can change the path with the WHATSAPP_WEBHOOK_PATH env variable).

When a you register the webhook meta will send a subscription, this requires a verification token that you set, you will need to set it with WHATSAPP_WEBHOOK_VERIFY_TOKEN env setting. When receiving a subscription intent a MissaelAnda\Whatsapp\Events\SubscriptionIntentReceived event will be fired, if the request is successful the MissaelAnda\Whatsapp\Events\SuccessfullySubscribed event will fire too.

On the other hand notifications will trigger a MissaelAnda\Whatsapp\Events\WebhookReceived event with all the payload, if you want to protect this route verifying the sha256 signature you must set the WHATSAPP_WEBHOOK_SIGNATURE_VERIFY to true and the WHATSAPP_SECRET to your whatsapp's app secret.

If the payload is invalid a MissaelAnda\Whatsapp\Events\UnprocessableWebhookPayload event will be fired with the exception describing the error.

If you want to know when a specific notification is fired you can subscribe to this events:

  • MissaelAnda\Whatsapp\Events\WebhookEntry generic entry
  • MissaelAnda\Whatsapp\Events\MessagesReceived for the messages entry

Notification Channel

This library has support for channel notification, just add the routeNotificationForWhatsapp() function to the Notifiable user (it can return a single whatsapp_id or an array of them):

class User extends Authenticatable
{
    use Notifiable;

    /**
     * @return string|array
     */
    public function routeNotificationForWhatsapp(): string|array
    {
        return "{$this->phone_code}{$this->phone}";
    }
}

Now just create a notification that implements the toWhatsapp() function:

//...
use MissaelAnda\Whatsapp\Messages\TemplateMessage;
use MissaelAnda\Whatsapp\Messages\Components\Parameters;
use MissaelAnda\Whatsapp\WhatsappChannel;

class VerificationCode extends Notification
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct(protected string $code)
    {
        //
    }

    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [WhatsappChannel::class];
    }

    /**
     * Get the message representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return \MissaelAnda\Whatsapp\Messages\WhatsappMessage
     */
    public function toWhatsapp($notifiable)
    {
        return TemplateMessage::create('one_time_password')
            ->language('en_US')
            ->body([
                Parameters\Text::create('123456'),
            ]);
    }
}

Now you can send whatsapp notifications:

$user->notify(new VerificationCode('12345678'));

Configuration file

return [
    /**
     * The whatsapp token to be used.
     */
    'token' => env('WHATSAPP_TOKEN'),

    /**
     * The whatsapp's app secret code. Required for webhook request signature verification.
     */
    'secret' => env('WHATSAPP_SECRET'),

    /**
     * The default NUMBER ID used to send the messages.
     */
    'default_number_id' => env('WHATSAPP_NUMBER_ID'),

    /**
     * If you want to use other number id's you can add them here so you can call
     * `numberName` with the name you provide here and make it easier to change
     * the phone where the messages are sended.
     */
    'numbers' => [
        // 'fallback' => env('WHATSAPP_FALLBACK_NUMBER_ID'),
    ],

    'webhook' => [
        /**
         * Wether to enable the webhook routes
         */
        'enabled' => env('WHATSAPP_WEBHOOK_ENABLED', true),

        /**
         * The webhook path, by default "/whatsapp/webhook"
         */
        'path' => env('WHATSAPP_WEBHOOK_PATH', 'whatsapp'),

        /**
         * The webhook verification token.
         * For more information check https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests
         */
        'verify_token' => env('WHATSAPP_WEBHOOK_VERIFY_TOKEN'),

        /**
         * Wether the webhook request signature should be verified or not.
         */
        'verify_signature' => env('WHATSAPP_WEBHOOK_SIGNATURE_VERIFY', false),
    ],
];

Missing features

  • Register/deregister/validate new phone numbers

License

This project is licensed under the MIT License.

missael-anda/laravel-whatsapp 适用场景与选型建议

missael-anda/laravel-whatsapp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.19k 次下载、GitHub Stars 达 67, 最近一次更新时间为 2023 年 02 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 8.19k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 67
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 67
  • Watchers: 3
  • Forks: 27
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-22