From 0cf631d096944ddf774792076970cf9d311fbec6 Mon Sep 17 00:00:00 2001 From: Chaitanya7666 Date: Sun, 10 Mar 2024 19:11:28 +0530 Subject: [PATCH 1/2] Added: Semaphore messaging adapter --- .../Messaging/Adapter/SMS/Semaphore.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 src/Utopia/Messaging/Adapter/SMS/Semaphore.php diff --git a/src/Utopia/Messaging/Adapter/SMS/Semaphore.php b/src/Utopia/Messaging/Adapter/SMS/Semaphore.php new file mode 100644 index 0000000..a2538d4 --- /dev/null +++ b/src/Utopia/Messaging/Adapter/SMS/Semaphore.php @@ -0,0 +1,91 @@ +getType()); + $result = $this->request( + method: 'POST', + url: 'https://api.semaphore.co/api/v4/messages', + headers: [ + 'Content-Type: application/json', + ], + body: [ + 'apikey' => $this->apikey, + 'number' => $this->number, + 'message' => $this->message, + 'senderName' => $this->senderName + ], + ); + + if ($result['statusCode'] === 200) { + $response->setDeliveredTo(\count($message->getTo())); + foreach ($message->getTo() as $to) { + $response->addResult($to); + } + } else { + foreach ($message->getTo() as $to) { + $response->addResult($to, 'Unknown error'); + } + } + + return $response->toArray(); + } +} + + +// The following lines of code are used to instantiate an object for the above class +$apikey='jkjdkfafd'; +$number = '07358574402'; +$message = 'Nice meeting you'; +$senderName = 'Chaitanya'; + + +$semaphore = new Semaphore($apikey, $number, $message, $senderName); + + +echo $semaphore; From d2786987ae2eec839507a1293287da9cece1d134 Mon Sep 17 00:00:00 2001 From: Chaitanya7666 Date: Wed, 13 Mar 2024 14:06:32 +0530 Subject: [PATCH 2/2] Added: Semaphore SMS adapter --- .env | 5 +- .../Messaging/Adapter/SMS/Semaphore.php | 114 ++++++++++++------ tests/Messaging/Adapter/SMS/SemaphoreTest.php | 25 ++++ 3 files changed, 108 insertions(+), 36 deletions(-) create mode 100644 tests/Messaging/Adapter/SMS/SemaphoreTest.php 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 index a2538d4..5206ffd 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Semaphore.php +++ b/src/Utopia/Messaging/Adapter/SMS/Semaphore.php @@ -13,19 +13,13 @@ class Semaphore extends SMSAdapter { protected const NAME = 'Semaphore'; - /** + /** * @param string $apikey Semaphore api key - * @param string $number recipient number(s). If multiple numnbers, split with comma. - * @param string $message message you want to send - * @param string $sendername What your recipient sees as the sender of your message */ - + public function __construct( - private string $apikey, - private string $number, - private string $message, - private string $senderName + private string $apikey ) { } @@ -36,17 +30,15 @@ public function getName(): string public function getMaxMessagesPerRequest(): int { - // TODO: Find real limit - return 25; + // NOTE: user can do upto 1000 numbers per API call + return 1000; } /** * {@inheritdoc} */ - protected function process(SMSMessage $message): array + public function process(SMSMessage $message): array { - - $response = new Response($this->getType()); $result = $this->request( method: 'POST', @@ -56,36 +48,88 @@ protected function process(SMSMessage $message): array ], body: [ 'apikey' => $this->apikey, - 'number' => $this->number, - 'message' => $this->message, - 'senderName' => $this->senderName + 'number' => $message->getTo()[0], + 'message' => $message->getContent(), + 'sendername' => $message->getFrom() ], ); if ($result['statusCode'] === 200) { - $response->setDeliveredTo(\count($message->getTo())); - foreach ($message->getTo() as $to) { - $response->addResult($to); - } - } else { - foreach ($message->getTo() as $to) { - $response->addResult($to, 'Unknown error'); + 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(); } } -// The following lines of code are used to instantiate an object for the above class -$apikey='jkjdkfafd'; -$number = '07358574402'; -$message = 'Nice meeting you'; -$senderName = 'Chaitanya'; - - -$semaphore = new Semaphore($apikey, $number, $message, $senderName); - - -echo $semaphore; +// 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); + } +}