-
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 1 commit
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,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 | ||
| ) { | ||
| } | ||
|
|
||
| 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
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 = $result['response']['errorMessage']; | ||
| } elseif (isset($result['response']['message'])) { | ||
| $errorMessage = $result['response']['message']; | ||
| } | ||
| $response->addResult($recipient, $errorMessage); | ||
| } | ||
| } | ||
|
|
||
| return $response->toArray(); | ||
| } | ||
| } | ||
| 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); | ||
|
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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.