我'm sending sms from AWS through the node SDK. SMS are going out well and I'我试图获得交付信息 . 显然它不是那么容易,必须设置SNS将日志发送到Cloudwatch并解析CloudWatch以获取查询MessageId的传递信息:https://stackoverflow.com/a/40327061/2054629
如果我通过SNS Web界面发送短信,日志我会看到cloudwatch中的日志,但是当我通过节点SDK发送它们时 . 在从节点发送之前,我无法获得有关如何设置内容的信息 .
理想情况下,我希望实现以下目标:
const sendSMS = async (message, number) => {
// send the SMS
// wait to get delivery info
// resolve with delivery info, or reject if failed
}
目前我有:
import AWS from 'aws-sdk';
AWS.config.update({
accessKeyId: accessKey,
secretAccessKey: secretKey,
region: 'us-east-1',
});
const sns = new AWS.SNS();
const sendSMS = async (message, number) => {
return await new Promise((resolve, reject) => {
sns.publish({
Message: message,
MessageStructure: 'string',
PhoneNumber: number,
}, (err, res) => {
if (err) { return reject(err); }
resolve(res);
});
});
}
它只向AWS发送一个SMS请求并通过类似的方式解析
{
ResponseMetadata: { RequestId: '7e0999a3-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
MessageId: 'f7f21871-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}
我不确定是否必须设置SNS应用程序才能获取日志,我宁愿不要保持简单 .
2 回答
通过更改import语句(缺少ES5 / 6支持),您的代码似乎可以与nodejs v6一起使用 . 启用日志记录到cloudwatch后,每条SMS(通过Web界面和此代码)都会在CloudWatch日志中创建日志记录 . 我认为您应该重新安装AWS SDK或避免使用ES5 / 6来使SDK正常工作 .
对于第二个问题,如果邮件未送达,您将收到错误消息:
如果邮件成功发送,您会收到如下响应:
您可能已经这样做但是要为SMS交付配置cloudwatch日志,您必须配置SMS首选项 . 为此,您需要创建一个IAM角色以允许cloudwatch日志访问 . 通过AWS控制台执行此操作非常简单 . 步骤在http://docs.aws.amazon.com/sns/latest/dg/sms_preferences.html给出
如果需要,您甚至可以控制记录SMS的成功交付百分比失败 . 完成此操作后,无论您发送SMS的方式如何,都应该开始查看cloudwatch日志 .
我想将此添加为评论,但我没有足够的代表 . 如果它不起作用,我会删除这个答案 .