From e41621773d8457430101344ce81ab25caf9ea862 Mon Sep 17 00:00:00 2001 From: deepshekhardas Date: Mon, 16 Mar 2026 17:55:43 +0530 Subject: [PATCH] feat: add Telnyx SMS adapter (#7811) --- src/Utopia/Messaging/Adapter/SMS/Telnyx.php | 51 +++++++++++---------- tests/Messaging/Adapter/SMS/TelnyxTest.php | 20 ++++---- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/src/Utopia/Messaging/Adapter/SMS/Telnyx.php b/src/Utopia/Messaging/Adapter/SMS/Telnyx.php index 4586c111..e5ed2830 100644 --- a/src/Utopia/Messaging/Adapter/SMS/Telnyx.php +++ b/src/Utopia/Messaging/Adapter/SMS/Telnyx.php @@ -11,19 +11,26 @@ class Telnyx extends SMSAdapter protected const NAME = 'Telnyx'; /** - * @param string $apiKey Telnyx APIv2 Key + * @param string $apiKey Telnyx API Key + * @param string $from Telnyx phone number or profile ID */ public function __construct( private string $apiKey, - private ?string $from = null + private string $from, ) { } + /** + * Get adapter name. + */ public function getName(): string { return static::NAME; } + /** + * Get max messages per request. + */ public function getMaxMessagesPerRequest(): int { return 1; @@ -31,35 +38,31 @@ public function getMaxMessagesPerRequest(): int /** * {@inheritdoc} - * - * @throws \Exception */ protected function process(SMSMessage $message): array { $response = new Response($this->getType()); - $result = $this->request( - method: 'POST', - url: 'https://api.telnyx.com/v2/messages', - headers: [ - 'Content-Type: application/json', - 'Authorization: Bearer '.$this->apiKey, - ], - body: [ - 'text' => $message->getContent(), - 'from' => $this->from ?? $message->getFrom(), - 'to' => $message->getTo()[0], - ], - ); + foreach ($message->getTo() as $to) { + $result = $this->request( + method: 'POST', + url: 'https://api.telnyx.com/v2/messages', + headers: [ + 'Content-Type: application/json', + "Authorization: Bearer {$this->apiKey}", + ], + body: [ + 'from' => $this->from, + 'to' => $to, + 'text' => $message->getContent(), + ] + ); - if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) { - $response->setDeliveredTo(\count($message->getTo())); - foreach ($message->getTo() as $to) { + if ($result['statusCode'] >= 200 && $result['statusCode'] < 300) { + $response->incrementDeliveredTo(); $response->addResult($to); - } - } else { - foreach ($message->getTo() as $to) { - $response->addResult($to, 'Unknown error.'); + } else { + $response->addResult($to, $result['response']['errors'][0]['detail'] ?? 'Unknown error'); } } diff --git a/tests/Messaging/Adapter/SMS/TelnyxTest.php b/tests/Messaging/Adapter/SMS/TelnyxTest.php index 029305af..765a5d42 100644 --- a/tests/Messaging/Adapter/SMS/TelnyxTest.php +++ b/tests/Messaging/Adapter/SMS/TelnyxTest.php @@ -2,6 +2,8 @@ namespace Utopia\Tests\Adapter\SMS; +use Utopia\Messaging\Adapter\SMS\Telnyx; +use Utopia\Messaging\Messages\SMS; use Utopia\Tests\Adapter\Base; class TelnyxTest extends Base @@ -11,18 +13,16 @@ class TelnyxTest extends Base */ public function testSendSMS(): void { - // $sender = new Telnyx(\getenv('TELNYX_API_KEY')); + $sender = new Telnyx(\getenv('TELNYX_API_KEY')); - // $message = new SMS( - // to: ['+18034041123'], - // content: 'Test Content', - // from: '+15005550006' - // ); + $message = new SMS( + to: [\getenv('TELNYX_TO')], + content: 'Test Content', + from: \getenv('TELNYX_FROM') + ); - // $result = $sender->send($message); + $result = $sender->send($message); - // $this->assertEquals('success', $result["type"]); - - $this->markTestSkipped('Telnyx had no testing numbers available at this time.'); + $this->assertResponse($result); } }