wilsonglasser/laravel-chatapi-whatsapp

使用 Chat API 发送 WhatsApp 消息

1.0.0 2018-11-05 06:11 UTC

This package is auto-updated.

Last update: 2024-09-13 22:03:40 UTC


README

Latest Version on Packagist Software License Total Downloads

此包使得使用 Laravel 5 通过 Chat API 发送 WhatsApp 消息变得简单。

内容

安装

您可以通过 composer 安装此包

composer require wilsonglasser/laravel-chatapi-whatsapp

您必须安装服务提供者

// config/app.php
'providers' => [
    ...
    NotificationChannels\ChatAPI\ChatAPIServiceProvider::class,
],

配置

配置您的凭据

// config/services.php
...
'chatapi' => [
    'token'          => env('CHATAPI_TOKEN', ''),
    'api_url'       => env('CHATAPI_URL', ''),
],
...

使用

现在您可以在通知类中的 via() 方法中使用此通道。

use NotificationChannels\ChatAPI\ChatAPIChannel;
use NotificationChannels\ChatAPI\ChatAPIMessage;
use Illuminate\Notifications\Notification;

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

    public function toChatAPI($notifiable)
    {
        return ChatAPIMessage::create()
            ->to($notifiable->phone) // your user phone
            ->file('/path/to/file','My Photo.jpg')
            ->content('Your invoice has been paid');
    }
}

路由消息

您可以通过提供接收者的 chat id 给 to($phone) 方法来发送通知,如下面的例子所示,或者在你的可通知模型中添加 routeNotificationForChatAPI() 方法

...
/**
 * Route notifications for the Telegram channel.
 *
 * @return int
 */
public function routeNotificationForChatAPI()
{
    return $this->phone;
}
...

可用的消息方法

  • to($phone): (integer) 接收者的电话。
  • content('message'): (string) 消息。
  • file('/path/to/file','My Photo.jpg'): (string) 文件真实路径,您也可以发送文件内容,并传递两个额外的参数用于文件名和文件 MIME 类型(必需)
  • file('/path/to/file','My Photo.jpg','image/jpg')