Skip to content

feat: add clicksend-sms-adapter #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)
Expand Down
66 changes: 66 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/ClickSend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Utopia\Messaging\Adapters\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
{
/**
* @param string $username ClickSend Username
* @param string $apiKey ClickSend API Key
*/
public function __construct(
private string $username,
private string $apiKey
) {
}

public function getName(): string
{
return 'ClickSend';
}

public function getMaxMessagesPerRequest(): int
{
return 1000;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
$to = \array_map(
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',
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
]),
);
}
}