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

Added Mobivate Messaging adapter #35

Open
wants to merge 3 commits 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
62 changes: 62 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/Mobivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Utopia\Messaging\Adapters\SMS;

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

// Reference Material
// https://wiki.mobivatebulksms.com/use-cases/send-batch-sms-messages
class Mobivate extends SMSAdapter
{
/**
* @param string $apiKey Mobivate API Key
* @param string $routeId Mobivate routeId
*/
public function __construct(
private string $apiKey,
private string $routeId
) {
}

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

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

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
$messages = [];
foreach ($message->getTo() as $to) {
$temp = [
'originator' => $message->getFrom(),
'recipient' => \ltrim($to, '+'),
'text' => $message->getContent(),
];
array_push($messages, $temp);
}

return $this->request(
method: 'POST',
url: 'https://api.mobivatebulksms.com/send/batch',
headers: [
'content-type: application/json',
'Authorization: Bearer '.$this->apiKey,
],
body: \json_encode([
'routeId' => $this->routeId,
'messages' => $messages,
]),
);
}
}
31 changes: 31 additions & 0 deletions tests/e2e/SMS/MobivateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Tests\E2E;

use Utopia\Messaging\Adapters\SMS\Mobivate;
use Utopia\Messaging\Messages\SMS;

class MobivateTest extends Base
{
/**
* @throws \Exception
*/
public function testSendSMS()
{
$sender = new Mobivate(getenv('MOBIVATE_API_KEY'), getenv('MOBIVATE_ROUTE_ID'));
$to = [getenv('MOBIVATE_TO')];
$from = getenv('MOBIVATE_FROM');

$message = new SMS(
to: $to,
content: 'Test Content',
from: $from
);

$result = \json_decode($sender->send($message));

$this->assertNotEmpty($result);
$this->assertCount(\count($to), $result->recipients);
$this->assertEquals($result->routeId, getenv('MOBIVATE_ROUTE_ID'));
}
}