valinteca/msegat

valinteca msegat

1.0.0 2024-07-18 15:40 UTC

This package is auto-updated.

Last update: 2024-09-18 15:56:41 UTC


README

Valinteca 的一个 composer 包,用于 Laravel 应用程序轻松集成 Msegat 平台。

功能

  • 发送短信消息
  • 发送个性化消息
  • 发送 & 验证 OTP 码
  • 管理发送者
  • 消息成本计算
  • 余额查询

安装

composer require valinteca/msegat

发布配置

php artisan vendor:publish --provider="Valinteca\Msegat\MsegatServiceProvider"

这将把配置文件发布到 config/msegat.php


然后向 .env 文件中添加这些密钥

MSEGAT_USERNAME="VALUE_HERE" # From your msegat subscription
MSEGAT_API_KEY="VALUE_HERE" # From your msegat subscription
MSEGAT_SENDER_NAME="VALUE_HERE" # Custom sender name
MSEGAT_LANG="VALUE_HERE" # Language ("En" or "Ar")

使用方法

use Valinteca\Msegat\Facades\Msegat;

发送消息

to: 接受数字或数字数组

at: 接受发送时间的格式 "Y-m-d H:i:s" 或 Carbon 实例

message: 接受消息文本

send: 执行操作并返回响应

示例

  • 使用默认设置
Msegat::to('05xxxxxxxx')
    ->message('hello world')
    ->send();
  • 自定义发送者
    使用 sender 方法来自定义发送者名称。如果您没有使用此方法,则将使用配置/环境中的默认发送者名称。
Msegat::sender('another sender')
    ->to('05xxxxxxxx')
    ->message('hello world')
    ->send();
  • 多个号码
    将数组传递给 to 方法。
Msegat::to(['05xxxxxxxx', '05xxxxxxxx'])
    ->message('hello world')
    ->send();
  • 在稍后时间
    将日期时间传递给 at 方法。
// Using string datetime format
Msegat::to('05xxxxxxxx', '05xxxxxxxx')
    ->message('hello world')
    ->at('2023-05-01 20:10:05')
    ->send();

// Using carbon instance
Msegat::to('05xxxxxxxx', '05xxxxxxxx')
    ->message('hello world')
    ->at(now()->addMinutes(5))
    ->send();
  • 自定义选项
    传递给 options 方法的选项有

    1- reqBulkId: 获取批处理消息 ID(truefalse,默认为 false

    2- msgEncoding: (UTF8windows-1256,默认为 UTF8

    3- reqFilter: 过滤重复的数字(truefalse,默认为 true

Msegat::to('05xxxxxxxx')
    ->options([
        'reqBulkId' => true,
        'msgEncoding' => 'windows-1256',
        'reqFilter' => false,
    ])
    ->message('hello world')
    ->send();

发送个性化消息

个性化消息是具有不同 SMS 内容的消息。要发送个性化消息,请使用包含每个数字的变量数组的 sendPersonalized 方法。

消息体应包含包含应在大括号 {} 之间的变量的消息体。

Msegat::to(['05xxxxxxxx', '05yyyyyyyy'])
    ->message('Hello {name}. Your order {order} will be delivered soon')
    ->sendPersonalized([
        ['name' => 'Mohammed', 'order' => '123'],
        ['name' => 'Ahmed', 'order' => '456'],
    ]);

    // Messages:
    // 05xxxxxxxx: "Hello Mohammed. Your order 123 will be delivered soon" 
    // 05yyyyyyyy: "Hello Ahmed. Your order 456 will be delivered soon"

发送测试消息

您可以免费测试 Msegat API。您将每天获得免费的短信。要测试服务,您可以使用 sendTestMessage 方法发送短信。

此方法使用预定义的发送者名称和消息文本,因此您只需定义号码即可。

Msegat::to('05xxxxxxxx')
    ->sendTestMessage();

余额查询

要查询您的余额,请使用 getBalance 方法

Msegat::getBalance();

获取消息

要获取批量 ID 的消息,请使用 forbulkId 方法与 getMessages。结果分页,默认返回第 1 页。

Msegat::forBulkId('bulk-id-returned-from-send-method')
    ->getMessages();

要定义页码,请使用 page 方法

Msegat::forBulkId('bulk-id-returned-from-send-method')
    ->page(2)
    ->getMessages();

要定义限制,请使用 limit 方法

Msegat::forBulkId('bulk-id-returned-from-send-method')
    ->page(1)
    ->limit(5)
    ->getMessages();

计算消息成本

Msegat::to(['05xxxxxxxx', '05yyyyyyyy'])
    ->message('this is message')
    ->calculateCost();

响应

{
    "success": true,
    "data": {
        "cost": "3,9"
    }
}

获取用户余额

Msegat::getBalance();

响应

{
    "success": true,
    "data": {
        "balance": "3042.00"
    }
}