-
Notifications
You must be signed in to change notification settings - Fork 68
feat: add Alibaba Cloud SMS adapter (#6307) #109
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,114 @@ | ||||||||||||||||||||||||||||||||||
| <?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 AlibabaCloud extends SMSAdapter | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| protected const NAME = 'AlibabaCloud'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * @param string $accessKeyId Alibaba Cloud Access Key ID | ||||||||||||||||||||||||||||||||||
| * @param string $accessKeySecret Alibaba Cloud Access Key Secret | ||||||||||||||||||||||||||||||||||
| * @param string $signName Alibaba Cloud SMS Sign Name | ||||||||||||||||||||||||||||||||||
| * @param string $templateCode Alibaba Cloud SMS Template Code | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public function __construct( | ||||||||||||||||||||||||||||||||||
| private string $accessKeyId, | ||||||||||||||||||||||||||||||||||
| private string $accessKeySecret, | ||||||||||||||||||||||||||||||||||
| private string $signName, | ||||||||||||||||||||||||||||||||||
| private string $templateCode, | ||||||||||||||||||||||||||||||||||
| ) { | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Get adapter name. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public function getName(): string | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| return static::NAME; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Get max messages per request. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| public function getMaxMessagesPerRequest(): int | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| return 1; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * {@inheritdoc} | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| protected function process(SMSMessage $message): array | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| $response = new Response($this->getType()); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| foreach ($message->getTo() as $to) { | ||||||||||||||||||||||||||||||||||
| $params = [ | ||||||||||||||||||||||||||||||||||
| 'AccessKeyId' => $this->accessKeyId, | ||||||||||||||||||||||||||||||||||
| 'Action' => 'SendSms', | ||||||||||||||||||||||||||||||||||
| 'Format' => 'JSON', | ||||||||||||||||||||||||||||||||||
| 'PhoneNumbers' => $to, | ||||||||||||||||||||||||||||||||||
| 'RegionId' => 'cn-hangzhou', | ||||||||||||||||||||||||||||||||||
| 'SignName' => $this->signName, | ||||||||||||||||||||||||||||||||||
| 'SignatureMethod' => 'HMAC-SHA1', | ||||||||||||||||||||||||||||||||||
| 'SignatureNonce' => \uniqid(), | ||||||||||||||||||||||||||||||||||
|
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.
Suggested change
|
||||||||||||||||||||||||||||||||||
| 'SignatureVersion' => '1.0', | ||||||||||||||||||||||||||||||||||
| 'TemplateCode' => $this->templateCode, | ||||||||||||||||||||||||||||||||||
| 'TemplateParam' => \json_encode(['code' => $message->getContent()]), | ||||||||||||||||||||||||||||||||||
|
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.
The adapter always wraps the message content under the key |
||||||||||||||||||||||||||||||||||
| 'Timestamp' => \gmdate('Y-m-d\TH:i:s\Z'), | ||||||||||||||||||||||||||||||||||
| 'Version' => '2017-05-25', | ||||||||||||||||||||||||||||||||||
| ]; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| $params['Signature'] = $this->generateSignature($params); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| $result = $this->request( | ||||||||||||||||||||||||||||||||||
| method: 'GET', | ||||||||||||||||||||||||||||||||||
| url: 'https://dysmsapi.aliyuncs.com', | ||||||||||||||||||||||||||||||||||
| headers: [], | ||||||||||||||||||||||||||||||||||
| body: $params | ||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if ($result['statusCode'] >= 200 && $result['statusCode'] < 300 && ($result['response']['Code'] ?? '') === 'OK') { | ||||||||||||||||||||||||||||||||||
| $response->incrementDeliveredTo(); | ||||||||||||||||||||||||||||||||||
| $response->addResult($to); | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| $response->addResult($to, $result['response']['Message'] ?? 'Unknown error'); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return $response->toArray(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Generate Alibaba Cloud API Signature. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| private function generateSignature(array $params): string | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| \ksort($params); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| $canonicalizedQueryString = ''; | ||||||||||||||||||||||||||||||||||
| foreach ($params as $key => $value) { | ||||||||||||||||||||||||||||||||||
| $canonicalizedQueryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($value); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| $stringToSign = 'GET&' . $this->percentEncode('/') . '&' . $this->percentEncode(\substr($canonicalizedQueryString, 1)); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| $signature = \base64_encode(\hash_hmac('sha1', $stringToSign, $this->accessKeySecret . '&', true)); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return $signature; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private function percentEncode(string $str): string | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| $res = \urlencode($str); | ||||||||||||||||||||||||||||||||||
| $res = \str_replace(['+', '*'], ['%20', '%2A'], $res); | ||||||||||||||||||||||||||||||||||
| $res = \preg_replace('/%7E/i', '~', $res); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return $res; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+106
to
+113
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.
If 🛡️ Proposed fix private function percentEncode(string $str): string
{
$res = \urlencode($str);
$res = \str_replace(['+', '*'], ['%20', '%2A'], $res);
- $res = \preg_replace('/%7E/i', '~', $res);
+ $res = \preg_replace('/%7E/i', '~', $res) ?? $res;
return $res;
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||
| <?php | ||||||||||||||||||
|
|
||||||||||||||||||
| namespace Utopia\Tests\Adapter\SMS; | ||||||||||||||||||
|
|
||||||||||||||||||
| use Utopia\Messaging\Adapter\SMS\AlibabaCloud; | ||||||||||||||||||
| use Utopia\Messaging\Messages\SMS; | ||||||||||||||||||
| use Utopia\Tests\Adapter\Base; | ||||||||||||||||||
|
|
||||||||||||||||||
| class AlibabaCloudTest extends Base | ||||||||||||||||||
| { | ||||||||||||||||||
| /** | ||||||||||||||||||
| * @throws \Exception | ||||||||||||||||||
| */ | ||||||||||||||||||
| public function testSendSMS(): void | ||||||||||||||||||
| { | ||||||||||||||||||
| $sender = new AlibabaCloud( | ||||||||||||||||||
| \getenv('ALIBABA_CLOUD_ACCESS_KEY_ID'), | ||||||||||||||||||
| \getenv('ALIBABA_CLOUD_ACCESS_KEY_SECRET'), | ||||||||||||||||||
| \getenv('ALIBABA_CLOUD_SIGN_NAME'), | ||||||||||||||||||
| \getenv('ALIBABA_CLOUD_TEMPLATE_CODE') | ||||||||||||||||||
| ); | ||||||||||||||||||
|
|
||||||||||||||||||
| $message = new SMS( | ||||||||||||||||||
| to: [\getenv('ALIBABA_CLOUD_TO')], | ||||||||||||||||||
| content: \json_encode(['code' => '123456']), | ||||||||||||||||||
| ); | ||||||||||||||||||
|
Comment on lines
+23
to
+26
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. Message content format may cause double JSON encoding. The test passes If the adapter fix to pass content directly as 🐛 Proposed fix (if adapter is updated to use content directly) $message = new SMS(
to: [\getenv('ALIBABA_CLOUD_TO')],
- content: \json_encode(['code' => '123456']),
+ content: '{"code":"123456"}',
);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents
Comment on lines
+23
to
+26
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.
The test sets |
||||||||||||||||||
|
|
||||||||||||||||||
| $result = $sender->send($message); | ||||||||||||||||||
|
|
||||||||||||||||||
| $this->assertResponse($result); | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
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.
RegionIdis hardcoded to'cn-hangzhou'The Alibaba Cloud SMS API supports multiple regions (e.g.,
ap-southeast-1for Southeast Asia,eu-central-1for Europe). Hardcodingcn-hangzhoumeans all traffic is routed through mainland China, which may incur higher latency and potential regulatory concerns for international deployments. Consider acceptingregionIdas an optional constructor parameter so callers can choose the appropriate datacenter.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!