karlos3098 / simply-connect-laravel-notifications
它允许您通过Laravel通知从simply-connect.ovh平台发送短信
Requires
- php: ^8.2|^8.3
Requires (Dev)
- laravel/pint: ^1.14
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^2.34
- pestphp/pest-plugin-arch: ^2.7
- pestphp/pest-plugin-laravel: ^2.3
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
README
Simply Connect Laravel Notifications
一个专注于Laravel 10/11的库,用于引入与Simply Connect的集成,用于Laravel内置的通知类
在介绍的话中。Simply Connect应用也是我的项目,旨在从您的设备发送短信。您只需在该平台上创建一个账户,找到抽屉里的旧安卓手机,安装.apk文件,就完成了。详细信息可以在门户网站上找到
安装
composer require karlos3098/simply-connect-laravel-notifications
php artisan vendor:publish --provider="Karlos3098\SimplyConnectLaravelNotifications\SimplyConnectServiceProvider" --tag=config
然后,将simply-connect.ovh平台的API密钥和发送消息的设备的ID添加到.env文件中
SIMPLY_CONNECT_API_KEY=API-KEY-HERE
SIMPLY_CONNECT_DEFAULT_DEVICE_ID=DEVICE-ID-HERE
如果您想发送短信消息,像发送电子邮件一样创建一个新的通知类,但额外声明toSimplyConnect方法。还建议使用SimplyConnectNotification接口
这样,消息将被发送到在给定模型中“phone_number”字段中应出现的号码。
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use Illuminate\Notifications\Messages\MailMessage; use Illuminate\Notifications\Notification; use Karlos3098\SimplyConnectLaravelNotifications\Interfaces\SimplyConnectNotification; use Karlos3098\SimplyConnectLaravelNotifications\Services\SimplyConnectMessage; class TestNotification extends Notification implements SimplyConnectNotification { use Queueable; /** * Create a new notification instance. */ public function __construct() { } /** * Get the notification's delivery channels. * * @return array<int, string> */ public function via(object $notifiable): array { return ['simply-connect', 'mail']; } /** * Get the mail representation of the notification. */ public function toMail(object $notifiable): MailMessage { return (new MailMessage)->line('Example Email'); } /** * Get the array representation of the notification. * * @return array<string, mixed> */ public function toArray(object $notifiable): array { return [ ]; } public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->text("Example SMS message"); } }
如果您需要更通用的方法来实现电话号码,请在您的模型中使用HasDifferentPhoneNumberForSimplyConnect扩展,然后添加routeNotificationForSimplyConnect方法。
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; use Illuminate\Notifications\Notification; use Karlos3098\SimplyConnectLaravelNotifications\Interfaces\HasDifferentPhoneNumberForSimplyConnect; class User extends Authenticatable implements HasDifferentPhoneNumberForSimplyConnect { use HasFactory, Notifiable; public function routeNotificationForSimplyConnect(Notification $notification): array|string|null { return $this->phone_number; //or whatever you want } /** * The attributes that are mass assignable. * * @var array<int, string> */ protected $fillable = [ 'name', 'email', 'password', ]; /** * The attributes that should be hidden for serialization. * * @var array<int, string> */ protected $hidden = [ 'password', 'remember_token', ]; /** * Get the attributes that should be cast. * * @return array<string, string> */ protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } }
如果您需要,也可以直接在通知中输入电话号码。
public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->phoneNumber("+48123456789") ->text("Example SMS message"); }
如果您想添加多个电话号码,这也是可能的。
public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->phoneNumber("+48123456789", "+48222222222", /* ... */) ->text("Example SMS message"); }
为了方便,您还可以使用line()和breakLine()方法使您的短信更易于阅读。
public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->phoneNumber("+48123456789") ->line("Line 1") ->line("Line 2") ->breakLine() ->line("Line by blank line"); }
您发送的消息将被添加到队列中,但您可以下载其ID,根据该ID您稍后可以检查其状态。
public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->callback(function(int $messageId) { //... }) ->text("Example SMS message"); }
控制器中的示例应用程序。有关消息对象中内容的详细信息,请参阅Simply Connect API文档
<?php namespace App\Http\Controllers; use Illuminate\Http\JsonResponse; use Karlos3098\SimplyConnectLaravelNotifications\Facades\MessageDetails; class MessageController extends Controller { public function show(int $messageId): JsonResponse { $message = MessageDetails::getMessageById($messageId); return response()->json($message); } }
您可以直接在通知中使用不同的设备ID和不同的API密钥。如果您使用不同的API密钥,请记住,如果您以后需要检索消息详情,您必须使用相应的密钥。
public function toSimplyConnect(object $notifiable): SimplyConnectMessage { return (new SimplyConnectMessage) ->device(123123) ->token("other Bearer API token") ->text("Example SMS message"); }
$message = MessageDetails::setBearerToken("other Bearer API token")->getMessageById($messageId);
也可能发生电话号码、设备ID或您提供的其他信息不正确,表单数据将被拒绝。在这种情况下,将抛出与以下示例类似的异常。$array包含表单错误列表。
try { \App\Models\User::first()->notify(new \App\Notifications\TestNotification()); } catch (\Karlos3098\SimplyConnectLaravelNotifications\Exceptions\CouldNotSendNotification $e) { $message = $e->getMessage(); $array = $e->getErrors(); //... }