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

Ada*_*rsh 5 amazon-sns python-3.x boto3

TL;DR不想在不必每次都设置消息属性的情况下动态指定交易/促销消息类型(作为参数)。

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

import boto3

client = boto3.client('sns')
response = client.publish(
               PhoneNumber='some_phone_number',
               Message='some_message'
           )
Run Code Online (Sandbox Code Playgroud)

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

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

client.set_sms_attributes(
           attributes={"DefaultSMSType": "Transactional" | "Promotional" }
)
Run Code Online (Sandbox Code Playgroud)

我不想继续更改此参数,因为它们是默认值。我不能将消息类型动态指定为参数publish()

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

有没有办法即时切换消息类型,而不必使用set_sms_attributes?

ACh*_*ion 7

您可以'AWS.SNS.SMS.SMSType'publish(). 此属性在此处描述,例如:

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'
                   }
               }
           )
Run Code Online (Sandbox Code Playgroud)

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