-
Notifications
You must be signed in to change notification settings - Fork 68
fix: rebase PR #99 - SMSGateApp SMS adapter #140
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| <?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 SMSGateApp extends SMSAdapter | ||
| { | ||
| protected const NAME = 'SMS Gateway for Android™'; | ||
| protected const DEFAULT_API_ENDPOINT = 'https://api.sms-gate.app/3rdparty/v1'; | ||
|
|
||
| public function __construct( | ||
| private readonly string $apiUsername, | ||
| private readonly string $apiPassword, | ||
| private readonly string $apiEndpoint = self::DEFAULT_API_ENDPOINT, | ||
| ) { | ||
| parent::__construct(); | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 10; | ||
| } | ||
|
|
||
| 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); | ||
| } else { | ||
| $errorMessage = $result['response']['message'] ?? 'Unknown error'; | ||
| foreach ($message->getTo() as $recipient) { | ||
| $response->addResult($recipient, $errorMessage); | ||
| } | ||
| } | ||
|
|
||
| return $response->toArray(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| <?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 | ||
| { | ||
| public function testSendSMS(): void | ||
| { | ||
| $username = \getenv('SMSGATEAPP_USERNAME'); | ||
| $password = \getenv('SMSGATEAPP_PASSWORD'); | ||
| $to = \getenv('SMSGATEAPP_TO'); | ||
|
|
||
| if (!$username || !$password || !$to) { | ||
| $this->markTestSkipped('SMSGateApp credentials not configured'); | ||
| } | ||
|
|
||
| $endpoint = \getenv('SMSGATEAPP_ENDPOINT') ?: null; | ||
|
|
||
| $sender = new SMSGateApp($username, $password, $endpoint); | ||
|
|
||
| $message = new SMS( | ||
| to: [$to], | ||
| content: 'Test content from SMSGateApp' | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
When the credentials and recipient are configured but
SMSGATEAPP_ENDPOINTis unset, the test explicitly passesnullto the non-nullablestringconstructor parameter, causing aTypeErrorinstead of usingDEFAULT_API_ENDPOINTand sending the SMS.Prompt To Fix With AI