在亚马逊 sns 中切换交易和促销短信

toggle between transactional and promotional sms in amazon sns

TL;DR 不想即时指定 transactional/promotional 消息类型(作为参数)每次都设置消息属性。

所以我想使用亚马逊 SNS 向客户发送 OTP,这是以下代码:

import boto3

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

根据他们的 documentation,有 2 种消息类型:
1.交易(时间关键交付)
2. 促销(非时间关键交付和成本效益)

我可以选择使用 set_sms_attributes() 设置默认消息属性,如下所示:

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

我不想继续更改此参数,因为它们是默认值。我不希望能够即时指定消息类型作为 publish()

中的参数

我查看了 MessageAttributes 但根据他们的 docs,它不是指定消息类型而是包含客户端在处理消息之前处理消息的元数据。

有没有一种方法可以即时切换消息类型,而无需使用 set_sms_attributes 在默认设置中进行设置?

您可以使用 publish() 中的 'AWS.SNS.SMS.SMSType' 进行设置。此属性描述为 here,例如:

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

注意:对于某些地区,这两种类型之间没有成本差异。

您可以将属性作为地图传递

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