-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add Vonage Messages API adapter #127
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS as SMSAdapter; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
| use Utopia\Messaging\Response; | ||
|
|
||
| class VonageMessages extends SMSAdapter | ||
| { | ||
| protected const NAME = 'Vonage Messages'; | ||
|
|
||
| public function __construct( | ||
| private string $apiKey, | ||
| private string $apiSecret, | ||
| private ?string $from = null | ||
| ) { | ||
| } | ||
|
|
||
| protected function getApiEndpoint(): string | ||
| { | ||
| return 'https://api.vonage.com/v1/messages'; | ||
| } | ||
|
|
||
| protected function getAuthorizationHeader(): string | ||
| { | ||
| return 'Basic ' . \base64_encode("{$this->apiKey}:{$this->apiSecret}"); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<string> | ||
| */ | ||
| protected function getRequestHeaders(): array | ||
| { | ||
| return [ | ||
| "Authorization: {$this->getAuthorizationHeader()}", | ||
| 'Content-Type: application/json', | ||
| 'Accept: application/json', | ||
| 'User-Agent: Utopia Messaging', | ||
| ]; | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 1; | ||
| } | ||
|
|
||
| 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 = "Error {$result['statusCode']}"; | ||
|
|
||
| if (\is_array($result['response'])) { | ||
| 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']; | ||
| } elseif (\is_string($result['response']) && !empty($result['response'])) { | ||
| $errorMessage = "Error {$result['statusCode']}: " . \mb_strimwidth(\strip_tags($result['response']), 0, 100, '...'); | ||
| } | ||
|
|
||
| $response->addResult($message->getTo()[0], $errorMessage); | ||
| } | ||
|
|
||
| return $response->toArray(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?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 | ||
| { | ||
| $this->markTestSkipped('Vonage Messages credentials are not available.'); | ||
|
|
||
| /* | ||
| $apiKey = \getenv('VONAGE_MESSAGES_API_KEY'); | ||
| $apiSecret = \getenv('VONAGE_MESSAGES_API_SECRET'); | ||
|
|
||
| $sender = new VonageMessages($apiKey, $apiSecret); | ||
|
|
||
| $message = new SMS( | ||
| to: [\getenv('VONAGE_MESSAGES_TO')], | ||
| content: 'Test Content', | ||
| from: \getenv('VONAGE_MESSAGES_FROM'), | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertNotEmpty($response['results']); | ||
| $this->assertNotEmpty($response['results'][0]['success']); | ||
| */ | ||
| } | ||
|
Comment on lines
+14
to
+35
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. The test immediately calls |
||
| } | ||
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.
Renaming
Vonage→VonageLegacydrops the original class name entirely. Any downstream code usingnew \Utopia\Messaging\Adapter\SMS\Vonage(...)oruse Utopia\Messaging\Adapter\SMS\Vonagewill throw a fatalClass not founderror after upgrading this library. The PR description mentions a "backward compat note" but no alias or deprecation shim is present in the diff. Consider addingclass_alias(VonageLegacy::class, 'Utopia\\Messaging\\Adapter\\SMS\\Vonage');at the bottom of this file (or in a dedicated compat file), or coordinate a semver-major release.