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

Fix MSG91 #65

Merged
merged 10 commits into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
44 changes: 19 additions & 25 deletions src/Utopia/Messaging/Adapters/SMS/Msg91.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class Msg91 extends SMSAdapter
{
private $templateId;
abnegate marked this conversation as resolved.
Show resolved Hide resolved

/**
* @param string $senderId Msg91 Sender ID
* @param string $authKey Msg91 Auth Key
Expand All @@ -31,17 +33,28 @@ public function getMaxMessagesPerRequest(): int
return 1000;
}

public function setTemplate(string $templateId): self
{
$this->templateId = $templateId;

return $this;
}

/**
* {@inheritdoc}
*
* @throws \Exception
*/
protected function process(SMS $message): string
{
$to = \array_map(
fn ($to) => ['mobiles' => \ltrim($to, '+')],
loks0n marked this conversation as resolved.
Show resolved Hide resolved
$message->getTo()
);
$recipients = [];
foreach ($message->getTo() as $recipient) {
$recipients[] = [
'mobiles' => $recipient,
'content' => $message->getContent(),
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
'otp' => $message->getContent(),
];
}

return $this->request(
method: 'POST',
Expand All @@ -52,28 +65,9 @@ protected function process(SMS $message): string
],
body: \json_encode([
'sender' => $this->senderId,
'otp' => $message->getContent(),
'flow_id' => $message->getFrom(),
'recipients' => [$to],
'template_id' => $this->templateId,
'recipients' => $recipients,
]),
);
}

private function addTemplate(string $content, string $senderId, string $templateName, string $smsType = 'NORMAL')
{
return $this->request(
method: 'POST',
url: 'https://control.msg91.com/api/v5/sms/addTemplate',
headers: [
'content-type: application/json',
"authkey: {$this->authKey}",
],
body: \json_encode([
'template' => $content,
'sender_id' => $senderId,
'template_name' => $templateName,
'smsType' => 'NORMAL',
])
);
}
}
21 changes: 10 additions & 11 deletions tests/e2e/SMS/Msg91Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ class Msg91Test extends Base
*/
public function testSendSMS()
{
// $sender = new Msg91(getenv('MSG_91_SENDER_ID'), getenv('MSG_91_AUTH_KEY'));
$sender = new Msg91(getenv('MSG_91_SENDER_ID'), getenv('MSG_91_AUTH_KEY'));
$sender->setTemplate(getenv('MSG_91_FROM'));

// $message = new SMS(
// to: [getenv('MSG_91_TO')],
// content: 'Test Content',
// from: getenv('MSG_91_FROM')
// );
$message = new SMS(
to: [getenv('MSG_91_TO')],
content: 'Test Content',
);

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

// $this->assertEquals('success', $result['type']);

$this->markTestSkipped('Msg91 requires business verification to use template and SMS api.');
$this->assertEquals('success', $result['type']);
$this->assertNotEmpty($result['message']);
}
}
Loading