使用 AWS 最终用户消息 SMS 发送短信或语音消息的示例 - AWS 最终用户消息 SMS

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 AWS 最终用户消息 SMS 发送短信或语音消息的示例

您可以使用 AWS 最终用户消息 SMS API 直接从您的应用程序发送消息。交易消息是您发送给特定收件人的消息。

本节包括发送短信语音消息的代码示例。

重要

要使用共享资源,您必须使用完整的 Amazon 资源名称 (ARN)。

使用 AWS 最终用户消息 SMS 发送 SMS 消息

如果您使用的是共享资源,则必须使用该资源的完整亚马逊资源名称 (ARN)。您可以参照以下代码示例,使用 适用于 Python (Boto3) 的 AWS SDK发送短信。

import boto3 from botocore.exceptions import ClientError def send_sms_message(sms_voice_v2_client, configuration_set, context_keys, country_parameters, destination_number, dry_run, keyword, max_price, message_body, message_type, origination_number, ttl): try: response = sms_voice_v2_client.send_text_message( ConfigurationSetName=configuration_set, Context=context_keys, DestinationCountryParameters=country_parameters, DestinationPhoneNumber=destination_number, DryRun=dry_run, Keyword=keyword, MaxPrice=max_price, MessageBody=message_body, MessageType=message_type, OriginationIdentity=origination_number, TimeToLive=ttl ) except ClientError as e: print(e.response) else: return response['MessageId'] def main(): configuration_set = "MyConfigurationSet" context_keys = {"key1": "value1"} country_parameters = { "IN_TEMPLATE_ID": "TEMPLATE01234", "IN_ENTITY_ID": "ENTITY98765" } destination_number = "+14255550168" dry_run = False keyword = "MyKeyword" max_price = "2.00" message_body = ("This is a test message sent from AWS End User Messaging SMS " "using the AWS SDK for Python (Boto3). ") message_type = "TRANSACTIONAL" origination_number = "+12065550183" ttl = 120 print( f"Sending text message to {destination_number}.") message_id = send_sms_message( boto3.client('pinpoint-sms-voice-v2'), configuration_set, context_keys, country_parameters, destination_number, dry_run, keyword, max_price, message_body, message_type, origination_number, ttl) print(f"Message sent!\nMessage ID: {message_id}") if __name__ == '__main__': main()

在前面的示例中,对 main() 函数进行以下更改:

  • configuration_set 的值改为您要用于发送此消息的配置集的名称或 Amazon 资源名称 (ARN)。

  • context_keys 的值改为您要在发送此消息时使用的键和值。这些键出现在与此消息关联的事件记录中。

  • 如果您使用已注册的发件人 ID 向印度的客户发送消息,请将 country_parameters 的值改为匹配您在注册发件人 ID 时收到的注册实体 ID 和模板 ID。

    重要

    如果您不使用注册的发件人 ID 向印度的客户发送消息,请完全忽略此参数。同时,还必须删除 send_sms_message 函数中的相应行。

  • destination_number 的值改为您要向其发送消息的电话号码。

  • 如果要在不发送任何消息的情况下执行此操作,请将 dry_run 的值改为 True

  • max_price 的值改为您发送此消息的每个消息部分要花费的最大金额(以美元为单位)。一个消息部分最多包含 140 字节的信息。有关更多信息,请参阅 短信字符限制

  • 更改 message_body 的值以包括您要发送的消息。一条消息的最大长度取决于其所包含的字符。有关短信字符编码的更多信息,请参阅短信字符限制

  • 更改 message_type 的值以表示相应的消息类别。有效值包括 TRANSACTIONAT(适用于重要或对时间敏感的消息)和 PROMOTION(适用于不重要或对不时间敏感的消息)。

  • origination_number 的值改为您要用于发送消息的电话号码。电话号码必须使用 E.164 格式。

  • 将的值更改ttl为 AWS 最终用户消息 SMS 应尝试传送消息的时间(以秒为单位)。您可以将 TTL 值设置为最多 259200 秒(72 小时)。

使用 AWS 最终用户消息 SMS 发送语音消息

您可以使用以下代码示例,通过 适用于 Python (Boto3) 的 AWS SDK发送语音消息。

import boto3 from botocore.exceptions import ClientError def send_voice_message(sms_voice_v2_client, configuration_set, context_keys, destination_number, dry_run, max_price, message_body, message_type, origination_number, ttl, voice_id): try: response = sms_voice_v2_client.send_voice_message( ConfigurationSetName=configuration_set, Context=context_keys, DestinationPhoneNumber=destination_number, DryRun=dry_run, MaxPricePerMinute=max_price, MessageBody=message_body, MessageBodyTextType=message_type, OriginationIdentity=origination_number, TimeToLive=ttl, VoiceId=voice_id ) except ClientError as e: print(e.response) else: return response['MessageId'] def main(): configuration_set = "MyConfigurationSet" context_keys = {"key1":"value1"} destination_number = "+12065550123" dry_run = False max_price = "2.00" message_body = ( "<speak>" "This is a test message sent from <emphasis>AWS End User Messaging SMS</emphasis>" "using the <break strength='weak'/> 适用于 Python (Boto3) 的 AWS SDK. " "<amazon:effect phonation='soft'>Thank you for listening." "</amazon:effect>" "</speak>") message_type = "SSML" origination_number = "+18445550142" ttl = 120 voice_id = "MATTHEW" print( f"Sending voice message with AWS End User Messaging SMS from {origination_number} to {destination_number}.") message_id = send_voice_message( boto3.client('pinpoint-sms-voice-v2'), configuration_set, context_keys, destination_number, dry_run, max_price, message_body, message_type, origination_number, ttl, voice_id) print(f"Message sent!\nMessage ID: {message_id}") if __name__ == '__main__': main()

在前面的示例中,对 main() 函数进行以下更改:

  • configuration_set 的值改为您要用于发送此消息的配置集的名称或 Amazon 资源名称 (ARN)。

  • context_keys 的值改为您要在发送此消息时使用的键和值。这些键出现在与此消息关联的事件记录中。

  • destination_number 的值改为您要向其发送消息的电话号码。

  • max_price 的值改为发送此消息每分钟要花费的最大金额。

  • 更改 message_body 的值以包括您要发送的消息。消息最多可包含 6,000 个字符。

  • 如果要使用纯文本脚本而不是 SSML 格式的脚本,请将 message_type 的值改为 TEXT

  • origination_number 的值改为您要用于发送消息的电话号码。电话号码必须使用 E.164 格式。

  • 如果要在不发送任何消息的情况下执行此操作,请将 dry_run 的值改为 True

  • 将的值更改ttl为 AWS 最终用户消息 SMS 应尝试传送消息的时间(以秒为单位)。您可以将 TTL 值设置为最多 259200 秒(72 小时)。

  • MATTHEW 替换为要用于发送消息的 Amazon Polly 语音的名称。有关支持的语音的完整列表,请参阅《短信和语音》第 2 版 API 参考SendVoiceMessage中。如果您未指定语音,则您的消息将使用 “MATTHEW” 语音发送。