Skip to content

Commit 11fa346

Browse files
committed
feat: Added SMSGateApp adapter
1 parent 5d12452 commit 11fa346

File tree

3 files changed

+124
-0
lines changed

3 files changed

+124
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ $messaging->send($message);
106106
- [x] [Seven](https://www.seven.io/)
107107
- [ ] [SmsGlobal](https://www.smsglobal.com/)
108108
- [x] [Inforu](https://www.inforu.co.il/)
109+
- [x] [SMS Gateway for Android™](https://sms-gate.app/)
109110

110111
### Push
111112
- [x] [FCM](https://firebase.google.com/docs/cloud-messaging)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Utopia\Messaging\Adapter\SMS;
4+
5+
use Utopia\Messaging\Adapter\SMS as SMSAdapter;
6+
use Utopia\Messaging\Messages\SMS as SMSMessage;
7+
use Utopia\Messaging\Response;
8+
9+
/**
10+
* SMSGateApp adapter class.
11+
*/
12+
class SMSGateApp extends SMSAdapter {
13+
protected const NAME = 'SMS Gateway for Android™';
14+
protected const DEFAULT_API_ENDPOINT = 'https://api.sms-gate.app/3rdparty/v1';
15+
16+
/**
17+
* @param string $apiUsername SMSGate username
18+
* @param string $apiPassword SMSGate password
19+
* @param string|null $apiEndpoint SMSGate API endpoint
20+
*/
21+
public function __construct(
22+
private string $apiUsername,
23+
private string $apiPassword,
24+
private ?string $apiEndpoint = null,
25+
) {
26+
$this->apiEndpoint = $this->apiEndpoint ?: self::DEFAULT_API_ENDPOINT;
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function getName(): string {
33+
return static::NAME;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getMaxMessagesPerRequest(): int {
40+
return 10;
41+
}
42+
43+
/**
44+
* {@inheritdoc}
45+
*/
46+
protected function process(SMSMessage $message): array {
47+
$response = new Response($this->getType());
48+
49+
$body = [
50+
'textMessage' => [
51+
'text' => $message->getContent(),
52+
],
53+
'phoneNumbers' => $message->getTo(),
54+
];
55+
56+
$result = $this->request(
57+
method: 'POST',
58+
url: $this->apiEndpoint . '/messages?skipPhoneValidation=true',
59+
headers: [
60+
'Content-Type: application/json',
61+
'Authorization: Basic ' . base64_encode("{$this->apiUsername}:{$this->apiPassword}"),
62+
],
63+
body: $body,
64+
);
65+
66+
if ($result['statusCode'] === 202) {
67+
$success = 0;
68+
foreach ($result['response']['recipients'] as $recipient) {
69+
$response->addResult($recipient['phoneNumber'], $recipient['error'] ?? '');
70+
71+
if ($recipient['state'] !== 'Failed') {
72+
$success++;
73+
}
74+
}
75+
76+
$response->setDeliveredTo($success);
77+
} else {
78+
$errorMessage = $result['response']['message'] ?? 'Unknown error';
79+
foreach ($message->getTo() as $recipient) {
80+
$response->addResult($recipient, $errorMessage);
81+
}
82+
}
83+
84+
return $response->toArray();
85+
}
86+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Utopia\Tests\Adapter\SMS;
4+
5+
use Utopia\Messaging\Adapter\SMS\SMSGateApp;
6+
use Utopia\Messaging\Messages\SMS;
7+
use Utopia\Tests\Adapter\Base;
8+
9+
class SMSGateAppTest extends Base {
10+
/**
11+
* Test sending SMS message with SMSGateApp
12+
*/
13+
public function testSendSMS(): void {
14+
// Environment variables for credentials
15+
$username = \getenv('SMSGATEAPP_USERNAME');
16+
$password = \getenv('SMSGATEAPP_PASSWORD');
17+
$to = \getenv('SMSGATEAPP_TO');
18+
19+
// Optional API endpoint if set
20+
$endpoint = \getenv('SMSGATEAPP_ENDPOINT') ?? null;
21+
22+
// Instantiate SMSGateApp
23+
$sender = new SMSGateApp($username, $password, $endpoint);
24+
25+
// Create SMS message with required 'to' parameter
26+
$message = new SMS(
27+
to: [$to],
28+
content: 'Test content from SMSGateApp'
29+
);
30+
31+
// Call send() and verify response
32+
$response = $sender->send($message);
33+
34+
// Assertion to match expected response formatting
35+
$this->assertResponse($response);
36+
}
37+
}

0 commit comments

Comments
 (0)