shadowbane/laravel-wablas

此包已被废弃且不再维护。作者建议使用 shadowbane/laravel-whatsmeow 包。

通过 Wablas API 发送简单的 WhatsApp 消息

v2.0.4-alpha 2022-01-17 07:30 UTC

This package is auto-updated.

Last update: 2022-04-07 04:26:52 UTC


README

License: GPL v3 Total Downloads

关于

此包提供与 Wablas 印度尼西亚 的简单集成,Wablas 是通过 HTTP API 发送 WhatsApp 消息的服务提供商。

安装

通过 composer 安装此包

composer require shadowbane/laravel-wablas

发布配置

php artisan vendor:publish --provider="Shadowbane\LaravelWablas\LaravelWablasServiceProvider"

用法

配置

将以下值添加到您的 .env 文件中

WABLAS_ENDPOINT=
WABLAS_TOKEN=
WHATSAPP_NUMBER_FIELD=
WHATSAPP_NUMBER_JSON_FIELD=
DEBUG_WHATSAPP_NUMBER=

WABLAS_ENDPOINT 用 Wablas API 端点的 URL 填充。

WABLAS_TOKEN 这是您从 Wablas 账户生成的 token。

WHATSAPP_NUMBER_FIELD 这是您在 users 表中存储用户 WhatsApp 号码的位置。

WHATSAPP_NUMBER_JSON_FIELD 只有在您在数据库的 JSON 列中存储用户的 WhatsApp 号码时才填写此选项,例如,数据可能看起来像这样

{"whatsapp": 0123456}

DEBUG_WHATSAPP_NUMBER 当您的 APP_ENV 设置为 'production' 并且 'APP_DEBUG' 设置为 true 时使用,以防止将其发送给真实用户。

发送文本消息

您可以使用通知类内的 'via' 方法发送文本消息。

app/notifications/WhatsAppNotification:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\LaravelWablas\Exceptions\FailedToSendNotification;
use Shadowbane\LaravelWablas\LaravelWablasChannel;
use Shadowbane\LaravelWablas\LaravelWablasMessage;

class WhatsappNotification extends Notification
{
    protected string $phoneNumber;
    protected string $message;

    /**
     * Create a new notification instance.
     *
     * @param string $phoneNumber
     * @param string $message;
     *
     * @return void
     */
    public function __construct(string $phoneNumber, string $message)
    {
        $this->phoneNumber = $phoneNumber;
        $this->message = $message;
    }

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

    /**
     * @param $notifiable
     *
     * @throws FailedToSendNotification
     *
     * @return LaravelWablasMessage
     */
    public function toWhatsapp($notifiable)
    {
        return LaravelWablasMessage::create()
            ->to($this->phoneNumber)
            ->content($this->message);
    }
}

更改 token

如果您的应用程序具有多个用于不同目的的 token,您可以将 token($token) 方法链到您的 LaravelWablasMessage 实例上

    use Shadowbane\LaravelWablas\LaravelWablasMessage;
    
    ...

    public function toWhatsapp($notifiable)
    {
        return LaravelWablasMessage::create()
            ->token('this-is-another-token-in-my-application')
            ->to($this->phoneNumber)
            ->content($this->message);
    }

使用 Notifiable 特性发送

如果您想通过 notifiable 发送,可以参考以下示例

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\LaravelWablas\LaravelWablasChannel;
use Shadowbane\LaravelWablas\LaravelWablasMessage;

class WhatsappNotification extends Notification
{
    protected string $phoneNumber;
    protected string $message;

    /**
     * Create a new notification instance.
     *
     * @param string $message;
     *
     * @return void
     */
    public function __construct(string $message)
    {
        $this->message = $message;
    }

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

    /**
     * @param $notifiable
     *
     * @return LaravelWablasMessage
     */
    public function toWhatsapp($notifiable)
    {
        return LaravelWablasMessage::create()
            ->content($this->message);
    }
}

然后,您可以使用以下方法触发它

use App\Notifications\WhatsappNotification;

$user->notify(new WhatsappNotification($message));

向多个用户发送

此包允许将数组作为参数传递到 to() 方法中。由于 Wablas 允许用逗号分隔值作为电话号码,我们自动将数组连接起来,并将其作为逗号分隔的值发送到 Wablas API。

示例

    use Shadowbane\LaravelWablas\LaravelWablasMessage;

    ...

    public function toWhatsapp($notifiable)
    {
        return LaravelWablasMessage::create()
            ->token('this-is-another-token-in-my-application')
            ->to([$destination1, $destination2])
            ->content($this->message);
    }

如果您想通过 notifiable 发送,可以通过通知外观发送。

use Illuminate\Support\Facades\Notification;
use App\Models\User;
use App\Notifications\WhatsappNotification;

Notification::send(User::whereSomeCondition(1)->get(), new WhatsappNotification(123) );

注意

如果您将其发送到 notifiable,请确保您的 WHATSAPP_NUMBER_FIELD 反映您存储用户 WhatsApp 号码的字段。

变更日志

请参阅 CHANGELOG 了解最近有哪些更改。

安全

如果您发现任何与安全相关的问题,请发送电子邮件至 adly.shadowbane@gmail.com,而不是使用问题跟踪器。

贡献

请参阅 CONTRIBUTING 以获取详细信息。