-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathMsg91.php
79 lines (70 loc) · 1.99 KB
/
Msg91.php
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
<?php
namespace Utopia\Messaging\Adapters\SMS;
use Utopia\Messaging\Adapters\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS;
// Reference Material
// https://docs.msg91.com/p/tf9GTextN/e/7WESqQ4RLu/MSG91
class Msg91 extends SMSAdapter
{
/**
* @param string $senderId Msg91 Sender ID
* @param string $authKey Msg91 Auth Key
*/
public function __construct(
private string $senderId,
private string $authKey
) {
}
public function getName(): string
{
return 'Msg91';
}
public function getMaxMessagesPerRequest(): int
{
// TODO: Find real limit
return 1000;
}
/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
$to = \array_map(
fn ($to) => ['mobiles' => \ltrim($to, '+')],
$message->getTo()
);
return $this->request(
method: 'POST',
url: 'https://api.msg91.com/api/v5/flow/',
headers: [
'content-type: application/json',
"authkey: {$this->authKey}",
],
body: \json_encode([
'sender' => $this->senderId,
'otp' => $message->getContent(),
'flow_id' => $message->getFrom(),
'recipients' => [$to],
]),
);
}
private function addTemplate(string $content, string $senderId, string $templateName, string $smsType = 'NORMAL')
{
return $this->request(
method: 'POST',
url: 'https://control.msg91.com/api/v5/sms/addTemplate',
headers: [
'content-type: application/json',
"authkey: {$this->authKey}",
],
body: \json_encode([
'template' => $content,
'sender_id' => $senderId,
'template_name' => $templateName,
'smsType' => 'NORMAL',
])
);
}
}