-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSemaphore.php
More file actions
135 lines (112 loc) · 3.78 KB
/
Semaphore.php
File metadata and controls
135 lines (112 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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" => "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" => ""
// );