From a53f95db7b5da2930780f9650bf9a011585585de Mon Sep 17 00:00:00 2001 From: Biswajit Mondal Date: Thu, 12 Oct 2023 00:10:50 +0530 Subject: [PATCH 1/2] Added Termii messaging adapter --- .github/workflows/tests.yml | 3 + docker-compose.yml | 3 + src/Utopia/Messaging/Adapters/SMS/Termii.php | 88 ++++++++++++++++++++ tests/e2e/SMS/TermiiTest.php | 35 ++++++++ 4 files changed, 129 insertions(+) create mode 100644 src/Utopia/Messaging/Adapters/SMS/Termii.php create mode 100644 tests/e2e/SMS/TermiiTest.php diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 552b207d..308c19d7 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -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 diff --git a/docker-compose.yml b/docker-compose.yml index 80d626d9..c0dd7200 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,6 +29,9 @@ services: - VONAGE_API_SECRET - VONAGE_TO - VONAGE_FROM + - TERMII_API_KEY + - TERMII_TO + - TERMII_FROM build: context: . volumes: diff --git a/src/Utopia/Messaging/Adapters/SMS/Termii.php b/src/Utopia/Messaging/Adapters/SMS/Termii.php new file mode 100644 index 00000000..6dabf8d2 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/SMS/Termii.php @@ -0,0 +1,88 @@ +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 + { + if (empty($from)) { + $from = ''; + } + + // removing + from numbers if exists + $to = \array_map( + fn ($to) => \ltrim($to, '+'), + $to + ); + + if (count($to) == 1) { + $to = $to[0]; + } + + $body = [ + 'api_key' => $this->apiKey, + 'to' => $to, + 'from' => $from, + 'sms' => $text, + 'type' => self::SMSType, + 'channel' => self::SMSChannel, + ]; + + return $body; + } +} diff --git a/tests/e2e/SMS/TermiiTest.php b/tests/e2e/SMS/TermiiTest.php new file mode 100644 index 00000000..37d528ae --- /dev/null +++ b/tests/e2e/SMS/TermiiTest.php @@ -0,0 +1,35 @@ +send($message); + $result = \json_decode($response, true); + + $this->assertArrayNotHasKey('errors', $result); + $this->assertArrayHasKey('message_id', $result); + $this->assertArrayHasKey('message', $result); + $this->assertArrayHasKey('balance', $result); + } +} From 838f80d45e45d754a5b36fce17c827b493aa1ab9 Mon Sep 17 00:00:00 2001 From: Biswajit Mondal Date: Thu, 26 Oct 2023 16:59:25 +0530 Subject: [PATCH 2/2] added null coalescing operator for checking null value --- src/Utopia/Messaging/Adapters/SMS/Termii.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Utopia/Messaging/Adapters/SMS/Termii.php b/src/Utopia/Messaging/Adapters/SMS/Termii.php index 6dabf8d2..b66159ee 100644 --- a/src/Utopia/Messaging/Adapters/SMS/Termii.php +++ b/src/Utopia/Messaging/Adapters/SMS/Termii.php @@ -60,9 +60,7 @@ protected function process(SMS $message): string */ private function getRequestBody(array $to, string $text, string $from = null): array { - if (empty($from)) { - $from = ''; - } + $from = $from ?? ''; // removing + from numbers if exists $to = \array_map(