-
Notifications
You must be signed in to change notification settings - Fork 66
feat: inforu sms adapter #96
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c31d6f1
feat: inforu sms adapter
ChiragAgg5k c5eab46
chore: update readme
ChiragAgg5k f1ea63c
chore: add env variables
ChiragAgg5k 8f8611e
chore: finish the adapter tests
ChiragAgg5k 3f34bac
fix: env format
ChiragAgg5k 6867085
chore: update to use senderId
ChiragAgg5k 3c5a61c
chore: review
ChiragAgg5k 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
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
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
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
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,81 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS as SMSAdapter; | ||
| use Utopia\Messaging\Messages\SMS as SMSMessage; | ||
| use Utopia\Messaging\Response; | ||
|
|
||
| // Reference Material | ||
| // https://apidoc.inforu.co.il/#d7b3f69d-7422-44b4-b7d1-0959f8a08881 | ||
| class Inforu extends SMSAdapter | ||
| { | ||
| protected const NAME = 'Inforu'; | ||
|
|
||
| /** | ||
| * @param string $apiToken Inforu API token | ||
| * @param string $senderId Sender ID | ||
| */ | ||
| public function __construct( | ||
| private string $senderId, | ||
| private string $apiToken, | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| return 100; // Setting a conservative limit, adjust if needed | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMSMessage $message): array | ||
| { | ||
| $response = new Response($this->getType()); | ||
|
|
||
| $recipients = array_map( | ||
| fn ($number) => ['Phone' => ltrim($number, '+')], | ||
| $message->getTo() | ||
| ); | ||
|
|
||
| $result = $this->request( | ||
| method: 'POST', | ||
| url: 'https://capi.inforu.co.il/api/v2/SMS/SendSms', | ||
| headers: [ | ||
| 'Content-Type: application/json', | ||
| 'Authorization: Basic ' . $this->apiToken, | ||
| ], | ||
| body: [ | ||
| 'Data' => [ | ||
| 'Message' => $message->getContent(), | ||
| 'Recipients' => $recipients, | ||
| 'Settings' => [ | ||
| 'Sender' => $this->senderId, | ||
| ], | ||
| ], | ||
| ], | ||
| ); | ||
|
|
||
| if ($result['statusCode'] === 200 && ($result['response']['StatusId'] ?? 0) === 1) { | ||
christyjacob4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $response->setDeliveredTo(count($message->getTo())); | ||
| foreach ($message->getTo() as $to) { | ||
| $response->addResult($to); | ||
| } | ||
| } else { | ||
| $errorMessage = $result['response']['StatusDescription'] ?? 'Unknown error'; | ||
| foreach ($message->getTo() as $to) { | ||
| $response->addResult($to, $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,30 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Tests\Adapter\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapter\SMS\Inforu; | ||
| use Utopia\Messaging\Messages\SMS; | ||
| use Utopia\Tests\Adapter\Base; | ||
|
|
||
| class InforuTest extends Base | ||
| { | ||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public function testSendSMS(): void | ||
| { | ||
| $sender = new Inforu( | ||
| senderId: \getenv('INFORU_SENDER_ID'), | ||
| apiToken: \getenv('INFORU_API_TOKEN'), | ||
| ); | ||
|
|
||
| $message = new SMS( | ||
| to: ['0541234567'], | ||
| content: 'Test Content', | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
| } |
Oops, something went wrong.
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.
why static? Why not
self::name?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.
thats what we do for Fast2SMS and others
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.
self::NAME will return the base class value even in child classes, better to use static in most cases