-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathTermii.php
More file actions
86 lines (73 loc) · 1.88 KB
/
Termii.php
File metadata and controls
86 lines (73 loc) · 1.88 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\Adapters\SMS;
use Utopia\Messaging\Adapters\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS;
// Reference Material
// https://developers.termii.com/messaging-api
class Termii extends SMSAdapter
{
const SMSType = 'plain';
const SMSChannel = 'generic';
public function __construct(
private string $apiKey
) {
}
public function getName(): string
{
return 'Termii';
}
public function getMaxMessagesPerRequest(): int
{
return 100;
}
/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
return $this->request(
method: 'POST',
url: 'https://api.ng.termii.com/api/sms/send',
headers: [
'Content-Type: application/json',
],
body: \json_encode(
$this->getRequestBody(
to: $message->getTo(),
text: $message->getContent(),
from: $message->getFrom()
)
),
);
}
/**
* Get the request body
*
* @param array $to Phone number
* @param string $text Message to send
* @param string|null $from Origin of the message
*/
private function getRequestBody(array $to, string $text, string $from = null): array
{
$from = $from ?? '';
// removing + from numbers if exists
$to = \array_map(
fn ($to) => \ltrim($to, '+'),
$to
);
if (count($to) == 1) {
$to = $to[0];
}
$body = [
'api_key' => $this->apiKey,
'to' => $to,
'from' => $from,
'sms' => $text,
'type' => self::SMSType,
'channel' => self::SMSChannel,
];
return $body;
}
}