bahricanli/corvass

使用 Corvass API 发送短信

1.1.0 2022-07-26 12:56 UTC

This package is not auto-updated.

Last update: 2024-09-17 23:12:23 UTC


README

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

本包简化了使用 Laravel 5.3 通过 Corvass 发送通知的过程。

内容

安装

您可以通过 composer 安装此包

composer require bahricanli/corvass

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

/*
 * Package Service Providers...
 */

NotificationChannels\Corvass\CorvassServiceProvider::class,

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

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

配置 Corvass 服务

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

...
    'Corvass' => [
        '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\Corvass\CorvassChannel;
use NotificationChannels\Corvass\CorvassMessage;

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

不要忘记在您的 notifiables 中放置针对 Corvass 的专用方法。(例如 User)

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

可用方法

Corvass 还可以直接用来发送短消息。

示例

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

请参阅 corvass-php 文档以获取更多信息。

可用事件

Corvass 通知通道提供便捷的事件,提供了有关短信消息所需的信息。

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

示例

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use NotificationChannels\Corvass\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)。有关更多信息,请参阅 许可文件