ERROR sending a SMS with Amazon SNS and Python and boto3

落爺英雄遲暮 提交于 2020-12-13 03:49:36

问题


The documentation suggests to use the script below but I can't seem to figure out why im getting an error message.

This what im using so far:

sns = boto3.client('sns', region_name='eu-west-1')
sns.publish(
  PhoneNumber='+5521981554856',
  Message='hi there',
  MessageAttributes={
                        'AWS.SNS.SMS.SenderID': {
                                                     'DataType': 'String',
                                                     'StringValue': 'MySenderID'   
}    
}   
)  

does anyone knows why im getting the error msg below?

raise ParamValidationError(report=report.generate_report())
ParamValidationError: Parameter validation failed:
Unknown parameter in input: "PhoneNumber", must be one of: TopicArn,TargetArn, Message, Subject, MessageStructure, MessageAttributes

why "PhoneNumber " is presenting such an awkward behaviour?


回答1:


I was able to get it working with the following code:

import boto3

sns = boto3.client('sns')
smsattrs = {
    'AWS.SNS.SMS.SenderID': { 'DataType': 'String', 'StringValue': 'TestSender' },
    'AWS.SNS.SMS.SMSType': { 'DataType': 'String', 'StringValue': 'Transactional'}
}
sns.publish(
    PhoneNumber = '+35840xxxxxxx',
    Message = 'Hello world!',
    MessageAttributes = smsattrs
)

The biggest difference I see is that you have not set AWS.SNS.SMS.SMSType.




回答2:


Documentation says that PhoneNumber is supported. http://boto3.readthedocs.io/en/latest/reference/services/sns.html

Sadly, that's just copy-paste from official AWS documentation.

If you look at source code, you'll see that Boto3 expects either TargetArn or TopicArn: https://github.com/boto/boto3/blob/master/boto3/data/sns/2010-03-31/resources-1.json

"Publish": {
      "request": {
        "operation": "Publish",
        "params": [
          { "target": "TopicArn", "source": "identifier", "name": "Arn" }
        ]
      }
    },

...

"Publish": {
          "request": {
            "operation": "Publish",
            "params": [
              { "target": "TargetArn", "source": "identifier", "name": "Arn" }
            ]
          }
        },

So I guess you have to patch Boto3 yourself or file an issue on GitHub.



来源:https://stackoverflow.com/questions/41045868/error-sending-a-sms-with-amazon-sns-and-python-and-boto3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!