diff --git a/.env b/.env index f5fc27a..33579d5 100644 --- a/.env +++ b/.env @@ -25,4 +25,7 @@ VONAGE_API_SECRET= VONAGE_TO= VONAGE_FROM= DISCORD_WEBHOOK_ID= -DISCORD_WEBHOOK_TOKEN= \ No newline at end of file +DISCORD_WEBHOOK_TOKEN= +SEMAPHORE_API_KEY= +SEMAPHORE_TO= +SEMAPHORE_SENDER_NAME= \ No newline at end of file diff --git a/src/Utopia/Messaging/Adapter/SMS/Semaphore.php b/src/Utopia/Messaging/Adapter/SMS/Semaphore.php new file mode 100644 index 0000000..5206ffd --- /dev/null +++ b/src/Utopia/Messaging/Adapter/SMS/Semaphore.php @@ -0,0 +1,135 @@ +getType()); + $result = $this->request( + method: 'POST', + url: 'https://api.semaphore.co/api/v4/messages', + headers: [ + 'Content-Type: application/json', + ], + body: [ + 'apikey' => $this->apikey, + 'number' => $message->getTo()[0], + 'message' => $message->getContent(), + 'sendername' => $message->getFrom() + ], + ); + + if ($result['statusCode'] === 200) { + if ($result['response'][0] && count($result['response'][0]) > 1) { + $response->addResult($message->getTo()[0]); + } else { + foreach ($result['response'] as $variableName) { + $errorMessage = $variableName; + if (is_array($variableName)) { + $response->addResult($message->getTo()[0], $errorMessage[0]); + } else { + $response->addResult($message->getTo()[0], 'Unknown error'); + } + } + } + } + if ($result['statusCode'] === 500) { + $response->addResult($message->getTo()[0], $result['response'][0]); + } + + return $response->toArray(); + } +} + + +// Below is a Sample Error response for bad payload +// The status code is 200 even if there is an error. +// The status code is 200 if semaphore returns a response irrespective of good or error response + +// $errorResponse = array( +// "url" => "https://api.semaphore.co/api/v4/messages", +// "statusCode" => 200, +// "response" => array( +// "sendername" => array( +// "The selected sendername is invalid." +// ) +// ), +// "error" => "" +// ); + + + +// Below is a Sample Error response when Semaphore credits are empty + +// $errorResponse = [ +// "url" => "https://api.semaphore.co/api/v4/messages", +// "statusCode" => 500, +// "response" => [ +// "Your current balance of 0 credits is not sufficient. This transaction requires 1 credits." +// ], +// "error" => "" +// ]; + + + +// Below is a sample success response +// Unlike error response, More than 1 keys are returned in the response array. Refer to docs + +// $successResponse = array( +// "url" => "https://api.semaphore.co/api/v4/messages", +// "statusCode" => 200, +// "response" => array( +// array( +// "message_id" => 212210271, +// "user_id" => 40495, +// "user" => "hello@gmail.com", +// "account_id" => 40356, +// "account" => "Semiphore", +// "recipient" => "639358574402", +// "message" => "Nice meeting you", +// "sender_name" => "Semaphore", +// "network" => "Globe", +// "status" => "Pending", +// "type" => "Single", +// "source" => "Api", +// "created_at" => "2024-03-12 23:14:50", +// "updated_at" => "2024-03-12 23:14:50" +// ) +// ), +// "error" => "" +// ); diff --git a/tests/Messaging/Adapter/SMS/SemaphoreTest.php b/tests/Messaging/Adapter/SMS/SemaphoreTest.php new file mode 100644 index 0000000..5be8e10 --- /dev/null +++ b/tests/Messaging/Adapter/SMS/SemaphoreTest.php @@ -0,0 +1,25 @@ +send($message); + + $this->assertResponse($response); + } +}