-
Notifications
You must be signed in to change notification settings - Fork 65
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
Changes from 6 commits
c31d6f1
c5eab46
f1ea63c
8f8611e
3f34bac
6867085
3c5a61c
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,83 @@ | ||
| <?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 $apiToken, | ||
| private string $senderId | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return static::NAME; | ||
|
Contributor
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. why static? Why not
Member
Author
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.
Member
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. self::NAME will return the base class value even in child classes, better to use static in most cases |
||
| } | ||
|
|
||
| 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() | ||
| ); | ||
|
|
||
| fwrite(STDOUT, json_encode($recipients, JSON_PRETTY_PRINT)); | ||
|
||
|
|
||
| $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(); | ||
| } | ||
| } | ||
| 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( | ||
| apiToken: \getenv('INFORU_API_TOKEN'), | ||
| senderId: \getenv('INFORU_SENDER_ID') | ||
| ); | ||
|
|
||
| $message = new SMS( | ||
| to: ['0541234567'], | ||
| content: 'Test Content', | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
|
|
||
| $this->assertResponse($response); | ||
| } | ||
| } |

Uh oh!
There was an error while loading. Please reload this page.