Adarsh
Adarsh

Reputation: 3573

toggle between transactional and promotional sms in amazon sns

TL;DR Wan't to specify transactional/promotional message type on the fly (as a param) without having to set the message attributes every time.

So I want to send OTPs to customer using amazon SNS and this is the code for the following:

import boto3

client = boto3.client('sns')
response = client.publish(
               PhoneNumber='some_phone_number',
               Message='some_message'
           )

According to their documentation, there are 2 message types:
1. Transactional (Time critical delivery)
2. Promotional (Non time critical delivery and cost effective)

I have an option to set the default message attributes using the set_sms_attributes() as follows:

client.set_sms_attributes(
           attributes={"DefaultSMSType": "Transactional" | "Promotional" }
)

I donot wan't to keep changing this param as they are defaults. I wan't to be able to specify the message type on the fly as a parameter in publish()

I checked out the MessageAttributes but according to their docs, it's not to specify the message type but contains metadata for the client to handle the message before processing it.

Is there a way to toggle the message type on the fly without having to set it in the default settings using the set_sms_attributes ?

Upvotes: 6

Views: 1895

Answers (2)

vaquar khan
vaquar khan

Reputation: 11479

You can pass attribute as Map

    Map<String, MessageAttributeValue> smsAttributes = new HashMap<String, 
    MessageAttributeValue>();
        
    smsAttributes.put("AWS.SNS.SMS.SMSType",new 
    MessageAttributeValue().withStringValue("Transactional").withDataType("String"));

Upvotes: 0

AChampion
AChampion

Reputation: 30278

You can set it using 'AWS.SNS.SMS.SMSType' in the publish(). This attribute is described here, e.g.:

client = boto3.client('sns')
response = client.publish(
               PhoneNumber='some_phone_number',
               Message='some_message',
               MessageAttributes = {
                   'AWS.SNS.SMS.SMSType': {
                       'DataType': 'String',
                       'StringValue': 'Promotional'  # or 'Transactional'
                   }
               }
           )

Note: for some regions there is no cost difference between these 2 types.

Upvotes: 10

Related Questions