当我的应用用户使用他们的电话号码注册时,我想向他们的电话号码发送欢迎消息 (SMS)。我找不到有关此特定任务的官方文档。
最佳答案
Amazon 让您可以做到这一点。假设您使用 Cognito 进行注册,您将需要使用确认后 Cognito lambda 触发器。
通过 AWS 控制台设置您的 SNS 账户,以发送 SMS 消息。通过控制台向自己发送一条测试消息。
运行
amplify auth update
当遇到问题
Do you want to configure Lambda Triggers for Cognito?
时,回答Yes并选择Post Confirmation
触发您需要向 lambda 授予 SNS (SMS) 权限。更新
PostConfirmation-cloudformation-template.json
文件以在Resources.lambdaexecutionpolicy.Properties.PolicyDocument.Statement
下添加新语句:{ "Resources": { "lambdaexecutionpolicy": { "Properties": { "PolicyDocument": { "Statement": [ { "Effect": "Allow", "Action": "sns:*", "Resource": "*" } ] ... } ... } ... } ... } ... }
将此代码用于触发器:
var aws = require('aws-sdk'); var sms = new aws.SNS(); exports.handler = (event, context, callback) => { console.log(event); if (event.request.userAttributes.phone_number) { sendSMS(event.request.userAttributes.phone_number, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) { // Return to Amazon Cognito callback(null, event); }); } else { // Nothing to do, the user's phone number is unknown callback(null, event); } }; function sendSMS(to, message, completedCallback) { const params = { Message: message, /* required */ PhoneNumber: to }; sns.publish(params, function (err, data) { if (err) { console.log(err, err.stack); // an error occurred } else { console.log(data); } completedCallback("SMS Sent"); }) };
关于aws-amplify - 如何使用 Amazon Amplify SDK 以编程方式为我的 Android 应用程序用户发送短信?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60430828/