How to send an SMS with custom sender ID with Amazon SNS and Python and boto3

让人想犯罪 __ 提交于 2019-12-03 14:53:16

问题


The documentation suggests to use message attributes for that but I can't seem to figure out what attribute name to use.

This works so far:

sns = boto3.client('sns', region_name='eu-west-1')

sns.publish(
  PhoneNumber='+491701234567',
  Message='hi there',
  MessageAttributes={
    'AWS.SNS.SMS.SenderID': {
      'DataType': 'String',
      'StringValue': 'MySenderID'   
    }    
  }   
)  

The SMS is delivered but with some (random?) value in the sender id field. So it seems my setting of message attributes is silently ignored. What is the correct way to set a custom sender id?


回答1:


The sender id must be 1-11 alpha-numeric characters, no spaces; for example:

  • THISISME - ✅
  • TestForSO - ✅
  • StackOverflow - 🛑 (too long. max 11 chars)
  • Some one - 🛑 (no spaces)

As others mentioned, the sender id customization depends on the country / cellular provider so make sure to test it.

Example snippet

import boto3

access_key = '....'
secret = '....'
region = "us-east-1"

number = '+972...<your number>'

sender_id = 'TestForSO'
sms_message = 'Your code: 123456'

sns = boto3.client('sns', aws_access_key_id=access_key, aws_secret_access_key=secret, region_name=region)
sns.publish(PhoneNumber=number, Message=sms_message, MessageAttributes={'AWS.SNS.SMS.SenderID': {'DataType': 'String', 'StringValue': sender_id}, 'AWS.SNS.SMS.SMSType': {'DataType': 'String', 'StringValue': 'Promotional'}})




回答2:


Check if your destination country supports sender IDs

http://docs.aws.amazon.com/sns/latest/dg/sms_supported-countries.html




回答3:


As noted by Adam Owczarczyk, some countries don't allow you to transmit a sender ID in a text message. The API will take your number and replace it with a string in this case to allow your message to get delivered. Attempting to work around it usually just gets the number blacklisted. You can test this by entering a short descriptive string for your tester ID, and seeing if that gets through.



来源:https://stackoverflow.com/questions/38355151/how-to-send-an-sms-with-custom-sender-id-with-amazon-sns-and-python-and-boto3

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