zsardarov/laravel-msg

Laravel 的 MSG.ge 集成

v1.0.0 2021-03-07 10:11 UTC

This package is auto-updated.

Last update: 2024-09-12 22:02:11 UTC


README

此包用于通过 MSG.ge API 发送短信。

安装

composer require zsardarov/laravel-msg

您可以使用以下方式发布配置

php artisan vendor:publish --provider="Zsardarov\Msg\MsgServiceProvider"

更新 .env 文件

MSG_ALTERNATIVE_PASSWORD 是可选的。

MSG_SERVICE_ID=1
MSG_CLIENT_ID=1
MSG_PASSWORD=password
MSG_USERNAME=user
MSG_ALTERNATIVE_PASSWORD=

使用

use Zsardarov\Msg\MsgService;
use Zsardarov\Msg\Enums\GatewayStatus;
use Zsardarov\Msg\Enums\DeliveryStatus;

class SampleController extends Controller
{
    public function sms(MsgService $sender)
    {        
        $result = $sender->send('9955XXXXXXXX', 'Text');
        
        if ($result->getStatusCode() === GatewayStatus::ACCEPTED) {
            // now we can check status
            $status = $sender->status($result->getMessageId());
            
            if ($status === DeliveryStatus::PENDING || $status === DeliveryStatus::SENT) {
                // message has been sent
            }
        }
    }
}

通知

要将其与 Laravel 通知一起使用,您首先必须在 User 模型中添加 toMsg() 方法

class User extends Authenticatable
{
    use Notifiable; 
    
    public function toMsg()
    {
        return $this->mobile;
    }
}

创建通知

php artisan make:notification SmsNotification
use Illuminate\Notifications\Notification;
use Zsardarov\Msg\Channels\MsgChannel;

class SmsNotification extends Notification
{
    private $message;
    
    public function __construct(string $message) 
    {
        $this->message = $message;
    }
    
    public function via($notifiable)
    {
        return [MsgChannel::class];
    }
    
    public function toSms()
    {
        return $this->message;
    }
}

然后向用户发送通知

$user->notify(new SmsNotification('Sample'));