Skip to content

Commit

Permalink
Added: Semaphore SMS adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
7777chaitanya committed Mar 13, 2024
1 parent 0cf631d commit 1ea4259
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 36 deletions.
5 changes: 4 additions & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ VONAGE_API_SECRET=
VONAGE_TO=
VONAGE_FROM=
DISCORD_WEBHOOK_ID=
DISCORD_WEBHOOK_TOKEN=
DISCORD_WEBHOOK_TOKEN=
SEMAPHORE_API_KEY='64d7707e8fb9b8ec0d120e98cb142a9d'
SEMAPHORE_TO='9358574402'
SEMAPHORE_SENDER_NAME='SEMAPHORE'
114 changes: 79 additions & 35 deletions src/Utopia/Messaging/Adapter/SMS/Semaphore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
}

Expand All @@ -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',
Expand All @@ -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" => "[email protected]",
// "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" => ""
// );
25 changes: 25 additions & 0 deletions tests/Messaging/Adapter/SMS/SemaphoreTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Utopia\Tests\Adapter\SMS;

use Utopia\Messaging\Adapter\SMS\Semaphore;
use Utopia\Messaging\Messages\SMS;
use Utopia\Tests\Adapter\Base;

class SemaphoreTest extends Base
{
public function testSendSMS(): void
{
$sender = new Semaphore(getenv('SEMAPHORE_API_KEY'));

$message = new SMS(
[getenv('SEMAPHORE_TO')],
'Test Content',
getenv('SEMAPHORE_SENDER_NAME')
);

$response = $sender->send($message);

$this->assertResponse($response);
}
}

0 comments on commit 1ea4259

Please sign in to comment.