Skip to content

Commit d278698

Browse files
committed
Added: Semaphore SMS adapter
1 parent 0cf631d commit d278698

File tree

3 files changed

+108
-36
lines changed

3 files changed

+108
-36
lines changed

.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,7 @@ VONAGE_API_SECRET=
2525
VONAGE_TO=
2626
VONAGE_FROM=
2727
DISCORD_WEBHOOK_ID=
28-
DISCORD_WEBHOOK_TOKEN=
28+
DISCORD_WEBHOOK_TOKEN=
29+
SEMAPHORE_API_KEY=
30+
SEMAPHORE_TO=
31+
SEMAPHORE_SENDER_NAME=

src/Utopia/Messaging/Adapter/SMS/Semaphore.php

Lines changed: 79 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,13 @@ class Semaphore extends SMSAdapter
1313
{
1414
protected const NAME = 'Semaphore';
1515

16-
/**
16+
/**
1717
* @param string $apikey Semaphore api key
18-
* @param string $number recipient number(s). If multiple numnbers, split with comma.
19-
* @param string $message message you want to send
20-
* @param string $sendername What your recipient sees as the sender of your message
2118
*/
2219

23-
20+
2421
public function __construct(
25-
private string $apikey,
26-
private string $number,
27-
private string $message,
28-
private string $senderName
22+
private string $apikey
2923
) {
3024
}
3125

@@ -36,17 +30,15 @@ public function getName(): string
3630

3731
public function getMaxMessagesPerRequest(): int
3832
{
39-
// TODO: Find real limit
40-
return 25;
33+
// NOTE: user can do upto 1000 numbers per API call
34+
return 1000;
4135
}
4236

4337
/**
4438
* {@inheritdoc}
4539
*/
46-
protected function process(SMSMessage $message): array
40+
public function process(SMSMessage $message): array
4741
{
48-
49-
5042
$response = new Response($this->getType());
5143
$result = $this->request(
5244
method: 'POST',
@@ -56,36 +48,88 @@ protected function process(SMSMessage $message): array
5648
],
5749
body: [
5850
'apikey' => $this->apikey,
59-
'number' => $this->number,
60-
'message' => $this->message,
61-
'senderName' => $this->senderName
51+
'number' => $message->getTo()[0],
52+
'message' => $message->getContent(),
53+
'sendername' => $message->getFrom()
6254
],
6355
);
6456

6557
if ($result['statusCode'] === 200) {
66-
$response->setDeliveredTo(\count($message->getTo()));
67-
foreach ($message->getTo() as $to) {
68-
$response->addResult($to);
69-
}
70-
} else {
71-
foreach ($message->getTo() as $to) {
72-
$response->addResult($to, 'Unknown error');
58+
if ($result['response'][0] && count($result['response'][0]) > 1) {
59+
$response->addResult($message->getTo()[0]);
60+
} else {
61+
foreach ($result['response'] as $variableName) {
62+
$errorMessage = $variableName;
63+
if (is_array($variableName)) {
64+
$response->addResult($message->getTo()[0], $errorMessage[0]);
65+
} else {
66+
$response->addResult($message->getTo()[0], 'Unknown error');
67+
}
68+
}
7369
}
7470
}
71+
if ($result['statusCode'] === 500) {
72+
$response->addResult($message->getTo()[0], $result['response'][0]);
73+
}
7574

7675
return $response->toArray();
7776
}
7877
}
7978

8079

81-
// The following lines of code are used to instantiate an object for the above class
82-
$apikey='jkjdkfafd';
83-
$number = '07358574402';
84-
$message = 'Nice meeting you';
85-
$senderName = 'Chaitanya';
86-
87-
88-
$semaphore = new Semaphore($apikey, $number, $message, $senderName);
89-
90-
91-
echo $semaphore;
80+
// Below is a Sample Error response for bad payload
81+
// The status code is 200 even if there is an error.
82+
// The status code is 200 if semaphore returns a response irrespective of good or error response
83+
84+
// $errorResponse = array(
85+
// "url" => "https://api.semaphore.co/api/v4/messages",
86+
// "statusCode" => 200,
87+
// "response" => array(
88+
// "sendername" => array(
89+
// "The selected sendername is invalid."
90+
// )
91+
// ),
92+
// "error" => ""
93+
// );
94+
95+
96+
97+
// Below is a Sample Error response when Semaphore credits are empty
98+
99+
// $errorResponse = [
100+
// "url" => "https://api.semaphore.co/api/v4/messages",
101+
// "statusCode" => 500,
102+
// "response" => [
103+
// "Your current balance of 0 credits is not sufficient. This transaction requires 1 credits."
104+
// ],
105+
// "error" => ""
106+
// ];
107+
108+
109+
110+
// Below is a sample success response
111+
// Unlike error response, More than 1 keys are returned in the response array. Refer to docs
112+
113+
// $successResponse = array(
114+
// "url" => "https://api.semaphore.co/api/v4/messages",
115+
// "statusCode" => 200,
116+
// "response" => array(
117+
// array(
118+
// "message_id" => 212210271,
119+
// "user_id" => 40495,
120+
// "user" => "[email protected]",
121+
// "account_id" => 40356,
122+
// "account" => "Semiphore",
123+
// "recipient" => "639358574402",
124+
// "message" => "Nice meeting you",
125+
// "sender_name" => "Semaphore",
126+
// "network" => "Globe",
127+
// "status" => "Pending",
128+
// "type" => "Single",
129+
// "source" => "Api",
130+
// "created_at" => "2024-03-12 23:14:50",
131+
// "updated_at" => "2024-03-12 23:14:50"
132+
// )
133+
// ),
134+
// "error" => ""
135+
// );
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Utopia\Tests\Adapter\SMS;
4+
5+
use Utopia\Messaging\Adapter\SMS\Semaphore;
6+
use Utopia\Messaging\Messages\SMS;
7+
use Utopia\Tests\Adapter\Base;
8+
9+
class SemaphoreTest extends Base
10+
{
11+
public function testSendSMS(): void
12+
{
13+
$sender = new Semaphore(getenv('SEMAPHORE_API_KEY'));
14+
15+
$message = new SMS(
16+
[getenv('SEMAPHORE_TO')],
17+
'Test Content',
18+
getenv('SEMAPHORE_SENDER_NAME')
19+
);
20+
21+
$response = $sender->send($message);
22+
23+
$this->assertResponse($response);
24+
}
25+
}

0 commit comments

Comments
 (0)