shadowbane/laravel-whatsmeow

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

安装: 130

依赖: 0

建议者: 0

安全: 0

星标: 3

关注者: 2

分支: 0

开放问题: 0

类型:项目

v1.0.0 2022-04-26 03:21 UTC

This package is auto-updated.

Last update: 2024-09-26 08:26:14 UTC


README

License: GPL v3 Total Downloads

关于

此包提供了与Whatsmeow的简单集成,这是我自己用于发送WhatsApp消息的API。

安装

使用composer安装此包

composer require shadowbane/laravel-whatsmeow

发布配置

php artisan vendor:publish --provider="Shadowbane\Whatsmeow\WhatsmeowServiceProvider"

用法

配置

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

WHATSMEOW_ENDPOINT=
WHATSMEOW_TOKEN=
WHATSAPP_NUMBER_FIELD=
WHATSAPP_NUMBER_JSON_FIELD=
DEBUG_WHATSAPP_NUMBER=

WHATSMEOW_ENDPOINT 用Wablas API端点的URL填充。

WHATSMEOW_TOKEN 这是您从Wablas账户生成的令牌。

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

WHATSAPP_NUMBER_JSON_FIELD 如果您在数据库中JSON列中存储用户的WhatsApp号码,则仅填充此值,例如,数据可能看起来像这样

{"whatsapp": 0123456}

DEBUG_WHATSAPP_NUMBER 当您的APP_ENV设置为'local'时,此功能用于防止将其发送给真实用户。它将发送到调试号码。

发送文本消息

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

app/notifications/WhatsAppNotification:

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\Whatsmeow\Exceptions\FailedToSendNotification;
use Shadowbane\Whatsmeow\WhatsmeowChannel;
use Shadowbane\Whatsmeow\WhatsmeowMessage;

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 [WhatsmeowChannel::class];
    }

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

更改令牌

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

    use Shadowbane\Whatsmeow\WhatsmeowMessage;
    
    ...

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

使用可通知的特质发送

如果您想通过可通知的发送,可以参考此示例

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use Shadowbane\Whatsmeow\WhatsmeowChannel;
use Shadowbane\Whatsmeow\WhatsmeowMessage;

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 [WhatsmeowChannel::class];
    }

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

然后,您可以触发它

use App\Notifications\WhatsappNotification;

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

向多个用户发送

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

示例

    use Shadowbane\Whatsmeow\WhatsmeowMessage;

    ...

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

如果您更喜欢向可通知的发送,可以通过通知外观发送。

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

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

注意

如果您向可通知的发送,请确保您的WHATSAPP_NUMBER_FIELD反映了您存储用户WhatsApp号码的字段。

变更日志

请参阅CHANGELOG以获取更多有关最近更改的信息。

安全

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

贡献

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