bitcodesa/msegat

Msegate msegat.com 通知渠道

2.2.0 2024-05-15 06:15 UTC

This package is auto-updated.

Last update: 2024-09-15 07:00:36 UTC


README

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Laravel Msegat 通知渠道

此包提供了一个 Laravel 通知渠道,用于使用 msegat.com SMS 提供商发送短信。

安装

  1. 安装包
composer require bitcodesa/msegat
  1. 发布配置文件
php artisan vendor:publish --tag="msegat-config"
  1. 配置包

编辑已发布的配置文件(config/msegat.php),并使用您的 Msegat 凭据

return [
    "api_url" => env("MSEGAT_API_URL", "https://www.msegat.com/gw/sendsms.php"),
    "api_key" => env("MSEGAT_API_KEY", ""),
    "username" => env("MSEGAT_USERNAME", ""),
    "sender" => env("MSEGAT_SENDER", ""),
    "unicode" => env("MSEGAT_UNICODE", "UTF8"),
];
  1. 配置 Msegat 服务
  • 获取您的凭证
    1. 在 msegat.com 上创建一个账户。
    2. 前往您的仪表板。
    3. 获取您的 API 密钥、用户名和发送者 ID。
  • 设置环境变量
    1. 打开您的 .env 文件。
    2. 添加以下行,将占位符值替换为您的凭证
# Msegat credentials
MSEGAT_API_KEY="xxxxxxxxxxxxxxxxxxxxxxxxx"
MSEGAT_USERNAME="BITCODE"
MSEGAT_SENDER="BITCODE"
  1. 使用消息日志:如果您想使用消息日志创建 Message 模型的记录,您必须发布该表
php artisan vendor:publish --tag="msegat-migrations"

确保您允许创建

MSEGAT_MESSAGES_LOG=true

您可以使用 Messageable 特性来获取与任何可通知模型相关联的任何注册信息

class User extends Authenticatable 
{
    use \BitcodeSa\Msegat\Messageable;
}

然后您可以通过以下方式获取消息

$user->messages

用法

  1. 将 Msegat 通道添加到您的通知类中
<?php

namespace App\Notifications\ReservationNotifications;

use App\Models\Reservation;
use BitcodeSa\Msegat\MsegatMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use BitcodeSa\Msegat\MsegatChannel;

class Reservation extends Notification
{
    use Queueable;
    protected Reservation $reservation;
    
    public function __construct(Reservation $reservation)
    {
        $this->reservation = $reservation;
    }
    public function via(object $notifiable): array
    {
        return [
                    MsegatChannel::class,
                    // Other notification channels
                ];
    }

    public function toMsegat()
    {
        return new MsegatMessage($this->reservation->title);
    }
    // Other notification methods
}

MsegatMessage 对象的可用方法

  • timeToExec("YYYY-MM-DD HH:i:SS") 允许您指定消息应发送的时间。
  • unicode("UTF8") 默认指定为 "UTF8",以指定消息的 Unicode。
  • type("TYPE_SMS") 指定消息类型,您可以选择 SMS 或 OTP。
  • sender($sender) 指定发送者名称。
  • lang("ar") 默认为 "ar",指定 OTP 语言。
  1. 发送短信通知
$user = User::find(1);
$user->notify(new Reservation($reservation));
  1. 发送 OTP 通知

注意:此功能从 msegat.com 的源代码中不可用

$user = User::find(1);
$user->notify(new SendOtp($reservation));

SendOtp

class SendOtp extends Notification
{
    use Queueable;

    public function __construct()
    {
        //
    }

    public function via(object $notifiable): array
    {
        return [MsegatChannel::class];
    }

    public function toMsegat()
    {
        return (new MsegatMessage())
            ->type(MsegatMessage::TYPE_OTP)
            ->lang("en");
    }
}

注意:此功能从 msegat.com 的源代码中不可用

验证 OTP

$user = \App\Models\User::first();
$otp = new \BitcodeSa\Msegat\MsegatVerifyOtp();
$otp->validate($user, $code);

注意:此功能从 msegat.com 的源代码中不可用

可通知标识符

默认情况下,Msegat 通知渠道使用可通知模型上的 phone 属性来识别收件人。如果您的模型使用不同的属性来存储电话号码,您可以通过在您的可通知模型上实现 routeNotificationForMsegat() 方法来覆盖默认行为

class User extends Authenticatable 
{
    use Notifiable;
    
    public function routeNotificationForMsegat()
    {
        return $this->mobile;
    }
}

这指示包使用 mobile 属性而不是 phone 来查找收件人的电话号码。

其他说明

  • 该包支持 Unicode 消息。
  • 该包允许您自定义在收件人手机上显示的发送者名称。

更新日志

请参阅 更新日志,获取有关最近更改的更多信息。

贡献

请参阅 贡献 以获取详细信息。

安全漏洞

请参阅 我们的安全策略 了解如何报告安全漏洞。

致谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 获取更多信息。