logicalcrow/laravel-whatsapp-api

为 Laravel 提供的 Whatsapp 商业云 API 包装器。

v1.0.1 2024-08-09 16:21 UTC

This package is auto-updated.

Last update: 2024-09-09 16:27:23 UTC


README

Whatsapp 商业云 API 提供的 Laravel 包。

安装

composer require logicalcrow/laravel-whatsapp-api

配置

您需要在 .env 中设置 WHATSAPP_TOKENWHATSAPP_NUMBER_ID 的值。

有关进一步配置,请参阅 config/whatsapp.php。您可以通过将其复制到本地的 config 目录或通过定义配置文件中使用的环境变量来修改配置。

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

使用

您可以向单个或多个客户端发送消息。

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'),
    ])));

您还可以以任何消息类型(除反应消息外)响应对话中的其他消息

use Logicalcrow\Whatsapp\Messages\TextMessage;

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

并将消息标记为已读

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

默认情况下,消息将从 default_number_id 发送,如果您想使用其他号码,可以使用 Whatsapp::numberId() 或将别名添加到配置的 numbers 列表中并使用 Whatsapp::numberName()。您还可以使用 Whatsapp::token() 手动设置令牌,或者您可以设置令牌和 numberId 并使用 Whatsapp::client()

支持的消息

  • 文本消息
  • 媒体消息
  • 模板消息
  • 反应消息
  • 联系人消息
  • 交互式消息
  • 位置消息

媒体

您还可以使用

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

商业资料

有两种方法可以管理号码的商业资料

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

Webhooks

Whatsapp 允许您订阅 webhooks 以接收通知和来自客户的消息。订阅和通知都在路线 whatsapp/webhook 中处理(您可以使用 WHATSAPP_WEBHOOK_PATH 环境变量更改路径)。

当您注册 webhook 时,会发送一个订阅,这需要一个您设置的验证令牌。您需要使用 WHATSAPP_WEBHOOK_VERIFY_TOKEN 环境设置来设置它。当收到订阅意图时,将触发 Logicalcrow\Whatsapp\Events\SubscriptionIntentReceived 事件,如果请求成功,将触发 Logicalcrow\Whatsapp\Events\SuccessfullySubscribed 事件。

另一方面,通知将触发带有所有有效负载的 Logicalcrow\Whatsapp\Events\WebhookReceived 事件,如果您想通过验证 sha256 签名来保护此路由,则必须将 WHATSAPP_WEBHOOK_SIGNATURE_VERIFY 设置为 true 并将 WHATSAPP_SECRET 设置为您的 WhatsApp 应用程序密钥。

如果有效负载无效,将触发带有描述错误的异常的 Logicalcrow\Whatsapp\Events\UnprocessableWebhookPayload 事件。

如果您想了解何时触发特定通知,可以订阅这些事件

  • Logicalcrow\Whatsapp\Events\WebhookEntry 通用条目
  • Logicalcrow\Whatsapp\Events\MessagesReceived 用于 messages 条目

通知渠道

此库支持频道通知,只需将 routeNotificationForWhatsapp() 函数添加到可通知的用户(它可以返回单个 whatsapp_id 或它们的数组)

class User extends Authenticatable
{
    use Notifiable;

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

现在只需创建一个实现 toWhatsapp() 函数的通知

//...
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'),
            ]);
    }
}

现在您可以发送 WhatsApp 通知

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

配置文件

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),
    ],
];

缺少的功能

  • 注册/注销/验证新电话号码

许可证

本项目采用 MIT 许可证 许可。