You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

  1. <?php
  2. namespace App\Channels;
  3. use Illuminate\Notifications\Notification;
  4. use GuzzleHttp\Client as HttpClient;
  5. class SmsChannel
  6. {
  7. /**
  8. * The API URL for Socket.
  9. *
  10. * @var string
  11. */
  12. protected $sms_url;
  13. /**
  14. * The HTTP client instance.
  15. *
  16. * @var \GuzzleHttp\Client
  17. */
  18. protected $http;
  19. /**
  20. * Create a new Socket channel instance.
  21. *
  22. * @param \GuzzleHttp\Client $http
  23. * @return void
  24. */
  25. public function __construct(HttpClient $http, string $sms_url)
  26. {
  27. $this->http = $http;
  28. $this->sms_url = $sms_url;
  29. }
  30. /**
  31. * Send the given notification.
  32. *
  33. * @param mixed $notifiable
  34. * @param \Illuminate\Notifications\Notification $notification
  35. * @return void
  36. */
  37. public function send($notifiable, Notification $notification)
  38. {
  39. $message = $notification->toSms($notifiable);
  40. $type = $notification->getType();
  41. $message->to($notifiable->routeNotificationFor('sms', $notification));
  42. if (! $message->to) {
  43. return;
  44. }
  45. try {
  46. $this->http->post($this->sms_url . 'api/' . $type , [
  47. 'headers' => [
  48. 'x-sms-ir-secure-token'=>self::getToken()
  49. ],
  50. 'connect_timeout' => 30,
  51. 'json' => ['data' => $message->data],
  52. ]);
  53. } catch (\GuzzleHttp\Exception\ConnectException $e) {
  54. report($e);
  55. }
  56. }
  57. }