danvick/yii2-jumbefupi

Yii2 扩展,用于与 JumbeFupi 短信网关集成

安装: 40

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

dev-main 2024-03-09 10:14 UTC

This package is auto-updated.

Last update: 2024-09-09 11:11:00 UTC


README

Yii2 扩展,用于与 JumbeFupi 短信网关集成

安装

安装此扩展的首选方式是通过 composer

运行以下命令之一:

php composer.phar require --prefer-dist danvick/yii2-jumbefupi "*"

或者在您的 composer.json 文件的 require 部分添加以下内容:

"danvick/yii2-jumbefupi": "*"

要始终使用来自 Github 的最新版本,在您的 composer.json 文件中,将此存储库添加到 repositories 部分。

{
  ...
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/danvick/yii2-jumbefupi"
    }
  ],
}

用法

该扩展用作应用程序组件,并在应用程序配置中按此类配置

'components' => [
    ...
    'jumbefupi' => [
        'class' => JumbefupiGateway::class,
        'gatewayUsername' => null,                          // REQUIRED - Your JumbeFupi username
        'gatewayApiKey' => null,                            // REQUIRED - Your JumbeFupi API key
        'senderId' => null,                                 // REQUIRED - Your SenderID / Alphanumeric. If not set here, should be set when sending message
        'callbackUrl' => null,                              // OPTIONAL - The URL where message status response from JumbeFupi Gateway will be sent
        'model' => 'danvick\jumbefupi\models\SmsMessage',   // OPTIONAL - (Default: danvick\jumbefupi\models\SmsMessage)
        'db' => 'db'                                        // OPTIONAL - the DB connection component for the messages table
        'cacheBalance' => false,                            // OPTIONAL - Whether to store balance after enquiry - cache will be burst on message sending
        'cache' => 'cache',                                 // OPTIONAL - The cache component to store balance if cacheBalance is true 
        'balanceCacheKey' => 'JUMBEFUPI_BALANCE',           // OPTIONAL - Cache key for storage of JumbeFupi account balance
        'useFileTransport' => false,                        // OPTIONAL - 
    ],
]

您还应该配置扩展迁移,以便在您的应用程序配置中运行,通过将 danvick\jumbefupi\migrations 添加到您的 migrationNamespaces

'controllerMap' => [
    'migrate' => [
        'class' => 'yii\console\controllers\MigrateController',
        ...
        'migrationNamespaces' => [
            ...
            'danvick\jumbefupi\migrations'
        ],
    ],
],

发送消息

use danvick\jumbefupi\TextMessage;
...
Yii::$app->jumbefupi->send(new TextMessage(['recipients' => $recipients, 'text' => $message]))

$recipients: 以逗号分隔的电话号码字符串或电话号码字符串数组

$message: 要发送的文本消息

$senderId (可选): 在用户中显示为消息发送者的字母数字发送者ID。如果已在配置中的 jumbefupi 组件中设置,则为可选

处理状态回调/检查消息状态

您可以选择创建一个端点来处理消息状态回调,以便在消息状态发生变化时(例如,当消息送达或其它情况)或手动调用 getMessageStatus() 函数

  1. 设置回调

    设置一个处理网关回调的动作,然后更新数据库中的消息状态。重要的是,此动作必须是非认证的

    回调的服务器响应将具有以下形式

    {
        "message_id": "xxxxxxxxxxxxx",
        "phone_number": "07xxx",
        "cost": 1.0,
        "status": "queued",
        "sms_count": 1
    }
  2. 手动调用 getMessageStatus() 来获取消息状态

    Yii::$app->jumbefupi->getMessageStatus($messageId)

    $messageId: 在 sms_message 表的 message_id 列中找到的消息标识符

    在服务器响应成功后,消息将在数据库中自动更新。

检查您的 JumbeFupi 账户余额

返回您的 JumbeFupi 账户余额。如果 cacheBalance 设置为 true,则余额可以缓存。如果有的话,在发送消息时会删除缓存的余额

Yii::$app->jumbefupi->checkBalance($fromCache)

$fromCache: 布尔值,用于启用获取可用的缓存余额或忽略缓存值。仅在配置中的 cacheBalance 设置为 true 时有用