-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add messagbird messaging adapter
- Loading branch information
1 parent
2d0f474
commit 0ee3087
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
|
||
]) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'])); | ||
} | ||
} |