-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add Vonage multi-channel Messages adapter (#8139) #110
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
Open
deepshekhardas
wants to merge
1
commit into
utopia-php:main
Choose a base branch
from
deepshekhardas:feat/8139-vonage-messages-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter; | ||
|
|
||
| use Utopia\Messaging\Adapter; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| abstract class MMS extends Adapter | ||
| { | ||
| protected const TYPE = 'mms'; | ||
| protected const MESSAGE_TYPE = SMSMessage::class; | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return static::TYPE; | ||
| } | ||
|
|
||
| public function getMessageType(): string | ||
| { | ||
| return static::MESSAGE_TYPE; | ||
| } | ||
|
|
||
| /** | ||
| * Send an MMS message. | ||
| * | ||
| * @param SMSMessage $message Message to send. | ||
| * @return array{deliveredTo: int, type: string, results: array<array<string, mixed>>} | ||
| * | ||
| * @throws \Exception If the message fails. | ||
| */ | ||
| abstract protected function process(SMSMessage $message): array; | ||
| } |
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\MMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\MMS as MMSAdapter; | ||
| use Utopia\Messaging\Adapter\VonageTrait; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| class Vonage extends MMSAdapter | ||
| { | ||
| use VonageTrait; | ||
|
|
||
| protected const NAME = 'Vonage'; | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| return $this->processMessage($message, 'mms'); | ||
| } | ||
| } |
This file contains hidden or 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 Utopia\Messaging\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS as SMSAdapter; | ||
| use Utopia\Messaging\Helpers\JWT; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
| use Utopia\Messaging\Response; | ||
|
|
||
| class VonageMessages extends SMSAdapter | ||
| { | ||
| use VonageTrait; | ||
|
|
||
| protected const NAME = 'VonageMessages'; | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| return $this->processMessage($message, 'sms'); | ||
| } | ||
| } | ||
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter; | ||
|
|
||
| use Utopia\Messaging\Adapter; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| abstract class Viber extends Adapter | ||
| { | ||
| protected const TYPE = 'viber'; | ||
| protected const MESSAGE_TYPE = SMSMessage::class; | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return static::TYPE; | ||
| } | ||
|
|
||
| public function getMessageType(): string | ||
| { | ||
| return static::MESSAGE_TYPE; | ||
| } | ||
|
|
||
| /** | ||
| * Send a Viber message. | ||
| * | ||
| * @param SMSMessage $message Message to send. | ||
| * @return array{deliveredTo: int, type: string, results: array<array<string, mixed>>} | ||
| * | ||
| * @throws \Exception If the message fails. | ||
| */ | ||
| abstract protected function process(SMSMessage $message): array; | ||
| } |
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\Viber; | ||
|
|
||
| use Utopia\Messaging\Adapter\Viber as ViberAdapter; | ||
| use Utopia\Messaging\Adapter\VonageTrait; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| class Vonage extends ViberAdapter | ||
| { | ||
| use VonageTrait; | ||
|
|
||
| protected const NAME = 'Vonage'; | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| return $this->processMessage($message, 'viber'); | ||
| } | ||
| } |
This file contains hidden or 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,72 @@ | ||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| namespace Utopia\Messaging\Adapter; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Helpers\JWT; | ||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Response; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| trait VonageTrait | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * @param string $applicationId Vonage Application ID | ||||||||||||||||||||||||||||||||
| * @param string $privateKey Vonage Private Key | ||||||||||||||||||||||||||||||||
| * @param string|null $from Sender phone number or name | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| public function __construct( | ||||||||||||||||||||||||||||||||
| private string $applicationId, | ||||||||||||||||||||||||||||||||
| private string $privateKey, | ||||||||||||||||||||||||||||||||
| private ?string $from = null | ||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||
| * @throws \Exception | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| protected function processMessage(SMSMessage $message, string $channel): array | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| $payload = [ | ||||||||||||||||||||||||||||||||
| 'from' => $this->from ?? $message->getFrom(), | ||||||||||||||||||||||||||||||||
| 'to' => \ltrim($message->getTo()[0], '+'), | ||||||||||||||||||||||||||||||||
| 'message_type' => 'text', | ||||||||||||||||||||||||||||||||
| 'text' => $message->getContent(), | ||||||||||||||||||||||||||||||||
| 'channel' => $channel, | ||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $jwt = JWT::encode( | ||||||||||||||||||||||||||||||||
| [ | ||||||||||||||||||||||||||||||||
| 'application_id' => $this->applicationId, | ||||||||||||||||||||||||||||||||
| 'iat' => \time(), | ||||||||||||||||||||||||||||||||
| 'jti' => \bin2hex(\random_bytes(16)), | ||||||||||||||||||||||||||||||||
| 'exp' => \time() + 3600, | ||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||
|
Comment on lines
+36
to
+42
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||
| $this->privateKey, | ||||||||||||||||||||||||||||||||
| 'RS256' | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $response = new Response($this->getType()); | ||||||||||||||||||||||||||||||||
| $result = $this->request( | ||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||
| url: 'https://api.nexmo.com/v1/messages', | ||||||||||||||||||||||||||||||||
| headers: [ | ||||||||||||||||||||||||||||||||
| 'Content-Type: application/json', | ||||||||||||||||||||||||||||||||
| 'Authorization: Bearer ' . $jwt, | ||||||||||||||||||||||||||||||||
| 'Accept: application/json', | ||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||
| body: $payload, | ||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| $statusCode = $result['statusCode']; | ||||||||||||||||||||||||||||||||
| $res = $result['response']; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if ($statusCode >= 200 && $statusCode < 300 && isset($res['message_uuid'])) { | ||||||||||||||||||||||||||||||||
| $response->setDeliveredTo(1); | ||||||||||||||||||||||||||||||||
| $response->addResult($message->getTo()[0]); | ||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||
| $error = $res['detail'] ?? $res['title'] ?? 'Unknown error'; | ||||||||||||||||||||||||||||||||
| $response->addResult($message->getTo()[0], $error); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return $response->toArray(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
This file contains hidden or 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,32 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter; | ||
|
|
||
| use Utopia\Messaging\Adapter; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| abstract class WhatsApp extends Adapter | ||
| { | ||
| protected const TYPE = 'whatsapp'; | ||
| protected const MESSAGE_TYPE = SMSMessage::class; | ||
|
|
||
| public function getType(): string | ||
| { | ||
| return static::TYPE; | ||
| } | ||
|
|
||
| public function getMessageType(): string | ||
| { | ||
| return static::MESSAGE_TYPE; | ||
| } | ||
|
|
||
| /** | ||
| * Send a WhatsApp message. | ||
| * | ||
| * @param SMSMessage $message Message to send. | ||
| * @return array{deliveredTo: int, type: string, results: array<array<string, mixed>>} | ||
| * | ||
| * @throws \Exception If the message fails. | ||
| */ | ||
| abstract protected function process(SMSMessage $message): array; | ||
| } |
This file contains hidden or 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,33 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\WhatsApp; | ||
|
|
||
| use Utopia\Messaging\Adapter\WhatsApp as WhatsAppAdapter; | ||
| use Utopia\Messaging\Adapter\VonageTrait; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
|
|
||
| class Vonage extends WhatsAppAdapter | ||
| { | ||
| use VonageTrait; | ||
|
|
||
| protected const NAME = 'Vonage'; | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| return $this->processMessage($message, 'whatsapp'); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
VonageTraitis never imported in this file. BecauseVonageMessageslives inUtopia\Messaging\Adapter\SMS, PHP will look forUtopia\Messaging\Adapter\SMS\VonageTraitwhen it encountersuse VonageTrait;— a class that doesn't exist — causing a fatal "Class not found" error at runtime. Additionally,JWTandResponseare imported here but never used directly in this class (they are consumed by the trait), making those imports both redundant and misleading.