diff --git a/AGENTS.md b/AGENTS.md index da9c7beadc..dd25856331 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -222,6 +222,7 @@ Actions should read like a story. The `action()` method is the plot: [`Teams/Htt ## Conventions - PSR-12 (Pint), PSR-4 autoloading. Avoid dependencies outside the `utopia-php` ecosystem. Never hardcode credentials — use env vars. Code changes may require a container restart; logs live on the relevant container. +- Do not add regular expressions (`preg_*`, regex patterns passed to validators). Use string functions (`str_starts_with`, `str_ends_with`, `str_contains`, `strlen`, `substr`, `explode`, `ctype_*`) or an existing validator instead; regex is hard to review and easy to get subtly wrong. If nothing else works, explain why in the PR. - When updating documents, pass only changed attributes as a sparse Document: ```php diff --git a/app/config/variables.php b/app/config/variables.php index a376989d3e..d3ca813a77 100644 --- a/app/config/variables.php +++ b/app/config/variables.php @@ -873,7 +873,7 @@ return [ ], [ 'name' => '_APP_SMS_FROM', - 'description' => 'Phone number used for sending out messages. If using Twilio, this may be a Messaging Service SID, starting with MG. Otherwise, the number must start with a leading \'+\' and maximum of 15 digits without spaces (+123456789). ', + 'description' => 'Phone number, or an alphanumeric sender ID where the provider supports it, used for sending out messages. A phone number must start with a leading \'+\' and have a maximum of 15 digits without spaces (+123456789). If using Twilio, this may also be a Messaging Service SID, starting with MG.', 'introduction' => '0.15.0', 'default' => '', 'required' => false, diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php index e453bb054c..2908b495d7 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Create.php @@ -2,7 +2,6 @@ namespace Appwrite\Platform\Modules\Messaging\Http\Providers\Twilio; -use Appwrite\Auth\Validator\Phone; use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Appwrite\SDK\AuthType; @@ -56,7 +55,7 @@ class Create extends Action )) ->param('providerId', '', fn (Database $dbForProject) => new CustomId(false, $dbForProject->getAdapter()->getMaxUIDLength()), 'Provider ID. Choose a custom ID or generate a random ID with `ID.unique()`. Valid chars are a-z, A-Z, 0-9, period, hyphen, and underscore. Can\'t start with a special char. Max length is 36 chars.', false, ['dbForProject']) ->param('name', '', new Text(128), 'Provider name.') - ->param('from', '', new Phone(), 'Sender Phone number. Format this number with a leading \'+\' and a country code, e.g., +16175551212.', true) + ->param('from', '', new Text(256), 'Sender phone number or alphanumeric sender ID. Format phone numbers with a leading \'+\' and a country code, e.g., +16175551212.', true) ->param('accountSid', '', new Text(0), 'Twilio account secret ID.', true) ->param('authToken', '', new Text(0), 'Twilio authentication token.', true) ->param('enabled', null, new Nullable(new Boolean()), 'Set as enabled.', true) diff --git a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php index 0f60946613..83b63222e1 100644 --- a/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php +++ b/src/Appwrite/Platform/Modules/Messaging/Http/Providers/Twilio/Update.php @@ -55,7 +55,7 @@ class Update extends Action ->param('enabled', null, new Nullable(new Boolean()), 'Set as enabled.', true) ->param('accountSid', '', new Text(0), 'Twilio account secret ID.', true) ->param('authToken', '', new Text(0), 'Twilio authentication token.', true) - ->param('from', '', new Text(256), 'Sender number.', true) + ->param('from', '', new Text(256), 'Sender phone number or alphanumeric sender ID. Format phone numbers with a leading \'+\' and a country code, e.g., +16175551212.', true) ->inject('queueForEvents') ->inject('dbForProject') ->inject('response') diff --git a/src/Appwrite/Platform/Workers/Messaging.php b/src/Appwrite/Platform/Workers/Messaging.php index 4e53ecd759..ce012272cb 100644 --- a/src/Appwrite/Platform/Workers/Messaging.php +++ b/src/Appwrite/Platform/Workers/Messaging.php @@ -1278,9 +1278,9 @@ class Messaging extends Action 'twilio' => [ 'accountSid' => $user, 'authToken' => $password, - // Twilio Messaging Service SIDs always start with MG - // https://www.twilio.com/docs/messaging/services - 'messagingServiceSid' => \str_starts_with($from, 'MG') ? $from : null + // Messaging Service SIDs are always 34 characters; alphanumeric sender IDs, at most 11, can also start with MG + // https://www.twilio.com/docs/messaging/api/service-resource + 'messagingServiceSid' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? $from : null ], 'textmagic' => [ 'username' => $user, @@ -1313,7 +1313,7 @@ class Messaging extends Action }, 'options' => match ($host) { 'twilio' => [ - 'from' => \str_starts_with($from, 'MG') ? null : $from + 'from' => \str_starts_with($from, 'MG') && \strlen($from) === 34 ? null : $from ], default => [ 'from' => $from diff --git a/tests/e2e/Services/Messaging/MessagingBase.php b/tests/e2e/Services/Messaging/MessagingBase.php index 35e8b933c5..d0a4f59140 100644 --- a/tests/e2e/Services/Messaging/MessagingBase.php +++ b/tests/e2e/Services/Messaging/MessagingBase.php @@ -1022,6 +1022,49 @@ trait MessagingBase $this->assertEquals("Invalid `serviceAccountJSON` param: FCM service account JSON must include a non-empty 'client_email' field, which identifies the service account used for authentication. or null", $response['body']['message']); } + public function testCreateTwilioProviderAlphanumericSender(): void + { + $response = $this->client->call(Client::METHOD_POST, '/messaging/providers/twilio', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'Twilio', + 'accountSid' => 'my-accountSid', + 'authToken' => 'my-authToken', + 'from' => 'Appwrite', + ]); + + $this->assertEquals(201, $response['headers']['status-code']); + $this->assertEquals('Appwrite', $response['body']['options']['from']); + } + + public function testUpdateTwilioProviderAlphanumericSender(): void + { + $provider = $this->client->call(Client::METHOD_POST, '/messaging/providers/twilio', [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'providerId' => ID::unique(), + 'name' => 'Twilio', + ]); + + $this->assertEquals(201, $provider['headers']['status-code']); + + $response = $this->client->call(Client::METHOD_PATCH, '/messaging/providers/twilio/' . $provider['body']['$id'], [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ], [ + 'from' => 'Appwrite', + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + $this->assertEquals('Appwrite', $response['body']['options']['from']); + } + public function testUpdateProviderMissingCredentialsThrows(): void { // Create new FCM provider with no serviceAccountJSON