From 3bc63f4a630f056748d88261a7bf315cd2b763ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josua=20K=C3=B6nig?= Date: Tue, 28 Feb 2023 21:33:48 +0100 Subject: [PATCH 1/3] add clicksend-adapter --- .../Messaging/Adapters/SMS/ClickSend.php | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Utopia/Messaging/Adapters/SMS/ClickSend.php diff --git a/src/Utopia/Messaging/Adapters/SMS/ClickSend.php b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php new file mode 100644 index 00000000..b6117bb4 --- /dev/null +++ b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php @@ -0,0 +1,64 @@ + \ltrim($to, '+'), + $message->getTo() + ); + + return $this->request( + method: 'POST', + url: 'https://rest.clicksend.com/v3/sms/send', + headers: [ + 'Content-Type: application/json', + 'Authorization: Basic ' . base64_encode("{$this->username}:{$this->apiKey}"), + ], + body: \json_encode([ + 'messages' => [ + [ + 'body' => $message->getContent(), + 'from' => $message->getFrom(), + 'to' => \implode(',', $to), + 'source' => 'appwrite' + ] + ] + ]), + ); + } +} From f62593235979364def504805d70c58765a0b06d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josua=20K=C3=B6nig?= Date: Fri, 19 May 2023 08:58:20 +0200 Subject: [PATCH 2/3] fix: multiple recipients --- README.md | 1 + .../Messaging/Adapters/SMS/ClickSend.php | 26 ++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 445406d1..cdee52ee 100644 --- a/README.md +++ b/README.md @@ -98,6 +98,7 @@ $messaging->send($message); - [x] [TextMagic](https://www.textmagic.com/) - [x] [Msg91](https://msg91.com/) - [x] [Vonage](https://www.vonage.com/) +- [x] [ClickSend](https://www.clicksend.com/) - [ ] [Plivo](https://www.plivo.com/) - [ ] [Infobip](https://www.infobip.com/) - [ ] [Clickatell](https://www.clickatell.com/) diff --git a/src/Utopia/Messaging/Adapters/SMS/ClickSend.php b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php index b6117bb4..bdfb69cb 100644 --- a/src/Utopia/Messaging/Adapters/SMS/ClickSend.php +++ b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php @@ -2,12 +2,12 @@ namespace Utopia\Messaging\Adapters\SMS; -// Reference Material -// https://developers.clicksend.com/docs/rest/v3/#send-sms - use Utopia\Messaging\Adapters\SMS as SMSAdapter; use Utopia\Messaging\Messages\SMS; +// Reference Material +// https://developers.clicksend.com/docs/rest/v3/#send-sms + class ClickSend extends SMSAdapter { /** @@ -27,7 +27,7 @@ public function getName(): string public function getMaxMessagesPerRequest(): int { - return 1; + return 1000; } /** @@ -41,6 +41,15 @@ protected function process(SMS $message): string fn ($to) => \ltrim($to, '+'), $message->getTo() ); + $body = array(); + foreach ($to as $t) { + array_push($body, [ + 'body' => $message->getContent(), + 'from' => $message->getFrom(), + 'to' => $t, + 'source' => 'appwrite' + ]); + } return $this->request( method: 'POST', @@ -50,14 +59,7 @@ protected function process(SMS $message): string 'Authorization: Basic ' . base64_encode("{$this->username}:{$this->apiKey}"), ], body: \json_encode([ - 'messages' => [ - [ - 'body' => $message->getContent(), - 'from' => $message->getFrom(), - 'to' => \implode(',', $to), - 'source' => 'appwrite' - ] - ] + 'messages' => $body ]), ); } From a0cc94e83fe026d68a3fb0853b5f15e3ebf8fccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Josua=20K=C3=B6nig?= Date: Fri, 19 May 2023 11:57:08 +0200 Subject: [PATCH 3/3] fix: some code improvement --- src/Utopia/Messaging/Adapters/SMS/ClickSend.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Utopia/Messaging/Adapters/SMS/ClickSend.php b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php index bdfb69cb..9be69731 100644 --- a/src/Utopia/Messaging/Adapters/SMS/ClickSend.php +++ b/src/Utopia/Messaging/Adapters/SMS/ClickSend.php @@ -7,7 +7,6 @@ // Reference Material // https://developers.clicksend.com/docs/rest/v3/#send-sms - class ClickSend extends SMSAdapter { /** @@ -37,18 +36,17 @@ public function getMaxMessagesPerRequest(): int */ protected function process(SMS $message): string { - $to = \array_map( + $toList = \array_map( fn ($to) => \ltrim($to, '+'), $message->getTo() ); - $body = array(); - foreach ($to as $t) { - array_push($body, [ + $body = []; + foreach ($toList as $to) { + $body[] = [ 'body' => $message->getContent(), 'from' => $message->getFrom(), - 'to' => $t, - 'source' => 'appwrite' - ]); + 'to' => $to + ]; } return $this->request(