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
74 changes: 74 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/Taqnyat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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 Taqnyat extends SMSAdapter
{
protected const NAME = 'Taqnyat';

/**
* @param string $apiKey Taqnyat API Key
* @param string $senderId Taqnyat Sender ID
*/
public function __construct(
private readonly string $apiKey,
private readonly string $senderId,
) {
parent::__construct();
}

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

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

protected function process(SMSMessage $message): array
{
$to = \array_map(
fn($to) => \ltrim($to, '+'),
$message->getTo()
);

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

$result = $this->request(
method: 'POST',
url: 'https://api.taqnyat.sa/v1/messages',
headers: [
'Content-Type: application/json',
'Authorization: Bearer ' . $this->apiKey,
],
body: [
'sender' => $this->senderId,
'recipients' => $to,
'body' => $message->getContent(),
],
);

if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addResult($to);
}
} else {
foreach ($message->getTo() as $to) {
if (!\is_null($result['response']['message'] ?? null)) {
$response->addResult($to, $result['response']['message']);
} else {
$response->addResult($to, 'Unknown error');
}
}
}

return $response->toArray();
}
}
35 changes: 35 additions & 0 deletions tests/Messaging/Adapter/SMS/TaqnyatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Utopia\Tests\Adapter\SMS;

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

class TaqnyatTest extends Base
{
public function testSendSMS(): void
{
$apiKey = \getenv('TAQNYAT_API_KEY');
$senderId = \getenv('TAQNYAT_SENDER_ID');
$to = \getenv('TAQNYAT_TO');

if (!$apiKey || !$senderId || !$to) {
$this->markTestSkipped('TAQNYAT credentials not configured');
Comment on lines +16 to +18

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Live credentials leave adapter untested

The sole Taqnyat test skips when live credentials are unavailable, so normal CI never exercises recipient normalization, JSON payload construction, or success and error response handling. Add deterministic client-backed tests for these paths so adapter regressions are detected without contacting the provider.

Prompt To Fix With AI
This is a comment left during a code review.
Path: tests/Messaging/Adapter/SMS/TaqnyatTest.php
Line: 16-18

Comment:
**Live credentials leave adapter untested**

The sole Taqnyat test skips when live credentials are unavailable, so normal CI never exercises recipient normalization, JSON payload construction, or success and error response handling. Add deterministic client-backed tests for these paths so adapter regressions are detected without contacting the provider.

How can I resolve this? If you propose a fix, please make it concise.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Claude Code Fix in Codex

}

$sender = new Taqnyat(
apiKey: $apiKey,
senderId: $senderId
);

$message = new SMS(
to: [$to],
content: 'Test Content',
);

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

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