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..b66159ee --- /dev/null +++ b/src/Utopia/Messaging/Adapters/SMS/Termii.php @@ -0,0 +1,86 @@ +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 ?? ''; + + // 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); + } +}