-
Notifications
You must be signed in to change notification settings - Fork 65
feat: add Vonage Messages API adapter #111
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
bhardwajparth51
wants to merge
5
commits into
utopia-php:main
Choose a base branch
from
bhardwajparth51:feat/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.
+217
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2af52bc
feat: add Vonage Messages API adapter
bhardwajparth51 6710210
fix: address code review feedback (Vonage domain, from formatting, te…
bhardwajparth51 81c75ee
test: refine type safety and isolate fallback logic in Vonage tests
bhardwajparth51 b0d9aec
docs: improve docstring coverage for Vonage methods to meet CI threshold
bhardwajparth51 62b741e
fix: move from field trimming before empty guard
bhardwajparth51 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
Some comments aren't visible on the classic Files Changed page.
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,98 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS as SMSAdapter; | ||
| use Utopia\Messaging\Adapter\VonageMessagesBase; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
| use Utopia\Messaging\Response; | ||
|
|
||
| /** | ||
| * Vonage Messages API SMS Adapter. | ||
| * | ||
| * Uses the newer Vonage Messages API (V1) instead of the older SMS API. | ||
| * The Messages API is cheaper and supports multiple message types. | ||
| * | ||
| * Reference: https://developer.vonage.com/en/api/messages | ||
| */ | ||
| class VonageMessages extends SMSAdapter | ||
| { | ||
| use VonageMessagesBase; | ||
|
|
||
| protected const NAME = 'Vonage Messages'; | ||
|
|
||
| /** | ||
| * @param string $apiKey Vonage API Key | ||
| * @param string $apiSecret Vonage API Secret | ||
| */ | ||
| public function __construct( | ||
| private string $apiKey, | ||
| private string $apiSecret, | ||
| private ?string $from = null | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Get adapter name. | ||
| */ | ||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| /** | ||
| * Get max messages per request. | ||
| */ | ||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| $to = \ltrim($message->getTo()[0], '+'); | ||
| $from = $this->from ?? $message->getFrom(); | ||
| $from = $from !== null ? \ltrim($from, '+') : null; | ||
|
|
||
| $response = new Response($this->getType()); | ||
|
|
||
| if (empty($from)) { | ||
| $response->addResult($message->getTo()[0], 'The "from" field is required for the Vonage Messages API.'); | ||
| return $response->toArray(); | ||
| } | ||
|
|
||
| $result = $this->request( | ||
| method: 'POST', | ||
| url: $this->getApiEndpoint(), | ||
| headers: $this->getRequestHeaders(), | ||
| body: [ | ||
| 'message_type' => 'text', | ||
| 'to' => $to, | ||
| 'from' => $from, | ||
| 'text' => $message->getContent(), | ||
| 'channel' => 'sms', | ||
| ], | ||
| ); | ||
|
|
||
| if ($result['statusCode'] === 202) { | ||
| $response->setDeliveredTo(1); | ||
| $response->addResult($message->getTo()[0]); | ||
| } else { | ||
| $errorMessage = 'Unknown error'; | ||
| if (isset($result['response']['detail'])) { | ||
| $errorMessage = $result['response']['detail']; | ||
| } elseif (isset($result['response']['title'])) { | ||
| $errorMessage = $result['response']['title']; | ||
| } elseif (!empty($result['error'])) { | ||
| $errorMessage = $result['error']; | ||
| } | ||
|
|
||
| $response->addResult($message->getTo()[0], $errorMessage); | ||
| } | ||
|
|
||
| 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,50 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter; | ||
|
|
||
| /** | ||
| * Trait for Vonage Messages API adapters. | ||
| * | ||
| * Provides common functionality for adapters using the Vonage Messages API (V1). | ||
| * This trait can be used by different message type adapters (SMS, Chat, etc.). | ||
| * | ||
| * Required properties (from extending class): | ||
| * - $apiKey: string | ||
| * - $apiSecret: string | ||
| * | ||
| * Reference: https://developer.vonage.com/en/api/messages | ||
| */ | ||
| trait VonageMessagesBase | ||
| { | ||
| /** | ||
| * Get the API endpoint for the Vonage Messages API. | ||
| */ | ||
| protected function getApiEndpoint(): string | ||
| { | ||
| return 'https://api.vonage.com/v1/messages'; | ||
| } | ||
|
|
||
| /** | ||
| * Get the authorization header value for the API request. | ||
| * | ||
| * @todo Implement JWT authentication for non-SMS channels | ||
| */ | ||
| protected function getAuthorizationHeader(): string | ||
| { | ||
| return 'Basic ' . \base64_encode("{$this->apiKey}:{$this->apiSecret}"); | ||
| } | ||
|
|
||
| /** | ||
| * Build the request headers for the Messages API. | ||
| * | ||
| * @return array<string> | ||
| */ | ||
| protected function getRequestHeaders(): array | ||
| { | ||
| return [ | ||
| "Authorization: {$this->getAuthorizationHeader()}", | ||
| 'Content-Type: application/json', | ||
| 'Accept: application/json', | ||
| ]; | ||
| } | ||
| } |
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,69 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS\VonageMessages; | ||
| use Utopia\Messaging\Messages\SMS; | ||
| use Utopia\Tests\Adapter\Base; | ||
|
|
||
| class VonageMessagesTest extends Base | ||
| { | ||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public function testSendSMS(): void | ||
| { | ||
| $apiKey = \getenv('VONAGE_API_KEY'); | ||
| $apiSecret = \getenv('VONAGE_API_SECRET'); | ||
| $to = \getenv('VONAGE_TO'); | ||
|
|
||
| if (!$apiKey || !$apiSecret || !$to) { | ||
| $this->markTestSkipped('Vonage Messages credentials or recipient are not available.'); | ||
| } | ||
|
|
||
| $sender = new VonageMessages( | ||
| apiKey: $apiKey, | ||
| apiSecret: $apiSecret, | ||
| from: \getenv('VONAGE_FROM') ?: 'Vonage', | ||
| ); | ||
|
|
||
| $message = new SMS( | ||
| to: [$to], | ||
| content: 'Test Content', | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
|
|
||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public function testSendSMSWithFallbackFrom(): void | ||
| { | ||
| $apiKey = \getenv('VONAGE_API_KEY'); | ||
| $apiSecret = \getenv('VONAGE_API_SECRET'); | ||
| $to = \getenv('VONAGE_TO'); | ||
| $from = \getenv('VONAGE_FROM') ?: null; | ||
|
|
||
| if (!$apiKey || !$apiSecret || !$to || !$from) { | ||
| $this->markTestSkipped('Vonage Messages credentials or sender/recipient are not available.'); | ||
| } | ||
|
|
||
| $sender = new VonageMessages( | ||
| apiKey: $apiKey, | ||
| apiSecret: $apiSecret, | ||
| ); | ||
|
|
||
| $message = new SMS( | ||
| to: [$to], | ||
| content: 'Test Content', | ||
| from: $from, | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.