无法在 AWS pinpoint 上发送语音消息
unable to send voice message on AWS pinpoint
努力让本教程发挥作用https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-voice.html。
- 当前阻塞问题:我遇到了这个异常:
Caused by: java.net.UnknownHostException: sms-voice.pinpoint.us-east-1.amazonaws.com: No address associated with hostname
- 另一件事:配置集是什么?我需要一个吗?
到目前为止我所做的事情:
- 在 pinpoint 上创建项目
- 请求长代码
- 请求生产访问权限(之前抛出错误提示我需要它)
有什么想法吗?
日志:
18:53:58.342 [main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true
18:53:58.705 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@3ecdeca4: Unable to load Client Side Monitoring configurations from environment variables!
18:53:58.705 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@63b96814: Unable to load Client Side Monitoring configurations from system properties variables!
18:53:58.709 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS
18:53:58.712 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@d141aa03: The 'default' profile does not define all the required properties!
18:53:58.732 [main] INFO com.amazonaws.internal.DefaultServiceEndpointBuilder - {sms-voice.pinpoint, eu-west-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'sms-voice.pinpoint.eu-west-1.amazonaws.com'.
18:53:58.809 [main] DEBUG com.amazonaws.metrics.AwsSdkMetrics - Admin mbean registered under com.amazonaws.management:type=AwsSdkMetrics
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY))
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey)
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path.
18:53:58.871 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Loading credentials from com.amazonaws.auth.profile.ProfileCredentialsProvider@24e02a53
18:53:58.877 [main] DEBUG com.amazonaws.request - Sending Request: POST https://sms-voice.pinpoint.eu-west-1.amazonaws.com /v1/sms-voice/voice/message Headers: (amz-sdk-invocation-id: XXX, Content-Length: 394, Content-Type: , User-Agent: aws-sdk-java/1.11.714 Linux/5.3.0-26-generic Eclipse_OpenJ9_VM/openj9-0.15.1 java/1.8.0_222 groovy/2.4.1 vendor/Eclipse_OpenJ9, )
18:53:58.956 [main] DEBUG com.amazonaws.auth.AWS4Signer - AWS4 Canonical Request: '"POST
/v1/sms-voice/voice/message
amz-sdk-invocation-id:XXXXX
amz-sdk-retry:0/0/500
content-length:394
content-type:
host:sms-voice.pinpoint.eu-west-1.amazonaws.com
user-agent:aws-sdk-java/1.11.714 Linux/5.3.0-26-generic Eclipse_OpenJ9_VM/openj9-0.15.1 java/1.8.0_222 groovy/2.4.1 vendor/Eclipse_OpenJ9
x-amz-date:XXXX
amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;user-agent;x-amz-date
XXXX"
18:53:58.957 [main] DEBUG com.amazonaws.auth.AWS4Signer - AWS4 String to Sign: '"XXXX
20200127T185358Z
20200127/eu-west-1/sms-voice/aws4_request
XXXX"
18:53:58.967 [main] DEBUG com.amazonaws.auth.AWS4Signer - Generating a new signing key as the signing key not available in the cache for the date 1580083200000
18:53:59.002 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
18:53:59.010 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
18:53:59.012 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 0 of 50; total allocated: 0 of 50]
18:53:59.026 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 1 of 50; total allocated: 1 of 50]
18:53:59.028 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443
18:53:59.053 [main] DEBUG com.amazonaws.http.conn.ClientConnectionManagerFactory -
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:76)
at com.amazonaws.http.conn.$Proxy8.connect(Unknown Source)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1311)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1127)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:784)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:752)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:686)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.doInvoke(AmazonPinpointSMSVoiceClient.java:713)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.invoke(AmazonPinpointSMSVoiceClient.java:680)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.invoke(AmazonPinpointSMSVoiceClient.java:669)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.executeSendVoiceMessage(AmazonPinpointSMSVoiceClient.java:569)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.sendVoiceMessage(AmazonPinpointSMSVoiceClient.java:540)
at com.helloworld.AlertVoiceMessageService.main(AlertVoiceMessageService.java:79)
Caused by: java.net.UnknownHostException: sms-voice.pinpoint.eu-west-1.amazonaws.com: No address associated with hostname
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:929)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
at java.net.InetAddress.getAllByName0(InetAddress.java:1277)
at java.net.InetAddress.getAllByName(InetAddress.java:1193)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at com.amazonaws.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:27)
at com.amazonaws.http.DelegatingDnsResolver.resolve(DelegatingDnsResolver.java:38)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
... 28 common frames omitted
18:53:59.053 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
18:53:59.053 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
18:53:59.053 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 0 of 50; total allocated: 0 of 50]
使用了以下代码:
package com.helloworld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoice;
import com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClientBuilder;
import com.amazonaws.services.pinpointsmsvoice.model.SSMLMessageType;
import com.amazonaws.services.pinpointsmsvoice.model.SendVoiceMessageRequest;
import com.amazonaws.services.pinpointsmsvoice.model.VoiceMessageContent;
public class AlertVoiceMessageService {
// The AWS Region that you want to use to send the voice message. For a list of
// AWS Regions where the Amazon Pinpoint SMS and Voice API is available, see
// https://docs.aws.amazon.com/pinpoint-sms-voice/latest/APIReference/
static final String region = "eu-west-1";
// The phone number that the message is sent from. The phone number that you
// specify has to be associated with your Amazon Pinpoint account. For best
// results, you should specify the phone number in E.164 format.
static final String originationNumber = "+XXX";
// The recipient's phone number. For best results, you should specify the
// phone number in E.164 format.
static final String destinationNumber = "+XXX";
// 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 = "Brian";
// 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-GB-WLS";
// 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 an alert message sent from "
+ "<emphasis>XXX</emphasis>."
+ "<amazon:effect phonation='soft'>Thank "
+ "you for listening.</amazon:effect></speak>";
// The phone number that you want to appear on the recipient's device. The
// phone number that you specify has to be associated with your Amazon Pinpoint
// account.
static final String callerId = "+XXXX";
// The configuration set that you want to use to send the message.
static final String configurationSet = "ConfigSet";
public static void main(String[] args) throws IOException {
try {
AmazonPinpointSMSVoice client = AmazonPinpointSMSVoiceClientBuilder.standard()
.withRegion(region).build();
SendVoiceMessageRequest request = new SendVoiceMessageRequest()
.withCallerId(callerId)
.withDestinationPhoneNumber(destinationNumber)
.withOriginationPhoneNumber(originationNumber)
// .withConfigurationSetName(configurationSet)
.withContent(new VoiceMessageContent()
.withSSMLMessage(new SSMLMessageType()
.withLanguageCode(languageCode)
.withVoiceId(voiceName)
.withText(ssmlMessage)
)
);
client.sendVoiceMessage(request);
System.out.println("The message was sent successfully.");
} catch (Exception ex) {
System.out.println("The message wasn't sent. Error message: " + ex.getMessage());
}
}
}
在 pom.xml
中使用了以下依赖项
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.714</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-pinpointsmsvoice</artifactId>
<version>1.11.714</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
谢谢!
我已经检查了您的 Java 代码片段,我可以尽我所能确认您的代码没有任何问题。
我已确认以下内容:
- 我已经在您发布的文档 link 上测试了样本 Java 代码示例,运行 没有问题。
建议:
我已经确认 us-east-1 和 eu-west-1 都支持 Pinpoint SMS & Voice。但是,由于在您的代码中您使用的是 "eu-west-1" 区域,因此请确保在您的环境变量或 .aws/credentails 文件中指定的区域也匹配 "eu-west-1" 而不是 "us-east-1"
从错误来看,由于您使用的是Java.net,我怀疑您的公司网络可能正在使用代理。如果是这种情况,您需要通过 ClientConfiguration 对象配置代理服务器设置(代理主机、端口和 username/password)。
这是使用 AWS SDK for Java 版本 2 的工作代码。这非常有效:
package com.example.pinpoint;
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.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SendVoiceMessage {
// The phone number that the message is sent from. The phone number that you
// specify has to be associated with your Amazon Pinpoint account. For best
// results, you should specify the phone number in E.164 format.
static final String originationNumber = "+1-204-555-5555";
// The recipient's phone number. For best results, you should specify the
// phone number in E.164 format.
static final String destinationNumber = "+1-555-555-8894";
// 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) {
System.out.println("Sending a message" );
// Set the content type to application/json required for this use case
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);
client.close();
}
public static void sendVoiceMsg(PinpointSmsVoiceClient client) {
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);
}
}
}
此外,不要忘记将这些依赖项添加到您的 POM 文件中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.15.14</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>pinpointsmsvoice</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>pinpoint</artifactId>
</dependency>
努力让本教程发挥作用https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-voice.html。
- 当前阻塞问题:我遇到了这个异常:
Caused by: java.net.UnknownHostException: sms-voice.pinpoint.us-east-1.amazonaws.com: No address associated with hostname
- 另一件事:配置集是什么?我需要一个吗?
到目前为止我所做的事情:
- 在 pinpoint 上创建项目
- 请求长代码
- 请求生产访问权限(之前抛出错误提示我需要它)
有什么想法吗?
日志:
18:53:58.342 [main] DEBUG com.amazonaws.AmazonWebServiceClient - Internal logging successfully configured to commons logger: true
18:53:58.705 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.EnvironmentVariableCsmConfigurationProvider@3ecdeca4: Unable to load Client Side Monitoring configurations from environment variables!
18:53:58.705 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.SystemPropertyCsmConfigurationProvider@63b96814: Unable to load Client Side Monitoring configurations from system properties variables!
18:53:58.709 [java-sdk-http-connection-reaper] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 60000 MILLISECONDS
18:53:58.712 [main] DEBUG com.amazonaws.monitoring.CsmConfigurationProviderChain - Unable to load configuration from com.amazonaws.monitoring.ProfileCsmConfigurationProvider@d141aa03: The 'default' profile does not define all the required properties!
18:53:58.732 [main] INFO com.amazonaws.internal.DefaultServiceEndpointBuilder - {sms-voice.pinpoint, eu-west-1} was not found in region metadata, trying to construct an endpoint using the standard pattern for this region: 'sms-voice.pinpoint.eu-west-1.amazonaws.com'.
18:53:58.809 [main] DEBUG com.amazonaws.metrics.AwsSdkMetrics - Admin mbean registered under com.amazonaws.management:type=AwsSdkMetrics
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY))
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey)
18:53:58.869 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Unable to load credentials from WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path.
18:53:58.871 [main] DEBUG com.amazonaws.auth.AWSCredentialsProviderChain - Loading credentials from com.amazonaws.auth.profile.ProfileCredentialsProvider@24e02a53
18:53:58.877 [main] DEBUG com.amazonaws.request - Sending Request: POST https://sms-voice.pinpoint.eu-west-1.amazonaws.com /v1/sms-voice/voice/message Headers: (amz-sdk-invocation-id: XXX, Content-Length: 394, Content-Type: , User-Agent: aws-sdk-java/1.11.714 Linux/5.3.0-26-generic Eclipse_OpenJ9_VM/openj9-0.15.1 java/1.8.0_222 groovy/2.4.1 vendor/Eclipse_OpenJ9, )
18:53:58.956 [main] DEBUG com.amazonaws.auth.AWS4Signer - AWS4 Canonical Request: '"POST
/v1/sms-voice/voice/message
amz-sdk-invocation-id:XXXXX
amz-sdk-retry:0/0/500
content-length:394
content-type:
host:sms-voice.pinpoint.eu-west-1.amazonaws.com
user-agent:aws-sdk-java/1.11.714 Linux/5.3.0-26-generic Eclipse_OpenJ9_VM/openj9-0.15.1 java/1.8.0_222 groovy/2.4.1 vendor/Eclipse_OpenJ9
x-amz-date:XXXX
amz-sdk-invocation-id;amz-sdk-retry;content-length;content-type;host;user-agent;x-amz-date
XXXX"
18:53:58.957 [main] DEBUG com.amazonaws.auth.AWS4Signer - AWS4 String to Sign: '"XXXX
20200127T185358Z
20200127/eu-west-1/sms-voice/aws4_request
XXXX"
18:53:58.967 [main] DEBUG com.amazonaws.auth.AWS4Signer - Generating a new signing key as the signing key not available in the cache for the date 1580083200000
18:53:59.002 [main] DEBUG org.apache.http.client.protocol.RequestAddCookies - CookieSpec selected: default
18:53:59.010 [main] DEBUG org.apache.http.client.protocol.RequestAuthCache - Auth cache not set in the context
18:53:59.012 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection request: [route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 0 of 50; total allocated: 0 of 50]
18:53:59.026 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 1 of 50; total allocated: 1 of 50]
18:53:59.028 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Opening connection {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443
18:53:59.053 [main] DEBUG com.amazonaws.http.conn.ClientConnectionManagerFactory -
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.amazonaws.http.conn.ClientConnectionManagerFactory$Handler.invoke(ClientConnectionManagerFactory.java:76)
at com.amazonaws.http.conn.$Proxy8.connect(Unknown Source)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
at com.amazonaws.http.apache.client.impl.SdkHttpClient.execute(SdkHttpClient.java:72)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeOneRequest(AmazonHttpClient.java:1311)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeHelper(AmazonHttpClient.java:1127)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.doExecute(AmazonHttpClient.java:784)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.executeWithTimer(AmazonHttpClient.java:752)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.execute(AmazonHttpClient.java:726)
at com.amazonaws.http.AmazonHttpClient$RequestExecutor.access0(AmazonHttpClient.java:686)
at com.amazonaws.http.AmazonHttpClient$RequestExecutionBuilderImpl.execute(AmazonHttpClient.java:668)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:532)
at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:512)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.doInvoke(AmazonPinpointSMSVoiceClient.java:713)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.invoke(AmazonPinpointSMSVoiceClient.java:680)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.invoke(AmazonPinpointSMSVoiceClient.java:669)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.executeSendVoiceMessage(AmazonPinpointSMSVoiceClient.java:569)
at com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClient.sendVoiceMessage(AmazonPinpointSMSVoiceClient.java:540)
at com.helloworld.AlertVoiceMessageService.main(AlertVoiceMessageService.java:79)
Caused by: java.net.UnknownHostException: sms-voice.pinpoint.eu-west-1.amazonaws.com: No address associated with hostname
at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
at java.net.InetAddress.lookupAllHostAddr(InetAddress.java:929)
at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1324)
at java.net.InetAddress.getAllByName0(InetAddress.java:1277)
at java.net.InetAddress.getAllByName(InetAddress.java:1193)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at com.amazonaws.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:27)
at com.amazonaws.http.DelegatingDnsResolver.resolve(DelegatingDnsResolver.java:38)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:374)
... 28 common frames omitted
18:53:59.053 [main] DEBUG org.apache.http.impl.conn.DefaultManagedHttpClientConnection - http-outgoing-0: Shutdown connection
18:53:59.053 [main] DEBUG org.apache.http.impl.execchain.MainClientExec - Connection discarded
18:53:59.053 [main] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Connection released: [id: 0][route: {s}->https://sms-voice.pinpoint.eu-west-1.amazonaws.com:443][total kept alive: 0; route allocated: 0 of 50; total allocated: 0 of 50]
使用了以下代码:
package com.helloworld;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoice;
import com.amazonaws.services.pinpointsmsvoice.AmazonPinpointSMSVoiceClientBuilder;
import com.amazonaws.services.pinpointsmsvoice.model.SSMLMessageType;
import com.amazonaws.services.pinpointsmsvoice.model.SendVoiceMessageRequest;
import com.amazonaws.services.pinpointsmsvoice.model.VoiceMessageContent;
public class AlertVoiceMessageService {
// The AWS Region that you want to use to send the voice message. For a list of
// AWS Regions where the Amazon Pinpoint SMS and Voice API is available, see
// https://docs.aws.amazon.com/pinpoint-sms-voice/latest/APIReference/
static final String region = "eu-west-1";
// The phone number that the message is sent from. The phone number that you
// specify has to be associated with your Amazon Pinpoint account. For best
// results, you should specify the phone number in E.164 format.
static final String originationNumber = "+XXX";
// The recipient's phone number. For best results, you should specify the
// phone number in E.164 format.
static final String destinationNumber = "+XXX";
// 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 = "Brian";
// 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-GB-WLS";
// 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 an alert message sent from "
+ "<emphasis>XXX</emphasis>."
+ "<amazon:effect phonation='soft'>Thank "
+ "you for listening.</amazon:effect></speak>";
// The phone number that you want to appear on the recipient's device. The
// phone number that you specify has to be associated with your Amazon Pinpoint
// account.
static final String callerId = "+XXXX";
// The configuration set that you want to use to send the message.
static final String configurationSet = "ConfigSet";
public static void main(String[] args) throws IOException {
try {
AmazonPinpointSMSVoice client = AmazonPinpointSMSVoiceClientBuilder.standard()
.withRegion(region).build();
SendVoiceMessageRequest request = new SendVoiceMessageRequest()
.withCallerId(callerId)
.withDestinationPhoneNumber(destinationNumber)
.withOriginationPhoneNumber(originationNumber)
// .withConfigurationSetName(configurationSet)
.withContent(new VoiceMessageContent()
.withSSMLMessage(new SSMLMessageType()
.withLanguageCode(languageCode)
.withVoiceId(voiceName)
.withText(ssmlMessage)
)
);
client.sendVoiceMessage(request);
System.out.println("The message was sent successfully.");
} catch (Exception ex) {
System.out.println("The message wasn't sent. Error message: " + ex.getMessage());
}
}
}
在 pom.xml
中使用了以下依赖项<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-bom</artifactId>
<version>1.11.714</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-pinpointsmsvoice</artifactId>
<version>1.11.714</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.9</version>
</dependency>
谢谢!
我已经检查了您的 Java 代码片段,我可以尽我所能确认您的代码没有任何问题。
我已确认以下内容:
- 我已经在您发布的文档 link 上测试了样本 Java 代码示例,运行 没有问题。
建议:
我已经确认 us-east-1 和 eu-west-1 都支持 Pinpoint SMS & Voice。但是,由于在您的代码中您使用的是 "eu-west-1" 区域,因此请确保在您的环境变量或 .aws/credentails 文件中指定的区域也匹配 "eu-west-1" 而不是 "us-east-1"
从错误来看,由于您使用的是Java.net,我怀疑您的公司网络可能正在使用代理。如果是这种情况,您需要通过 ClientConfiguration 对象配置代理服务器设置(代理主机、端口和 username/password)。
这是使用 AWS SDK for Java 版本 2 的工作代码。这非常有效:
package com.example.pinpoint;
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.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class SendVoiceMessage {
// The phone number that the message is sent from. The phone number that you
// specify has to be associated with your Amazon Pinpoint account. For best
// results, you should specify the phone number in E.164 format.
static final String originationNumber = "+1-204-555-5555";
// The recipient's phone number. For best results, you should specify the
// phone number in E.164 format.
static final String destinationNumber = "+1-555-555-8894";
// 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) {
System.out.println("Sending a message" );
// Set the content type to application/json required for this use case
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);
client.close();
}
public static void sendVoiceMsg(PinpointSmsVoiceClient client) {
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);
}
}
}
此外,不要忘记将这些依赖项添加到您的 POM 文件中:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>bom</artifactId>
<version>2.15.14</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>pinpointsmsvoice</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>pinpoint</artifactId>
</dependency>