亞馬遜使用適用於 Java 2.x 的 SDK 精確定位短信和語音 API 示例 - AWS SDK for Java 2.x

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

亞馬遜使用適用於 Java 2.x 的 SDK 精確定位短信和語音 API 示例

下列程式碼範例說明如何透過 AWS SDK for Java 2.x 搭配 Amazon Pinpoint 位簡訊和語音 API 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數,但您可以在其相關情境和跨服務範例中查看內容中的動作。

Scenarios (案例) 是向您展示如何呼叫相同服務中的多個函數來完成特定任務的程式碼範例。

每個範例都包含一個連結 GitHub,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例會示範如何使用SendVoiceMessage

適用於 Java 2.x 的 SDK
注意

還有更多關於 GitHub。尋找完整範例,並了解如何在AWS 設定和執行程式碼範例儲存庫

import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpointsmsvoice.PinpointSmsVoiceClient; import software.amazon.awssdk.services.pinpointsmsvoice.model.SSMLMessageType; import software.amazon.awssdk.services.pinpointsmsvoice.model.VoiceMessageContent; import software.amazon.awssdk.services.pinpointsmsvoice.model.SendVoiceMessageRequest; import software.amazon.awssdk.services.pinpointsmsvoice.model.PinpointSmsVoiceException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SendVoiceMessage { // The Amazon Polly voice that you want to use to send the message. For a list // of voices, see https://docs.aws.amazon.com/polly/latest/dg/voicelist.html static final String voiceName = "Matthew"; // The language to use when sending the message. For a list of supported // languages, see // https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html static final String languageCode = "en-US"; // The content of the message. This example uses SSML to customize and control // certain aspects of the message, such as by adding pauses and changing // phonation. The message can't contain any line breaks. static final String ssmlMessage = "<speak>This is a test message sent from " + "<emphasis>Amazon Pinpoint</emphasis> " + "using the <break strength='weak'/>AWS " + "SDK for Java. " + "<amazon:effect phonation='soft'>Thank " + "you for listening.</amazon:effect></speak>"; public static void main(String[] args) { final String usage = """ Usage: <originationNumber> <destinationNumber>\s Where: originationNumber - The phone number or short code that you specify has to be associated with your Amazon Pinpoint account. For best results, specify long codes in E.164 format (for example, +1-555-555-5654). destinationNumber - The recipient's phone number. For best results, you should specify the phone number in E.164 format (for example, +1-555-555-5654).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String originationNumber = args[0]; String destinationNumber = args[1]; System.out.println("Sending a voice message"); // Set the content type to application/json. List<String> listVal = new ArrayList<>(); listVal.add("application/json"); Map<String, List<String>> values = new HashMap<>(); values.put("Content-Type", listVal); ClientOverrideConfiguration config2 = ClientOverrideConfiguration.builder() .headers(values) .build(); PinpointSmsVoiceClient client = PinpointSmsVoiceClient.builder() .overrideConfiguration(config2) .region(Region.US_EAST_1) .build(); sendVoiceMsg(client, originationNumber, destinationNumber); client.close(); } public static void sendVoiceMsg(PinpointSmsVoiceClient client, String originationNumber, String destinationNumber) { try { SSMLMessageType ssmlMessageType = SSMLMessageType.builder() .languageCode(languageCode) .text(ssmlMessage) .voiceId(voiceName) .build(); VoiceMessageContent content = VoiceMessageContent.builder() .ssmlMessage(ssmlMessageType) .build(); SendVoiceMessageRequest voiceMessageRequest = SendVoiceMessageRequest.builder() .destinationPhoneNumber(destinationNumber) .originationPhoneNumber(originationNumber) .content(content) .build(); client.sendVoiceMessage(voiceMessageRequest); System.out.println("The message was sent successfully."); } catch (PinpointSmsVoiceException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • 如需 API 詳細資訊,請參閱 AWS SDK for Java 2.x API 參考SendVoiceMessage中的。