-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add AfricasTalking SMS adapter #132
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 100; | ||
| } | ||
|
Comment on lines
+28
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Africa's Talking API supports multiple recipients natively — the Prompt To Fix With AIThis 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. |
||
|
|
||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Africa's Talking returns per-recipient statuses in ArtifactsRepro: focused PHP harness mocking a 201 failed-recipient Africa's Talking response
Repro: harness execution output showing HTTP 201 Created and adapter success/deliveredTo behavior
|
||
| } 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(); | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.