helliosolutions/helliosms

Hellio Messaging 服务数组集成到 Laravel。

dev-main 2022-08-10 23:57 UTC

This package is auto-updated.

Last update: 2024-09-11 04:33:46 UTC


README

关于

官方 Laravel 包,与 Hellio Messaging 的 API 群组完美集成,支持 SMS通知语音 SMSOTP 码OTP 验证电子邮件验证服务Laravel 验证器

注册

注册 Hellio Messaging 并从您的账户获取授权密钥。登录后,您可以在“个人资料设置 > API 密钥 & Webhooks”中找到 client_idapplication_secret

安装

composer require helliosolutions/helliosms

Laravel < 5.5

安装包后,打开您的 app/config/app.php 配置文件,找到 providers 键。将以下行添加到末尾

\Hellio\HellioMessaging\HellioMessagingServiceProvider::class

接下来,找到 aliases 键,并添加以下行

'HellioMessaging' => \Hellio\HellioMessaging\Facades\HellioMessaging::class,

配置

使用以下键将凭证和首选项放入 ENV 中

HELLIO_MESSAGING_CLIENT_ID
HELLIO_MESSAGING_APPLICATION_SECRET
HELLIO_MESSAGING_DEFAULT_SENDER
HELLIO_MESSAGING_API_VERSION

如果您想自定义此内容,请发布默认配置,这将创建一个配置文件 config/helliomessaging.php

### Configuration Structure
The configuration looks like this:
<?php

return [

    /**
     * Live API url
     *
     */
    'baseUrl' => 'https://api.helliomessaging.com/',

    /**
     * Client Id
     *
     */
    'clientId' => getenv('HELLIO_MESSAGING_CLIENT_ID'),

    /**
     * Application Secret
     *
     */
    'applicationSecret' => getenv('HELLIO_MESSAGING_APPLICATION_SECRET'),


    /**
     * Default Sender Id
     *
     */
    'defaultSender' => getenv('HELLIO_MESSAGING_DEFAULT_SENDER'),


    /**
     * Default API version
     *
     */

    'apiVersion' => getenv('HELLIO_MESSAGING_API_VERSION'),

    /**
     * Default username
     *
     */
    'username' => getenv('HELLIO_MESSAGING_USERNAME'),


    /**
     * Default password
     *
     */

    'password' => getenv('HELLIO_MESSAGING_PASSWORD'),

];

发布服务提供商

$ php artisan vendor:publish --provider="Hellio\HellioMessaging\HellioMessagingServiceProvider" --tag=helliomessaging

使用方法

打开您的 .env 文件,并添加您的 API 密钥,如下所示

HELLIO_MESSAGING_CLIENT_ID=xxxxxxxx
HELLIO_MESSAGING_APPLICATION_SECRET=xxxxxxxx
HELLIO_MESSAGING_DEFAULT_SENDER=YourSenderName //Max of 11 characters
HELLIO_MESSAGING_API_VERSION= // From v1 to v3. Kindly check the documentation for the appropriate version you wish to use

基本

  • 向一个或多个手机号码发送短信。
<?php

//The first call assumes that you've already set a default sender_id in the helliomessaging.php config file.
$response = HellioMessaging::sms('233242813656', 'Hello there!');

$response = HellioMessaging::sms('233242813656', 'Hello there!', 'HellioSMS');

$response = HellioMessaging::sms(null, [
    ['mobile_number' => ['233242813656', '233591451609'], 'message' => 'Hello there!'],
    ['mobile_number' => ['233203555816'], 'message' => 'Come here!'],
], 'HellioSMS');

SMS 响应

成功

{
    "success": true,
    "message": "1 sms sent successfully"
}
  • 向手机号码发送 OTP。
<?php

$response = HellioMessaging::otp('233242813656');

$response = HellioMessaging::otp('233242813656', 'HellioSMS', '4', '10');

$response = HellioMessaging::otp('233242813656', 'HellioSMS', '4', '10', '##OTP## is your OTP, Please dont share it with anyone.');
  • 验证发送到手机号码的 OTP。
<?php

$response = HellioMessaging::verify('233242813656', 1290); // returns true or false

OTP 响应

成功

{
    "success": true,
    "data": {
        "status": true,
        "token": "528830",
        "message": "OTP generated"
    },
    "message": "2FA code sent successfully"
}

不存在

{
    "success": true,
    "data": {
        "status": false,
        "message": "OTP does not exist"
    },
}

无效*

{
    "success": true,
    "data": {
        "status": false,
        "message": "OTP is not valid"
    },
}

已过期

{
    "success": true,
    "data": {
        "status": false,
        "message": "OTP Expired"
    },
}

电子邮件验证

  • 验证电子邮件地址是否正确且可以接收电子邮件。
<?php

$response = HellioMessaging::emailvalidator('someemail@domain.com', 'Marketing leads');

$response = HellioMessaging::emailvalidator(['someemail@domain.com', 'support@domain.com'], 'Marketing leads'); // Validate multiple emails at once

号码查询服务

  • 在发送您的群发短信活动之前检查手机号码的状态。这确保您正在向活跃的号码发送消息,并且收件人实际上可以接收您的消息。
<?php

$response = HellioMessaging::numberLookup('233242813656');

$response = HellioMessaging::numberLookup(['233242813656', '233591451609']); // Validate multiple mobile numbers at once

Hellio 账户余额

  • 轻松检查您的 Hellio Messaging 账户余额。
<?php

$response = HellioMessaging::balance();

通知

在通知的通道中包含 helliomessaging

<?php

/**
 * @param  mixed  $notifiable
 * @return array
 */
public function via($notifiable)
{
    return ['helliomessaging'];
}

定义 toHellioMessaging 方法

<?php

use Hellio\HellioMessaging\Message\HellioMessagingSms;

public function toHellioMessaging()
{
    return (new HellioMessagingSms)
        ->message(__('This is just a test message.'))
	    ->sender_id(__('HellioSMS')) // [Optional] - Will pick default sender ID from HELLIO_MESSAGING_DEFAULT_SENDER or if not set, will use the application name.
        ->to('233242813656');
}

在您的可通知类中定义默认的 routeNotificationForHellioMessaging 方法

<?php

public function routeNotificationForHellioMessaging($notification)
{
    return $this->mobile_number;
}

最后,发送通知

<?php

$notifiable = /* some class */
$notifiable->notify(new App\Notifications\HellioMessagingTestNotification());

对于将通知发送到任意手机号码,请使用以下语法

<?php
use Illuminate\Support\Facades\Notification

Notification::route('helliomessaging', '233242813656')
    ->notify(new App\Notifications\HellioMessagingTestNotification());

OTP 验证器

您可以使用提供的名为 hellio_otp 的验证规则来验证发送的 OTP,如下所示

<?php

use Illuminate\Support\Facades\Validator

$data = ['mobile_number' => '233242813656', 'token' => '1234'];

$validator = Validator::make($data, [
    'mobile_number' => ['required', 'digits:10'],
    'token' => ['required', 'digits:4', 'hellio_otp'], // default key for source number is 'mobile_number', you can customize this using 'hellio_otp:key_name'
]);

if ($validator->fails()) {
    // report errors
}

测试

$ composer test

安全

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

贡献

有关详细信息,请参阅 CONTRIBUTING

鸣谢

许可

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