Skip to content
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

Feat 6378 AfricasTalking Messaging Adapter #37

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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
57 changes: 57 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/AfricasTalking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Utopia\Messaging\Adapters\SMS;

use Utopia\Messaging\Adapters\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS;

//Reference: https://developers.africastalking.com/docs/authentication
//https://developers.africastalking.com/docs/request_headers
//https://developers.africastalking.com/docs/sms/sending/bulk

class AfricasTalking extends SMSAdapter
{
/**
* @param string $username AfricasTalking app username
* @param string $apiKey AfricasTalking API Key
*/
public function __construct(
private string $username,
private string $apiKey
) {
}

public function getName(): string
{
return 'AfricasTalking';
}

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

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
return $this->request(
method: 'POST',
url: "https://api.sandbox.africastalking.com/version1/messaging",
// live endpoint: url: "https://api.africastalking.com/version1/messaging ",
headers: [
'apiKey: '.$this->apiKey,
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/json',
],
body: \http_build_query([
'username' => $this->username,
'to' => $message->getTo()[0],
'message' => $message->getContent(),
]),
);
}
}
34 changes: 34 additions & 0 deletions tests/e2e/SMS/AfricasTalkingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\E2E;

use Utopia\Messaging\Adapters\SMS\AfricasTalking;
use Utopia\Messaging\Messages\SMS;

class AfricasTalkingTest extends Base
{
/**
* @throws \Exception
*/
public function testSendSMS()
{
// username is sandbox for sandbox env
$username = getenv('AFRICASTALKING_USERNAME');
$apiKey = getenv('AFRICASTALKING_API_KEY');

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

$message = new SMS(
to: [getenv('SMS_TO')],
// to must be a comma separated string of recipients' phone numbers.
content: 'Test Content',
// from: getenv('SMS_FROM'), optional - https://developers.africastalking.com/docs/sms/sending/bulk
// defaults to AFRICASTKNG.
);

$response = $sender->send($message);
$result = \json_decode($response, true);

$this->assertNotEmpty($result);
}
}
Loading