AligoSmsService.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\ThirdPartyApi\Aligo\Services;
  3. use App\ThirdPartyApi\Interfaces\SmsInterface;
  4. use Carbon\Carbon;
  5. use Unirest\Request as Unirest;
  6. class AligoSmsService implements SmsInterface
  7. {
  8. public function sendMessage($title, $msg, $receiver): array
  9. {
  10. $data = [
  11. 'key' => env('SMS_APIKEY'),
  12. 'user_id' => env('SMS_USER'),
  13. 'sender' => env('SMS_SENDER'),
  14. 'title' => $title,
  15. 'msg_type' => 'sms',
  16. 'msg' => $msg,
  17. 'receiver' => $receiver,
  18. ];
  19. $response = Unirest::post(
  20. 'https://apis.aligo.in/send/',
  21. ['Accept' => 'application/json'],
  22. $data,
  23. );
  24. $body = json_decode($response->raw_body, true);
  25. if ($body['result_code'] !== '1') {
  26. return [
  27. 'Success' => false,
  28. 'Message' => $body['message'],
  29. 'Error' => [
  30. 'Code' => $body['result_code'],
  31. 'Time' => Carbon::now()->timestamp
  32. ],
  33. ];
  34. }
  35. return [
  36. 'Success' => true,
  37. 'Message' => $body['message'],
  38. 'Response' => [
  39. 'MessageId' => $body['msg_id'],
  40. 'MsgType' => $body['msg_type'],
  41. ],
  42. ];
  43. }
  44. }