Skip to content
Closed
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
64 changes: 64 additions & 0 deletions src/Utopia/Messaging/Adapters/SMS/Mobivate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?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 = array();

foreach($message->getTo() as $to){
$temp = array(
"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
]),
);
}
}
34 changes: 34 additions & 0 deletions tests/e2e/SMS/MobivateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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'))

}
}