-
Notifications
You must be signed in to change notification settings - Fork 66
feat: Add SMSGateApp adapter #99
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,86 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| <?php | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace Utopia\Messaging\Adapter\SMS; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Adapter\SMS as SMSAdapter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| use Utopia\Messaging\Response; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * SMSGateApp adapter class. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class SMSGateApp extends SMSAdapter { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected const NAME = 'SMS Gateway for Android™'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected const DEFAULT_API_ENDPOINT = 'https://api.sms-gate.app/3rdparty/v1'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $apiUsername SMSGate username | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string $apiPassword SMSGate password | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param string|null $apiEndpoint SMSGate API endpoint | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function __construct( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private string $apiUsername, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private string $apiPassword, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private ?string $apiEndpoint = null, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->apiEndpoint = $this->apiEndpoint ?: self::DEFAULT_API_ENDPOINT; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function getName(): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return static::NAME; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public function getMaxMessagesPerRequest(): int { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return 10; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| protected function process(SMSMessage $message): array { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $response = new Response($this->getType()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $body = [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'textMessage' => [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'text' => $message->getContent(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'phoneNumbers' => $message->getTo(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = $this->request( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| method: 'POST', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| url: $this->apiEndpoint . '/messages?skipPhoneValidation=true', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| headers: [ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Content-Type: application/json', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 'Authorization: Basic ' . base64_encode("{$this->apiUsername}:{$this->apiPassword}"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| body: $body, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ($result['statusCode'] === 202) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $success = 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ($result['response']['recipients'] as $recipient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $response->addResult($recipient['phoneNumber'], $recipient['error'] ?? ''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if ($recipient['state'] !== 'Failed') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $success++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $response->setDeliveredTo($success); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+66
to
+76
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. 🛠️ Refactor suggestion Add defensive programming for response structure. The code assumes the response will have a Add validation for the response structure: if ($result['statusCode'] === 202) {
+ if (!isset($result['response']['recipients']) || !is_array($result['response']['recipients'])) {
+ foreach ($message->getTo() as $recipient) {
+ $response->addResult($recipient, 'Invalid API response structure');
+ }
+ return $response->toArray();
+ }
+
$success = 0;
foreach ($result['response']['recipients'] as $recipient) {
+ if (!isset($recipient['phoneNumber'])) {
+ continue;
+ }
$response->addResult($recipient['phoneNumber'], $recipient['error'] ?? '');
- if ($recipient['state'] !== 'Failed') {
+ if (isset($recipient['state']) && $recipient['state'] !== 'Failed') {
$success++;
}
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $errorMessage = $result['response']['message'] ?? 'Unknown error'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| foreach ($message->getTo() as $recipient) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| $response->addResult($recipient, $errorMessage); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return $response->toArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS\SMSGateApp; | ||
| use Utopia\Messaging\Messages\SMS; | ||
| use Utopia\Tests\Adapter\Base; | ||
|
|
||
| class SMSGateAppTest extends Base { | ||
| /** | ||
| * Test sending SMS message with SMSGateApp | ||
| */ | ||
| public function testSendSMS(): void { | ||
| // Environment variables for credentials | ||
| $username = \getenv('SMSGATEAPP_USERNAME'); | ||
| $password = \getenv('SMSGATEAPP_PASSWORD'); | ||
| $to = \getenv('SMSGATEAPP_TO'); | ||
|
|
||
| if (!$username || !$password || !$to) { | ||
| $this->markTestSkipped('SMSGateApp credentials not configured'); | ||
| } | ||
|
|
||
| // Optional API endpoint if set | ||
| $endpoint = \getenv('SMSGATEAPP_ENDPOINT') ?? null; | ||
|
|
||
| // Instantiate SMSGateApp | ||
| $sender = new SMSGateApp($username, $password, $endpoint); | ||
|
|
||
| // Create SMS message with required 'to' parameter | ||
| $message = new SMS( | ||
| to: [$to], | ||
| content: 'Test content from SMSGateApp' | ||
| ); | ||
|
|
||
| // Call send() and verify response | ||
| $response = $sender->send($message); | ||
|
|
||
| // Assertion to match expected response formatting | ||
| $this->assertResponse($response); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.