项目作者: AGILEDROP

项目描述 :
Laravel integration for Telnyx SMS service
高级语言: PHP
项目地址: git://github.com/AGILEDROP/laravel-telnyx.git
创建时间: 2020-10-29T10:44:11Z
项目社区:https://github.com/AGILEDROP/laravel-telnyx

开源协议:MIT License

下载


Laravel Telnyx Driver

Latest Version on Packagist
GitHub Tests Action Status
Total Downloads

This package enables to send SMS and MMS notifications from your Laravel application.

Requires Laravel 7+

Prerequisites

You first need to register a Telnyx account, generate a phone number a messaging profile,
and an API key.

Notice that at the moment just American phone numbers are allowed to send SMS, so you will need to generate an American number.

Installation

You can install the package via composer:

  1. composer require agiledrop/laravel-telnyx

You should publish and run the migrations with:

  1. php artisan vendor:publish --provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" --tag="migrations"
  2. php artisan migrate

You should publish the config file with:

  1. php artisan vendor:publish --provider="AGILEDROP\LaravelTelnyx\LaravelTelnyxServiceProvider" --tag="config"

This is the contents of the published config file:

  1. return [
  2. /*
  3. * The API KEY.
  4. *
  5. * You can generate API keys from the Telnyx web interface.
  6. * See https://developers.telnyx.com/docs/v2/development/authentication for details
  7. */
  8. 'api_key' => env('TELNYX_API_KEY'),
  9. /*
  10. * The phone number or a text that is shown as sender
  11. *
  12. */
  13. 'from' => env('TELNYX_FROM'), // Can be phone number or name
  14. /*
  15. * The messaging profile id.
  16. * Also generated from the Telnyx web interface.
  17. */
  18. 'messaging_profile_id' => env('TELNYX_MESSAGING_PROFILE_ID'),
  19. ];

You should then add you your .env file the following variables with your parameters:

  1. TELNYX_API_KEY=
  2. TELNYX_FROM=
  3. TELNYX_MESSAGING_PROFILE_ID=

Usage

In your Laravel Notification you just need to

  • Specify the notification channel
  • import the class and implement the toTelnyx() method.

Sending an SMS notification

This is an example of SMS notification.

Create a notification

Generate a notification with an artisan command:

  1. php artisan make:notification SmsNotification

This will create for you the file at app/Notifications/SmsNotification.php

Then paste in that the following code:

  1. <?php
  2. namespace App\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use AGILEDROP\LaravelTelnyx\Messages\TelnyxSmsMessage;
  5. use Illuminate\Notifications\Notification;
  6. class SmsNotification extends Notification
  7. {
  8. use Queueable;
  9. private string $from;
  10. private string $content;
  11. /**
  12. * Create a new notification instance.
  13. *
  14. * @param string $from
  15. * @param string $content
  16. */
  17. public function __construct(string $from, string $content)
  18. {
  19. $this->from = $from;
  20. $this->content = $content;
  21. }
  22. /**
  23. * Get the notification's delivery channels.
  24. *
  25. * @param mixed $notifiable
  26. * @return array
  27. */
  28. public function via($notifiable): array
  29. {
  30. return ['telnyx-sms'];
  31. }
  32. /**
  33. * Get the Telnyx / SMS representation of the notification.
  34. *
  35. * @param mixed $notifiable
  36. * @return TelnyxSmsMessage
  37. */
  38. public function toTelnyx($notifiable): TelnyxSmsMessage
  39. {
  40. return (new TelnyxSmsMessage())
  41. ->from($this->from)
  42. ->content($this->content);
  43. }
  44. }

Then to use this notification, just import it in where you need it and run it like below:

  1. use App\Notifications\Alerts\SmsNotification;
  2. // ...
  3. $from = env('TELNYX_FROM');
  4. $content = 'The text of your sms…';
  5. $admin->notify(new SmsNotification($from, $content));

Override the RouteNotificationFor in your user model

This metod is used to determine where to route the notification to.

  1. /**
  2. * Override the RouteNotificationFor
  3. *
  4. * The routeNotificationFor() method exists in the Notifications\RoutesNotifications trait,
  5. * this trait is used inside the Notifications\Notifiable trait that a User model uses
  6. * by default in a fresh laravel installation,
  7. * this method is used to determine where to route the notification to.
  8. *
  9. * @param string $driver
  10. *
  11. * @return \Illuminate\Database\Eloquent\Relations\MorphMany|string
  12. */
  13. public function routeNotificationFor(string $driver)
  14. {
  15. if (method_exists($this, $method = 'routeNotificationFor' . Str::studly($driver))) {
  16. return $this->{$method}();
  17. }
  18. switch ($driver) {
  19. case 'database':
  20. return $this->notifications();
  21. case 'mail':
  22. return $this->email; // set here the name of your user mail field
  23. case 'telnyx':
  24. return $this->phone; // set here the name of your user phone field
  25. }
  26. }

Sending an MMS notification (available just for US)

This is an example of MMS notification.

Create a notification with an artisan command:

  1. php artisan make:notification MmsNotification

This will create: /App/Notification/MmsNotification.php.

Then paste in that the following code:

  1. <?php
  2. namespace App\Notifications;
  3. use Illuminate\Bus\Queueable;
  4. use AGILEDROP\LaravelTelnyx\Messages\TelnyxMmsMessage;
  5. use Illuminate\Notifications\Notification;
  6. class MmsNotification extends Notification
  7. {
  8. use Queueable;
  9. private string $content;
  10. private string $subject;
  11. private array $images;
  12. private string $from;
  13. /**
  14. * Create a new notification instance.
  15. *
  16. * @param string $content
  17. * @param string $subject
  18. * @param array $images
  19. * @param string $from
  20. */
  21. public function __construct(string $from, string $content, string $subject, array $images)
  22. {
  23. $this->from = $from;
  24. $this->content = $content;
  25. $this->subject = $subject;
  26. $this->images = $images;
  27. }
  28. /**
  29. * Get the notification's delivery channels.
  30. *
  31. * @param mixed $notifiable
  32. * @return array
  33. */
  34. public function via($notifiable): array
  35. {
  36. return ['telnyx-mms'];
  37. }
  38. /**
  39. * Get the Telnyx / SMS representation of the notification.
  40. *
  41. * @param mixed $notifiable
  42. * @return TelnyxMmsMessage
  43. */
  44. public function toTelnyx($notifiable): TelnyxMmsMessage
  45. {
  46. return (new TelnyxMmsMessage())
  47. ->from($this->from)
  48. ->content($this->content)
  49. ->subject($this->subject)
  50. ->images($this->images);
  51. }
  52. }

Then to use this notification, just import it where you need and run it like below:

  1. use App\Notifications\Alerts\MmsNotification;
  2. $from = env('TELNYX_FROM');
  3. $content = 'The text of your mms…';
  4. $subject = 'The mms subject';
  5. $photos = []; //Array with images urls
  6. $member->notify(new MmsNotification($from, $content, $subject, $photos));

Testing

To test you need to create a .env file with the Telnyx credentials.
eg.

  1. TELNYX_API_KEY=0000000000
  2. TELNYX_FROM=+10000000000
  3. TELNYX_MESSAGING_PROFILE_ID=0000000-0000-0000-000000000
  1. composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.