简体   繁体   English

获取 AWS SMS 的发送状态

[英]Getting delivery status of AWS SMS

I'm sending sms from AWS through the node SDK.我正在通过节点 SDK 从 AWS 发送短信。 SMS are going out well and I'm trying to get delivery informations. SMS 运行良好,我正在尝试获取送货信息。 Apparently it's not that easy and one has to setup SNS to send logs to Cloudwatch and to parse CloudWatch to get the delivery information looking up the MessageId: https://stackoverflow.com/a/40327061/2054629显然这不是那么容易,必须设置 SNS 以将日志发送到 Cloudwatch 并解析 CloudWatch 以获取查找 MessageId 的传递信息: https ://stackoverflow.com/a/40327061/2054629

If I send sms through SNS web interface, logs I see logs in cloudwatch, but not when I send them through the node SDK.如果我通过 SNS Web 界面发送短信,我会在 cloudwatch 中看到日志,但当我通过节点 SDK 发送它们时却看不到。 I could not get information on how to setup things before sending them from node.在从节点发送它们之前,我无法获得有关如何设置它们的信息。

Ideally, I want to achieve something like:理想情况下,我想实现如下目标:

const sendSMS = async (message, number) => {
    // send the SMS
    // wait to get delivery info
    // resolve with delivery info, or reject if failed
}

Currently I have:目前我有:

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);
    });  
  });
}

which only send a SMS request to AWS and resolves with something like它只向 AWS 发送 SMS 请求并解析为类似

{
  ResponseMetadata: { RequestId: '7e0999a3-xxxx-xxxx-xxxx-xxxxxxxxxxxx' },
  MessageId: 'f7f21871-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}

I'm not sure if one has to setup an SNS application to be able to get logs or not, and I'd rather not to keep things simple.我不确定是否必须设置一个 SNS 应用程序才能获取日志,我不想让事情变得简单。

You might have already done this but to configure cloudwatch logs for SMS deliveries, you have to configure SMS preferences.您可能已经这样做了,但是要为 SMS 传送配置 cloudwatch 日志,您必须配置 SMS 首选项。 For that you need to create an IAM role to allow cloudwatch logs access.为此,您需要创建一个 IAM 角色以允许访问 cloudwatch 日志。 It is very simple to do it through AWS console.通过 AWS 控制台进行操作非常简单。 The steps are given at http://docs.aws.amazon.com/sns/latest/dg/sms_preferences.html这些步骤在http://docs.aws.amazon.com/sns/latest/dg/sms_preferences.html中给出

You can even control what percentage of successful deliveries + failed SMSs are logged if you want.如果需要,您甚至可以控制记录成功交付+失败短信的百分比。 Once this is done, you should start seeing cloudwatch logs whichever way you sent the SMS.完成此操作后,您应该开始看到 cloudwatch 日志,无论您以何种方式发送 SMS。

I wanted to add this as a comment but I don't have enough rep.我想将其添加为评论,但我没有足够的代表。 I'll delete this answer if it doesn't work.如果它不起作用,我会删除这个答案。

Your code seems to work just fine with nodejs v6 by changing the import statement (lack of ES5/6 support).通过更改 import 语句(缺少 ES5/6 支持),您的代码似乎与 nodejs v6 一起工作得很好。 After enabling logging to cloudwatch, every SMS (both through the Web interface and this code) creates a log steam in CloudWatch logs.启用到 cloudwatch 的日志记录后,每个 SMS(通过 Web 界面和此代码)都会在 CloudWatch 日志中创建一个日志流。 I think you should reinstall the AWS SDK or avoid using ES5/6 to make the SDK work correctly.我认为您应该重新安装 AWS SDK 或避免使用 ES5/6 以使 SDK 正常工作。

For the second question, if the message wasn't delivered, you would get an error:对于第二个问题,如果消息未送达,您将收到错误消息:

(err, res) => {
      if (err) { return reject(err); }
      resolve(res);
    }); 

if the message was successfully sent, you get a response like:如果消息发送成功,您会收到如下响应:

{ ResponseMetadata: { RequestId: 'e31feda6-669c-5b13-XXX-bc25b07877b5' },
  MessageId: '53555115-6acb-5684-XXXX-0096bc2f6a22' }

为什么不使用像Send With SES( https://www.sendwithses.com/ )那样的包装器来为您收集交付统计信息,而不是与AWS纠缠不清?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM