Skip to content
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

feat: add messagbird messaging adapter #39

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 51 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/MessageBird.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace Utopia\Messaging\Adapters\SMS;

use Utopia\Messaging\Adapters\SMS as SMSAdapter;
use Utopia\Messaging\Messages\SMS;

class MessageBird extends SMSAdapter {
/**
* @param string $authToken MessageBird Auth Token
*/
public function __construct(
private string $authToken,
) {
}

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

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

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string {
$to = \array_map(
fn ($to) => \ltrim($to, '+'),
$message->getTo()
);

return $this->request(
method: 'POST',
url: "https://rest.messagebird.com/messages",
headers: [
'Authorization: AccessKey ' . $this->authToken,
'Content-Type: application/json',
],
body: \json_encode([
"recipients" => $to,
"originator" => $message->getFrom(),
"body" => $message->getContent(),

])
);
}
}
34 changes: 34 additions & 0 deletions tests/e2e/SMS/MessageBirdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Test\E2E;

use Tests\E2E\Base;
use Utopia\Messaging\Messages\SMS;
use Utopia\Messaging\Adapters\SMS\MessageBird;


class MessageBirdTest extends Base {
/**
* @throws \Exception
*/
public function testSendSMS() {
$apiKey = getenv('MESSAGEBIRD_API_KEY');


$sender = new MessageBird($apiKey);

$message = new SMS(
to: [getenv('MESSAGEBIRD_TO')],
content: 'Test Content',
from: getenv('MESSAGEBIRD_FROM')
);

$response = $sender->send($message);
print_r($response);
$result = \json_decode($response, true);

$this->assertArrayHasKey('body', $result);
$this->assertEquals('Test Content', $result['body']);
$this->assertEquals(1, count($result['recipients']['items']));
}
}
Loading