Skip to content
Closed
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
77 changes: 77 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/SmsGatewayHub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?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';

public function __construct(
private readonly string $apiKey,
private readonly string $senderId,
private readonly string $route,
private readonly string $dltTemplateId,
private readonly string $peId,
) {
parent::__construct();
}

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

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

protected function process(SMSMessage $message): array
{
$recipients = [];
foreach ($message->getTo() as $recipient) {
$recipients[] = \ltrim($recipient, '+');
}

$response = new Response($this->getType());

$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',
]
);

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();
}
}
41 changes: 41 additions & 0 deletions tests/Messaging/Adapter/SMS/SmsGatewayHubTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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
{
$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');
$to = \getenv('SMS_GATEWAY_TO');

if (!$apiKey || !$senderId || !$route || !$dltTemplateId || !$peId || !$to) {
$this->markTestSkipped('SMSGatewayHub credentials not configured');
}

$sender = new SmsGatewayHub(
apiKey: $apiKey,
senderId: $senderId,
route: $route,
dltTemplateId: $dltTemplateId,
peId: $peId,
);

$message = new SMS(
to: [$to],
content: 'Your login OTP is 789456.',
);

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

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