-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathVonageMessages.php
More file actions
108 lines (91 loc) · 3.33 KB
/
VonageMessages.php
File metadata and controls
108 lines (91 loc) · 3.33 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
<?php
namespace Utopia\Messaging\Adapter\SMS;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS as SMSMessage;
use Utopia\Messaging\Response;
// Vonage Messages API SMS Adapter.
// This adapter uses the modern Vonage Messages API (V1) which is more cost-effective
// and versatile than the legacy SMS API.
// https://developer.vonage.com/en/api/messages
class VonageMessages extends SMSAdapter
{
protected const NAME = 'Vonage Messages';
public function __construct(
private string $apiKey,
private string $apiSecret,
private ?string $from = null
) {
}
// Vonage Messages API endpoint.
protected function getApiEndpoint(): string
{
return 'https://api.vonage.com/v1/messages';
}
// Generates the Basic Authorization header.
protected function getAuthorizationHeader(): string
{
return 'Basic ' . \base64_encode("{$this->apiKey}:{$this->apiSecret}");
}
// Sets common headers for the API request.
protected function getRequestHeaders(): array
{
return [
"Authorization: {$this->getAuthorizationHeader()}",
'Content-Type: application/json',
'Accept: application/json',
'User-Agent: Utopia Messaging',
];
}
// Get adapter name.
public function getName(): string
{
return static::NAME;
}
// Get max messages per request.
public function getMaxMessagesPerRequest(): int
{
return 1;
}
protected function process(SMSMessage $message): array
{
$to = \ltrim($message->getTo()[0], '+');
$from = $this->from ?? $message->getFrom();
$from = $from !== null ? \ltrim($from, '+') : null;
$response = new Response($this->getType());
if (empty($from)) {
$response->addResult($message->getTo()[0], 'The "from" field is required for the Vonage Messages API.');
return $response->toArray();
}
$result = $this->request(
method: 'POST',
url: $this->getApiEndpoint(),
headers: $this->getRequestHeaders(),
body: [
'message_type' => 'text',
'to' => $to,
'from' => $from,
'text' => $message->getContent(),
'channel' => 'sms',
],
);
if ($result['statusCode'] === 202) {
$response->setDeliveredTo(1);
$response->addResult($message->getTo()[0]);
} else {
$errorMessage = "Error {$result['statusCode']}";
if (\is_array($result['response'])) {
if (isset($result['response']['detail'])) {
$errorMessage = $result['response']['detail'];
} elseif (isset($result['response']['title'])) {
$errorMessage = $result['response']['title'];
}
} elseif (!empty($result['error'])) {
$errorMessage = $result['error'];
} elseif (\is_string($result['response']) && !empty($result['response'])) {
$errorMessage = "Error {$result['statusCode']}: " . \mb_strimwidth(\strip_tags($result['response']), 0, 100, '...');
}
$response->addResult($message->getTo()[0], $errorMessage);
}
return $response->toArray();
}
}