-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0cf631d
commit 1ea4259
Showing
3 changed files
with
108 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" => "[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" => "" | ||
// ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |