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?
解决方案
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...'
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'}})