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

Added Termii messaging adapter #52

Open
wants to merge 2 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
3 changes: 3 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ jobs:
VONAGE_API_SECRET: ${{ secrets.VONAGE_API_SECRET }}
VONAGE_TO: ${{ secrets.VONAGE_TO }}
VONAGE_FROM: ${{ secrets.VONAGE_FROM }}
TERMII_API_KEY: ${{ secrets.TERMII_API_KEY }}
TERMII_TO: ${{ secrets.TERMII_TO }}
TERMII_FROM: ${{ secrets.TERMII_FROM }}
run: |
docker compose up -d --build
sleep 5
Expand Down
3 changes: 3 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ services:
- VONAGE_API_SECRET
- VONAGE_TO
- VONAGE_FROM
- TERMII_API_KEY
- TERMII_TO
- TERMII_FROM
build:
context: .
volumes:
Expand Down
86 changes: 86 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/Termii.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Utopia\Messaging\Adapters\SMS;

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

// Reference Material
// https://developers.termii.com/messaging-api
class Termii extends SMSAdapter
{
const SMSType = 'plain';

const SMSChannel = 'generic';

public function __construct(
private string $apiKey
) {
}

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

public function getMaxMessagesPerRequest(): int
{
return 100;
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
return $this->request(
method: 'POST',
url: 'https://api.ng.termii.com/api/sms/send',
headers: [
'Content-Type: application/json',
],
body: \json_encode(
$this->getRequestBody(
to: $message->getTo(),
text: $message->getContent(),
from: $message->getFrom()
)
),
);
}

/**
* Get the request body
*
* @param array $to Phone number
* @param string $text Message to send
* @param string|null $from Origin of the message
*/
private function getRequestBody(array $to, string $text, string $from = null): array
{
$from = $from ?? '';
Copy link
Contributor

Choose a reason for hiding this comment

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

It's okay to pass an empty string for from?

Copy link
Author

Choose a reason for hiding this comment

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

No, it will throw exception as Invalid Sender Id

Copy link
Contributor

Choose a reason for hiding this comment

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

And what if null is passed?

Copy link
Author

@biswajit287 biswajit287 Nov 16, 2023

Choose a reason for hiding this comment

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

Hi, sorry for late reply. I was busy on some office work.
If null/empty from passed then also it will throw exception

Exception: {"message":"The given data was invalid.","errors":{"from":["The from field is required."]}}


// removing + from numbers if exists
$to = \array_map(
fn ($to) => \ltrim($to, '+'),
$to
);

if (count($to) == 1) {
$to = $to[0];
}
Comment on lines +71 to +73
Copy link
Contributor

Choose a reason for hiding this comment

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

Do they not accept an array with 1 element?

Copy link
Author

Choose a reason for hiding this comment

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

not sure about that, based on the documentation they just mentioned that to send to multiple numbers we need to pass as array

Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to test and see so that you can clean up this code.

Copy link
Author

Choose a reason for hiding this comment

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

Hi, I am unable to test the service anymore as my account balance is low. Termii does not provide free testing and since I am testing from India cost is high.

Exception: {"message":"Insufficient balance"}


$body = [
'api_key' => $this->apiKey,
'to' => $to,
'from' => $from,
'sms' => $text,
'type' => self::SMSType,
'channel' => self::SMSChannel,
];

return $body;
}
}
35 changes: 35 additions & 0 deletions tests/e2e/SMS/TermiiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Tests\E2E;

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

class TermiiTest extends Base
{
/**
* @throws \Exception
*/
public function testSendSMS()
{
$apiKey = getenv('TERMII_API_KEY');

$to = [getenv('TERMII_TO')];
$from = getenv('TERMII_FROM');

$sender = new Termii($apiKey);
$message = new SMS(
to: $to,
content: 'Test Content',
from: $from
);

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

$this->assertArrayNotHasKey('errors', $result);
$this->assertArrayHasKey('message_id', $result);
$this->assertArrayHasKey('message', $result);
$this->assertArrayHasKey('balance', $result);
}
}
Loading