Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added SMSGatewayHub adapter #84

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace Utopia\Messaging\Adapter\SMS;

use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS as SMSMessage;
use Utopia\Messaging\Response;

class SMSGatewayHub extends SMSAdapter
{
protected const NAME = 'SMSGatewayHub';

/**
* @param string $apiKey SMS Gateway Hub API Key
* @param string $senderId SMS Gateway Hub Sender ID
* @param string $route SMS Gateway Hub Route
* @param string $dltTemplateId SMS Gateway Hub DLT Template ID
* @param string $peId SMS Gateway Hub PEID
*/
public function __construct(
private string $apiKey,
private string $senderId,
private string $route,
private string $dltTemplateId,
private string $peId,
) {
}

public function getName(): string
{
return static::NAME;
}

public function getMaxMessagesPerRequest(): int
{
return 100;
}

/**
* {@inheritdoc}
*/
protected function process(SMSMessage $message): array
{
$recipients = [];
foreach ($message->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();
}
}
30 changes: 30 additions & 0 deletions tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Utopia\Tests\Adapter\SMS;

use Utopia\Messaging\Adapter\SMS\SMSGatewayHub;
use Utopia\Messaging\Messages\SMS;
use Utopia\Tests\Adapter\Base;

class SMSGatewayHubTest extends Base
{
public function testSendSMS(): void
{
$sender = new SMSGatewayHub(
apiKey: getenv('SMS_GATEWAY_APIKEY'),
senderId: getenv('SMS_GATEWAY_SENDER_ID'),
route: getenv('SMS_GATEWAY_ROUTE'),
dltTemplateId: getenv('SMS_GATEWAY_DLT_TEMPLATE_ID'),
peId: getenv('SMS_GATEWAY_PEID')
);

$message = new SMS(
to: [917358240825],
content: 'Your login OTP is 789456. This OTP is Valid for 5 Minutes. Never share your OTP with anyone.',
);

$response = $sender->send($message);

$this->assertResponse($response);
}
}