Skip to content
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
77 changes: 77 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/AfricasTalking.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 AfricasTalking extends SMSAdapter
{
protected const NAME = 'AfricasTalking';

private const API_URL = 'https://api.africasTalking.com/version1/messaging';

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

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

public function getMaxMessagesPerRequest(): int
{
return 100;
}
Comment on lines +28 to +31

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 getMaxMessagesPerRequest claims 100 but makes one HTTP call per recipient

getMaxMessagesPerRequest() returning 100 tells the base Adapter::send() to allow up to 100 recipients in a single call, but process() loops over each one and fires a separate $this->request() for every number. A caller passing 100 recipients will trigger 100 sequential HTTP round-trips rather than the single batched request the value implies, and can easily hit Africa's Talking rate limits or exhaust the 30-second socket timeout.

The Africa's Talking API supports multiple recipients natively — the to parameter accepts a comma-separated list in one POST. The adapter should either join all recipients into a single request body (and process the per-recipient statuses from SMSMessageData.Recipients) or, as a minimal safe fix, change getMaxMessagesPerRequest() to 1 to match the actual one-request-per-recipient behavior, consistent with how Twilio and Vonage adapters are implemented.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/Utopia/Messaging/Adapter/SMS/AfricasTalking.php
Line: 28-31

Comment:
**`getMaxMessagesPerRequest` claims 100 but makes one HTTP call per recipient**

`getMaxMessagesPerRequest()` returning `100` tells the base `Adapter::send()` to allow up to 100 recipients in a single call, but `process()` loops over each one and fires a separate `$this->request()` for every number. A caller passing 100 recipients will trigger 100 sequential HTTP round-trips rather than the single batched request the value implies, and can easily hit Africa's Talking rate limits or exhaust the 30-second socket timeout.

The Africa's Talking API supports multiple recipients natively — the `to` parameter accepts a comma-separated list in one POST. The adapter should either join all recipients into a single request body (and process the per-recipient statuses from `SMSMessageData.Recipients`) or, as a minimal safe fix, change `getMaxMessagesPerRequest()` to `1` to match the actual one-request-per-recipient behavior, consistent with how Twilio and Vonage adapters are implemented.

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

Fix in Claude Code Fix in Codex


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

$recipients = $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 +57 to +59

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 = \is_string($result['response']['errorMessage'])
? $result['response']['errorMessage']
: \json_encode($result['response']['errorMessage']);
} elseif (isset($result['response']['message'])) {
$errorMessage = \is_string($result['response']['message'])
? $result['response']['message']
: \json_encode($result['response']['message']);
}
$response->addResult($recipient, $errorMessage);
}
}

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

$this->assertResponse($response);
}

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);

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