bahricanli/netgsm

使用 Netgsm API 发送 SMS

1.0.2 2023-04-04 15:07 UTC

This package is not auto-updated.

Last update: 2024-09-18 18:03:18 UTC


README

Latest Version on Packagist Software License Build Status StyleCI SensioLabsInsight Quality Score Code Coverage Total Downloads

此包简化了使用 Laravel 5.3 和 Netgsm 发送通知的过程。

内容

安装

您可以通过 composer 安装此包。

composer require bahricanli/Netgsm

接下来,将服务提供者添加到您的 config/app.php 文件中。

/*
 * Package Service Providers...
 */

NotificationChannels\Netgsm\NetgsmServiceProvider::class,

将 Netgsm 别名注册到您的应用程序中。此注册不是可选的,因为该通道本身使用此别名。

'Netgsm' => NotificationChannels\Netgsm\Netgsm::class,

设置 Netgsm 服务

将您想要的客户端、用户名、密码、发件人(发件箱名称,发件人姓名)和请求超时配置添加到您的 config/services.php 文件中。

...
    'Netgsm' => [
        'client'     => 'http', // or xml
        'http'       => [
            'endpoint' => 'https://service.jetsms.com.tr/SMS-Web/HttpSmsSend',
        ],
        'xml'        => [
            'endpoint' => 'www.biotekno.biz:8080/SMS-Web/xmlsms',
        ],
        'username'   => '',
        'password'   => '',
        'originator' => "", // Sender name.
        'timeout'    => 60,
    ],
...

使用

现在您可以在通知的 via() 方法中使用该通道。

use NotificationChannels\Netgsm\NetgsmChannel;
use NotificationChannels\Netgsm\NetgsmMessage;

class ResetPasswordWasRequested extends Notification
{
    /**
     * Get the notification's delivery channels.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function via($notifiable)
    {
        return [NetgsmChannel::class];
    }
    
    /**
     * Get the Netgsm representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return string|\NotificationChannels\Netgsm\NetgsmMessage
     */
    public function toNetgsm($notifiable) {
        return "Test notification";
        // Or
        return new ShortMessage($notifiable->phone_number, 'Test notification');
    }
}

别忘了在您的 notifiables(例如 User)中放置为 Netgsm 定制的专用方法。

class User extends Authenticatable
{
    use Notifiable;
    
    public function routeNotificationForNetgsm()
    {
        return "905123456789";
    }
}

可用方法

Netgsm 也可以直接用于发送短信。

示例

Netgsm::sendShortMessage($to, $message);
Netgsm::sendShortMessages([[
    'recipient' => $to,
    'message'   => $message,
], [
    'recipient' => $anotherTo,
    'message'   => $anotherMessage,
]]);

更多信息请参阅 Netgsm-php 文档。

可用事件

Netgsm 通知通道包含方便的事件,提供了关于 SMS 消息所需的信息。

  1. 消息已发送 (NotificationChannels\Netgsm\Events\MessageWasSent)
  2. 消息已发送 (NotificationChannels\Netgsm\Events\MessageWasSent)
  3. 发送消息 (NotificationChannels\Netgsm\Events\SendingMessage)
  4. 发送消息 (NotificationChannels\Netgsm\Events\SendingMessages)

示例

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Netgsm\Events\MessageWasSent;

class SentMessageHandler
{
    /**
     * Handle the event.
     *
     * @param  MessageWasSent  $event
     * @return void
     */
    public function handle(MessageWasSent $event)
    {
        $response = $event->response;
        $message = $event->message;
    }
}

备注

如果客户端设置为 'http',$response->groupId() 将引发 BadMethodCallException。如果客户端设置为 'xml',$response->messageReportIdentifiers() 将引发 BadMethodCallException。

请谨慎更改客户端配置。

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

测试

$ composer test

安全性

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

贡献

有关详细信息,请参阅 CONTRIBUTING

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件