-
Notifications
You must be signed in to change notification settings - Fork 66
Added SmsGlobal messaging adapter #40
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
Open
biswajit287
wants to merge
3
commits into
utopia-php:main
Choose a base branch
from
biswajit287:feat-6403-SmsGlobal-messaging-adapter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
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
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,146 @@ | ||
| <?php | ||
|
|
||
| namespace Utopia\Messaging\Adapters\SMS; | ||
|
|
||
| use Utopia\Messaging\Adapters\SMS as SMSAdapter; | ||
| use Utopia\Messaging\Messages\SMS; | ||
|
|
||
| // Reference Material | ||
| // https://www.smsglobal.com/rest-api/ | ||
| class SmsGlobal extends SMSAdapter | ||
| { | ||
| /** | ||
| * Hash Algorithm for API Authentication | ||
| */ | ||
| const HASH_ALGO = 'sha256'; | ||
|
|
||
| /** | ||
| * @param string $apiKey REST API key from MXT https://mxt.smsglobal.com/integrations | ||
| * @param string $apiSecret REST API Secret from MXT https://mxt.smsglobal.com/integrations | ||
| */ | ||
| public function __construct( | ||
| private string $apiKey, | ||
| private string $apiSecret | ||
| ) { | ||
| } | ||
|
|
||
| public function getName(): string | ||
| { | ||
| return 'SmsGlobal'; | ||
| } | ||
|
|
||
| public function getMaxMessagesPerRequest(): int | ||
| { | ||
| //TODO:: Didn't find the limit for REST API in SmsGlobal documentation | ||
| return 1000; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| * | ||
| * @throws \Exception | ||
| */ | ||
| protected function process(SMS $message): string | ||
| { | ||
| $authorizationHeader = $this->getAuthorizationHeader( | ||
| method: 'POST', | ||
| requestUri: '/v2/sms/', | ||
| host: 'api.smsglobal.com' | ||
| ); | ||
|
|
||
| return $this->request( | ||
| method: 'POST', | ||
| url: "https://api.smsglobal.com/v2/sms/", | ||
| headers: [ | ||
| 'Authorization: ' . $authorizationHeader, | ||
| 'Content-Type: application/json', | ||
| ], | ||
| body: \json_encode( | ||
| $this->getRequestBody( | ||
| to: $message->getTo(), | ||
| text: $message->getContent(), | ||
| from: $message->getFrom() | ||
| ) | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Get the value to use for the Authorization header | ||
| * | ||
| * @param string $method HTTP method (e.g. POST) | ||
| * @param string $requestUri Request URI (e.g. /v2/sms/) | ||
| * @param string $host Hostname | ||
| * @return string | ||
| */ | ||
| public function getAuthorizationHeader(string $method, string $requestUri, string $host): string | ||
| { | ||
| // Server or computer time should match with the current Unix timestamp otherwise authentication will fail | ||
| $timestamp = time(); | ||
| $nonce = md5(microtime() . mt_rand()); | ||
|
|
||
| $hash = $this->getRequestHash( | ||
| timestamp: $timestamp, | ||
| nonce: $nonce, | ||
| method: $method, | ||
| requestUri: $requestUri, | ||
| host: $host | ||
| ); | ||
|
|
||
| $headerFormat = 'MAC id="%s", ts="%s", nonce="%s", mac="%s"'; | ||
| $header = sprintf($headerFormat, $this->apiKey, $timestamp, $nonce, $hash); | ||
| return $header; | ||
| } | ||
|
|
||
| /** | ||
| * Hashes a request using the API secret, to use in the Authorization header | ||
| * | ||
| * @param int $timestamp Unix timestamp of request time | ||
| * @param string $nonce Random unique string | ||
| * @param string $method HTTP method (e.g. POST) | ||
| * @param string $requestUri Request URI (e.g. /v1/sms/) | ||
| * @param string $host Hostname | ||
| * @param int $port Port (e.g. 443) | ||
| * @return string | ||
| */ | ||
| private function getRequestHash( | ||
| int $timestamp, | ||
| string $nonce, | ||
| string $method, | ||
| string $requestUri, | ||
| string $host, | ||
| int $port = 443 | ||
| ) { | ||
| $string = array($timestamp, $nonce, $method, $requestUri, $host, $port, ''); | ||
| $string = sprintf("%s\n", implode("\n", $string)); | ||
| $hash = hash_hmac(self::HASH_ALGO, $string, $this->apiSecret, true); | ||
| $hash = base64_encode($hash); | ||
| return $hash; | ||
| } | ||
|
|
||
| /** | ||
| * Get the request body | ||
| * | ||
| * @param array $to | ||
| * @param string $text | ||
| * @param string|null $from | ||
| * @return array | ||
| */ | ||
| private function getRequestBody(array $to, string $text, string $from = null): array | ||
| { | ||
| $origin = !empty($from) ? $from : ''; | ||
| if (count($to) == 1) { | ||
| return [ | ||
| "destination" => $to[0], | ||
| "message" => $text, | ||
| "origin" => $origin, | ||
| ]; | ||
| } else { | ||
| return [ | ||
| "destinations" => $to, | ||
| "message" => $text, | ||
| "origin" => $origin, | ||
| ]; | ||
| } | ||
| } | ||
| } |
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,36 @@ | ||
| <?php | ||
|
|
||
| namespace Tests\E2E; | ||
|
|
||
| use Utopia\Messaging\Adapters\SMS\SmsGlobal; | ||
| use Utopia\Messaging\Messages\SMS; | ||
|
|
||
| class SmsGlobalTest extends Base | ||
| { | ||
| /** | ||
| * @throws \Exception | ||
| */ | ||
| public function testSendSMS() | ||
| { | ||
| $apiKey = getenv('SMS_GLOBAL_API_KEY'); | ||
| $apiSecret = getenv('SMS_GLOBAL_API_SECRET'); | ||
|
|
||
| $to = [getenv('SMS_GLOBAL_TO')]; | ||
| $from = getenv('SMS_GLOBAL_FROM'); | ||
|
|
||
| $sender = new SmsGlobal($apiKey, $apiSecret); | ||
| $message = new SMS( | ||
| to: $to, | ||
| content: 'Test Content', | ||
| from: $from | ||
| ); | ||
|
|
||
| $response = $sender->send($message); | ||
| $result = \json_decode($response, true); | ||
|
|
||
| $this->assertArrayHasKey('messages', $result); | ||
| $this->assertEquals(count($to), count($result['messages'])); | ||
|
|
||
| // $dummyResponseStructure = '{"messages":[{"id":"154","outgoing_id":1,"origin":"origin","destination":"destination","message":"Test Content","status":"sent","dateTime":""}]}'; | ||
| } | ||
| } | ||
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.
Please remove this comment if it's not needed.
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.
Hi @gewenyu99, removed the comment.