From 3e61154cf6cbb4bd513aa26dfd3281a8348c6298 Mon Sep 17 00:00:00 2001 From: kevin Date: Thu, 6 Jun 2024 22:45:52 +0530 Subject: [PATCH] Added SMSGatewayHub adapter --- .../Messaging/Adapter/SMS/SmsGatewayHub.php | 91 +++++++++++++++++++ .../Adapter/SMS/SmsGatewayHubTest.php | 30 ++++++ 2 files changed, 121 insertions(+) create mode 100644 src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php create mode 100644 tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php diff --git a/src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php b/src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php new file mode 100644 index 0000000..834cd71 --- /dev/null +++ b/src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php @@ -0,0 +1,91 @@ +getTo() as $recipient) { + $recipients[] = \ltrim($recipient, '+'); + } + + $response = new Response($this->getType()); + + // Construct the query string + $queryParams = http_build_query([ + 'apiKey' => $this->apiKey, + 'senderid' => $this->senderId, + 'channel' => 'Trans', + 'DCS' => '0', + 'flashsms' => '0', + 'number' => \implode(',', $recipients), + 'text' => $message->getContent(), + 'route' => $this->route, + 'DLTTemplateId' => $this->dltTemplateId, + 'PEID' => $this->peId, + ]); + + $url = 'https://login.smsgatewayhub.com/api/mt/SendSMS?' . $queryParams; + $result = $this->request( + 'GET', + $url, + [ + 'Content-Type: application/json', + ] + ); + + // Log the URL and result for debugging + \error_log('Request URL: ' . $url); + \error_log('Response: ' . \json_encode($result)); + + if ($result['statusCode'] === 200) { + $response->setDeliveredTo(\count($message->getTo())); + foreach ($message->getTo() as $to) { + $response->addResult($to); + } + } else { + foreach ($message->getTo() as $to) { + $response->addResult($to, 'Unknown error'); + } + } + + return $response->toArray(); + } +} diff --git a/tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php b/tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php new file mode 100644 index 0000000..ee7d1c6 --- /dev/null +++ b/tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php @@ -0,0 +1,30 @@ +send($message); + + $this->assertResponse($response); + } +}