-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathSmsGatewayHub.php
More file actions
91 lines (78 loc) · 2.48 KB
/
SmsGatewayHub.php
File metadata and controls
91 lines (78 loc) · 2.48 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
<?php
namespace Utopia\Messaging\Adapter\SMS;
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS as SMSMessage;
use Utopia\Messaging\Response;
class SMSGatewayHub extends SMSAdapter
{
protected const NAME = 'SMSGatewayHub';
/**
* @param string $apiKey SMS Gateway Hub API Key
* @param string $senderId SMS Gateway Hub Sender ID
* @param string $route SMS Gateway Hub Route
* @param string $dltTemplateId SMS Gateway Hub DLT Template ID
* @param string $peId SMS Gateway Hub PEID
*/
public function __construct(
private string $apiKey,
private string $senderId,
private string $route,
private string $dltTemplateId,
private string $peId,
) {
}
public function getName(): string
{
return static::NAME;
}
public function getMaxMessagesPerRequest(): int
{
return 100;
}
/**
* {@inheritdoc}
*/
protected function process(SMSMessage $message): array
{
$recipients = [];
foreach ($message->getTo() as $recipient) {
$recipients[] = \ltrim($recipient, '+');
}
$response = new Response($this->getType());
// Construct the query string
$queryParams = http_build_query([
'apiKey' => $this->apiKey,
'senderid' => $this->senderId,
'channel' => 'Trans',
'DCS' => '0',
'flashsms' => '0',
'number' => \implode(',', $recipients),
'text' => $message->getContent(),
'route' => $this->route,
'DLTTemplateId' => $this->dltTemplateId,
'PEID' => $this->peId,
]);
$url = 'https://login.smsgatewayhub.com/api/mt/SendSMS?' . $queryParams;
$result = $this->request(
'GET',
$url,
[
'Content-Type: application/json',
]
);
// Log the URL and result for debugging
\error_log('Request URL: ' . $url);
\error_log('Response: ' . \json_encode($result));
if ($result['statusCode'] === 200) {
$response->setDeliveredTo(\count($message->getTo()));
foreach ($message->getTo() as $to) {
$response->addResult($to);
}
} else {
foreach ($message->getTo() as $to) {
$response->addResult($to, 'Unknown error');
}
}
return $response->toArray();
}
}