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+ }
0 commit comments