agiledrop / laravel-telnyx
Laravel 的 Telnyx 短信服务通知通道。
1.0.1
2021-11-25 13:44 UTC
Requires
- php: ^7.4|^8.0
- curl/curl: ^2.3
- telnyx/telnyx-php: ^2.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- nunomaduro/larastan: ^0.6.9
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.3
- vimeo/psalm: ^3.11
This package is not auto-updated.
Last update: 2024-09-20 09:59:46 UTC
README
此包允许您从 Laravel 应用程序发送短信和 MMS 通知。
要求 Laravel 7+
先决条件
首先,您需要注册一个 Telnyx 账户,生成一个 电话号码、消息配置文件 和 API 密钥。
请注意,目前只允许使用美国电话号码发送短信,因此您需要生成一个美国号码。
安装
您可以通过 composer 安装此包
composer require agiledrop/laravel-telnyx
您应该使用以下命令发布和运行迁移
php artisan vendor:publish --provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" --tag="migrations" php artisan migrate
您应该使用以下命令发布配置文件
php artisan vendor:publish --provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" --tag="config"
这是已发布配置文件的内容
return [ /* * The API KEY. * * You can generate API keys from the Telnyx web interface. * See https://developers.telnyx.com/docs/v2/development/authentication for details */ 'api_key' => env('TELNYX_API_KEY'), /* * The phone number or a text that is shown as sender * */ 'from' => env('TELNYX_FROM'), // Can be phone number or name /* * The messaging profile id. * Also generated from the Telnyx web interface. */ 'messaging_profile_id' => env('TELNYX_MESSAGING_PROFILE_ID'), ];
然后,您需要在 .env 文件中添加以下变量和参数
TELNYX_API_KEY=
TELNYX_FROM=
TELNYX_MESSAGING_PROFILE_ID=
用法
在您的 Laravel 通知中,只需
- 指定通知通道
- 导入类并实现 toTelnyx() 方法。
发送短信通知
这是短信通知的示例。
创建通知
使用 artisan 命令生成通知
php artisan make:notification SmsNotification
这将为您在 app/Notifications/SmsNotification.php
中创建文件
然后粘贴以下代码
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use AGILEDROP\LaravelTelnyx\Messages\TelnyxSmsMessage; use Illuminate\Notifications\Notification; class SmsNotification extends Notification { use Queueable; private string $from; private string $content; /** * Create a new notification instance. * * @param string $from * @param string $content */ public function __construct(string $from, string $content) { $this->from = $from; $this->content = $content; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable): array { return ['telnyx-sms']; } /** * Get the Telnyx / SMS representation of the notification. * * @param mixed $notifiable * @return TelnyxSmsMessage */ public function toTelnyx($notifiable): TelnyxSmsMessage { return (new TelnyxSmsMessage()) ->from($this->from) ->content($this->content); } }
要使用此通知,只需导入它并在需要的地方运行它,如下所示
use App\Notifications\Alerts\SmsNotification; // ... $from = env('TELNYX_FROM'); $content = 'The text of your sms…'; $admin->notify(new SmsNotification($from, $content));
在用户模型中重写 RouteNotificationFor
此方法用于确定通知路由到何处。
/** * Override the RouteNotificationFor * * The routeNotificationFor() method exists in the Notifications\RoutesNotifications trait, * this trait is used inside the Notifications\Notifiable trait that a User model uses * by default in a fresh laravel installation, * this method is used to determine where to route the notification to. * * @param string $driver * * @return \Illuminate\Database\Eloquent\Relations\MorphMany|string */ public function routeNotificationFor(string $driver) { if (method_exists($this, $method = 'routeNotificationFor' . Str::studly($driver))) { return $this->{$method}(); } switch ($driver) { case 'database': return $this->notifications(); case 'mail': return $this->email; // set here the name of your user mail field case 'telnyx': return $this->phone; // set here the name of your user phone field } }
发送 MMS 通知(仅限美国可用)
这是 MMS 通知的示例。
使用 artisan 命令创建通知
php artisan make:notification MmsNotification
这将创建 /App/Notification/MmsNotification.php.
然后粘贴以下代码
<?php namespace App\Notifications; use Illuminate\Bus\Queueable; use AGILEDROP\LaravelTelnyx\Messages\TelnyxMmsMessage; use Illuminate\Notifications\Notification; class MmsNotification extends Notification { use Queueable; private string $content; private string $subject; private array $images; private string $from; /** * Create a new notification instance. * * @param string $content * @param string $subject * @param array $images * @param string $from */ public function __construct(string $from, string $content, string $subject, array $images) { $this->from = $from; $this->content = $content; $this->subject = $subject; $this->images = $images; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable): array { return ['telnyx-mms']; } /** * Get the Telnyx / SMS representation of the notification. * * @param mixed $notifiable * @return TelnyxMmsMessage */ public function toTelnyx($notifiable): TelnyxMmsMessage { return (new TelnyxMmsMessage()) ->from($this->from) ->content($this->content) ->subject($this->subject) ->images($this->images); } }
要使用此通知,只需导入它并在需要的地方运行它,如下所示
use App\Notifications\Alerts\MmsNotification; … $from = env('TELNYX_FROM'); $content = 'The text of your mms…'; $subject = 'The mms subject'; $photos = []; //Array with images urls $member->notify(new MmsNotification($from, $content, $subject, $photos));
测试
要测试,您需要创建一个包含 Telnyx 凭据的 .env
文件。例如。
TELNYX_API_KEY=0000000000
TELNYX_FROM=+10000000000
TELNYX_MESSAGING_PROFILE_ID=0000000-0000-0000-000000000
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
贡献
有关详细信息,请参阅 CONTRIBUTING。
安全漏洞
请查看我们的安全策略以了解如何报告安全漏洞 我们的安全策略。
鸣谢
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。