Skip to content

Commit

Permalink
feat: add messagbird messaging adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
olamide203 committed Oct 4, 2023
1 parent 2d0f474 commit 0ee3087
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
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']));
}
}

0 comments on commit 0ee3087

Please sign in to comment.