-
Notifications
You must be signed in to change notification settings - Fork 66
Added Termii messaging adapter #52
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,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; | ||
| } | ||
|
|
||
| /** | ||
| * {@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 ?? ''; | ||
|
Contributor
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. It's okay to pass an empty string for from?
Author
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. No, it will throw exception as Invalid Sender Id
Contributor
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. And what if null is passed?
Author
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. Hi, sorry for late reply. I was busy on some office work.
|
||
|
|
||
| // 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
Contributor
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. Do they not accept an array with 1 element?
Author
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. not sure about that, based on the documentation they just mentioned that to send to multiple numbers we need to pass as array
Contributor
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. It would be good to test and see so that you can clean up this code.
Author
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. 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.
|
||
|
|
||
| $body = [ | ||
| 'api_key' => $this->apiKey, | ||
| 'to' => $to, | ||
| 'from' => $from, | ||
| 'sms' => $text, | ||
| 'type' => self::SMSType, | ||
| 'channel' => self::SMSChannel, | ||
| ]; | ||
|
|
||
| return $body; | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.