承接 logicalcrow/laravel-whatsapp-api 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

logicalcrow/laravel-whatsapp-api

Composer 安装命令:

composer require logicalcrow/laravel-whatsapp-api

包简介

A Whatsapp Business Cloud API wrapper for Laravel.

README 文档

README

A Laravel package for the Whatsapp Business Cloud API.

Installation

composer require logicalcrow/laravel-whatsapp-api

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="Logicalcrow\Whatsapp\WhatsappServiceProvider" --tag=config

Usage

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

use Logicalcrow\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 Logicalcrow\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): \Logicalcrow\Whatsapp\WhatsappMedia|string
Whatsapp::getMedia(string $mediaId, bool $download = false): \Logicalcrow\Whatsapp\WhatsappMedia
Whatsapp::deleteMedia(\Logicalcrow\Whatsapp\WhatsappMedia|string $id): bool
Whatsapp::downloadMedia(string|\Logicalcrow\Whatsapp\WhatsappMedia $media): \Logicalcrow\Whatsapp\WhatsappMedia

Business Profile

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

Whatsapp::getProfile(): \Logicalcrow\Whatsapp\BusinessProfile
Whatsapp::updateProfile(\Logicalcrow\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 Logicalcrow\Whatsapp\Events\SubscriptionIntentReceived event will be fired, if the request is successful the Logicalcrow\Whatsapp\Events\SuccessfullySubscribed event will fire too.

On the other hand notifications will trigger a Logicalcrow\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 Logicalcrow\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:

  • Logicalcrow\Whatsapp\Events\WebhookEntry generic entry
  • Logicalcrow\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 Logicalcrow\Whatsapp\Messages\TemplateMessage;
use Logicalcrow\Whatsapp\Messages\Components\Parameters;
use Logicalcrow\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 \Logicalcrow\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.

logicalcrow/laravel-whatsapp-api 适用场景与选型建议

logicalcrow/laravel-whatsapp-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 08 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-09