-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSMSGateApp.php
More file actions
86 lines (73 loc) · 2.37 KB
/
SMSGateApp.php
File metadata and controls
86 lines (73 loc) · 2.37 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
<?php
namespace Utopia\Messaging\Adapter\SMS;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS as SMSMessage;
use Utopia\Messaging\Response;
/**
* SMSGateApp adapter class.
*/
class SMSGateApp extends SMSAdapter {
protected const NAME = 'SMS Gateway for Android™';
protected const DEFAULT_API_ENDPOINT = 'https://api.sms-gate.app/3rdparty/v1';
/**
* @param string $apiUsername SMSGate username
* @param string $apiPassword SMSGate password
* @param string|null $apiEndpoint SMSGate API endpoint
*/
public function __construct(
private string $apiUsername,
private string $apiPassword,
private ?string $apiEndpoint = null,
) {
$this->apiEndpoint = $this->apiEndpoint ?: self::DEFAULT_API_ENDPOINT;
}
/**
* {@inheritdoc}
*/
public function getName(): string {
return static::NAME;
}
/**
* {@inheritdoc}
*/
public function getMaxMessagesPerRequest(): int {
return 10;
}
/**
* {@inheritdoc}
*/
protected function process(SMSMessage $message): array {
$response = new Response($this->getType());
$body = [
'textMessage' => [
'text' => $message->getContent(),
],
'phoneNumbers' => $message->getTo(),
];
$result = $this->request(
method: 'POST',
url: $this->apiEndpoint . '/messages?skipPhoneValidation=true',
headers: [
'Content-Type: application/json',
'Authorization: Basic ' . base64_encode("{$this->apiUsername}:{$this->apiPassword}"),
],
body: $body,
);
if ($result['statusCode'] === 202) {
$success = 0;
foreach ($result['response']['recipients'] as $recipient) {
$response->addResult($recipient['phoneNumber'], $recipient['error'] ?? '');
if ($recipient['state'] !== 'Failed') {
$success++;
}
}
$response->setDeliveredTo($success);
} else {
$errorMessage = $result['response']['message'] ?? 'Unknown error';
foreach ($message->getTo() as $recipient) {
$response->addResult($recipient, $errorMessage);
}
}
return $response->toArray();
}
}