diff --git a/src/Utopia/Messaging/Adapter/SMS/Vonage.php b/src/Utopia/Messaging/Adapter/SMS/VonageLegacy.php similarity index 95% rename from src/Utopia/Messaging/Adapter/SMS/Vonage.php rename to src/Utopia/Messaging/Adapter/SMS/VonageLegacy.php index d583104c..5e75618f 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Vonage.php +++ b/src/Utopia/Messaging/Adapter/SMS/VonageLegacy.php @@ -9,9 +9,9 @@ use Utopia\Messaging\Messages\SMS; use Utopia\Messaging\Response; -class Vonage extends SMSAdapter +class VonageLegacy extends SMSAdapter { - protected const NAME = 'Vonage'; + protected const NAME = 'Vonage Legacy'; /** * @param string $apiKey Vonage API Key @@ -22,7 +22,6 @@ public function __construct( private string $apiSecret, private ?string $from = null ) { - parent::__construct(); } public function getName(): string diff --git a/src/Utopia/Messaging/Adapter/SMS/VonageMessages.php b/src/Utopia/Messaging/Adapter/SMS/VonageMessages.php new file mode 100644 index 00000000..61c43dfe --- /dev/null +++ b/src/Utopia/Messaging/Adapter/SMS/VonageMessages.php @@ -0,0 +1,102 @@ +apiKey}:{$this->apiSecret}"); + } + + /** + * @return array + */ + 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(); + } +} diff --git a/tests/Messaging/Adapter/SMS/VonageTest.php b/tests/Messaging/Adapter/SMS/VonageLegacyTest.php similarity index 95% rename from tests/Messaging/Adapter/SMS/VonageTest.php rename to tests/Messaging/Adapter/SMS/VonageLegacyTest.php index 67027be9..2a9dcfca 100644 --- a/tests/Messaging/Adapter/SMS/VonageTest.php +++ b/tests/Messaging/Adapter/SMS/VonageLegacyTest.php @@ -4,7 +4,7 @@ use Utopia\Tests\Adapter\Base; -class VonageTest extends Base +class VonageLegacyTest extends Base { /** * @throws \Exception diff --git a/tests/Messaging/Adapter/SMS/VonageMessagesTest.php b/tests/Messaging/Adapter/SMS/VonageMessagesTest.php new file mode 100644 index 00000000..b956b3f1 --- /dev/null +++ b/tests/Messaging/Adapter/SMS/VonageMessagesTest.php @@ -0,0 +1,36 @@ +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']); + */ + } +}