Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat 7714 adding semaphore messaging adapter #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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=
SEMAPHORE_TO=
SEMAPHORE_SENDER_NAME=
135 changes: 135 additions & 0 deletions src/Utopia/Messaging/Adapter/SMS/Semaphore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Utopia\Messaging\Adapter\SMS;

use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS as SMSMessage;
use Utopia\Messaging\Response;

// Reference Material
// https://semaphore.co/docs

class Semaphore extends SMSAdapter
{
protected const NAME = 'Semaphore';

/**
* @param string $apikey Semaphore api key
*/


public function __construct(
private string $apikey
) {
}

public function getName(): string
{
return static::NAME;
}

public function getMaxMessagesPerRequest(): int
{
// NOTE: user can do upto 1000 numbers per API call
return 1000;
}

/**
* {@inheritdoc}
*/
public function process(SMSMessage $message): array
{
$response = new Response($this->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" => "[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);
}
}