Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 75 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/AfricasTalking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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 AfricasTalking extends SMSAdapter
{
protected const NAME = 'AfricasTalking';

private const API_URL = 'https://api.africasTalking.com/sms/1/action';

public function __construct(
private string $username,
private string $apiKey,
private ?string $from = null
) {
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.

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

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

protected function process(SMSMessage $message): array
{
$response = new Response($this->getType());

$recipients = \array_map(
fn ($to) => \ltrim($to, '+'),
$message->getTo()
);

$from = $this->from ?? $message->getFrom();

foreach ($recipients as $recipient) {
$result = $this->request(
method: 'POST',
url: self::API_URL,
headers: [
'Content-Type: application/x-www-form-urlencoded',
'apiKey: ' . $this->apiKey,
],
body: [
'username' => $this->username,
'to' => $recipient,
'message' => $message->getContent(),
'from' => $from,
]
);

if ($result['statusCode'] === 201) {
$response->incrementDeliveredTo();
$response->addResult($recipient);
Comment on lines +60 to +62

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Check recipient status

Africa's Talking returns per-recipient statuses in SMSMessageData.Recipients, including failures such as invalid numbers or insufficient balance, while the HTTP request itself can still succeed. This branch marks every 201 response as delivered without reading those recipient statuses, so callers can receive success and an incremented deliveredTo count for messages the provider rejected.

Artifacts

Repro: focused PHP harness mocking a 201 failed-recipient Africa's Talking response

  • Contains supporting evidence from the run (text/x-php; charset=utf-8).

Repro: harness execution output showing HTTP 201 Created and adapter success/deliveredTo behavior

  • Keeps the command output available without making the summary code-heavy.

View artifacts

T-Rex Ran code and verified through T-Rex

} else {
$errorMessage = 'Unknown error';
if (isset($result['response']['errorMessage'])) {
$errorMessage = $result['response']['errorMessage'];
} elseif (isset($result['response']['message'])) {
$errorMessage = $result['response']['message'];
}
$response->addResult($recipient, $errorMessage);
}
}

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

namespace Utopia\Tests\Adapter\SMS;

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

class AfricasTalkingTest extends Base
{
public function testSendSMS(): void
{
$username = \getenv('AFRICAS_TALKING_USERNAME');
$apiKey = \getenv('AFRICAS_TALKING_API_KEY');
$to = \getenv('AFRICAS_TALKING_TO');

if (empty($username) || empty($apiKey) || empty($to)) {
$this->markTestSkipped('AfricasTalking credentials are not available.');
}

$sender = new AfricasTalking($username, $apiKey);

$message = new SMS(
to: [$to],
content: 'Test Content',
from: \getenv('AFRICAS_TALKING_FROM') ?: null
);

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

$result = \json_decode($response, true);

$this->assertResponse($result);
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Outdated
}

public function testSendSMSWithFrom(): void
{
$username = \getenv('AFRICAS_TALKING_USERNAME');
$apiKey = \getenv('AFRICAS_TALKING_API_KEY');
$to = \getenv('AFRICAS_TALKING_TO');
$from = \getenv('AFRICAS_TALKING_FROM');

if (empty($username) || empty($apiKey) || empty($to)) {
$this->markTestSkipped('AfricasTalking credentials are not available.');
}

$sender = new AfricasTalking($username, $apiKey, $from ?: 'AFRICAST');

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

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

$result = \json_decode($response, true);

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