简体   繁体   中英

unable to send voice message on AWS pinpoint

trying to make this tutorial work https://docs.aws.amazon.com/pinpoint/latest/developerguide/send-messages-voice.html .

  • current blocking issue: I'm facing this exception:
Caused by: java.net.UnknownHostException: sms-voice.pinpoint.us-east-1.amazonaws.com: No address associated with hostname
  • another thing: what's the configurationSet ? do I need one?

So far the things I've done:

  • create project on pinpoint
  • request long code
  • request production access (was previously throwing an error telling I required it)

any idea?

log:

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.access$500(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$2.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]

used the following code:

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());
}
}

}

used the following dependencies in the 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>

Thanks!

I have examined your Java code snippet and I can confirm to the best of my ability there's nothing wrong with your code.

I've confirmed the following :

  • I have tested the sample Java code example on the docs link you posted and it ran without issues.

Suggestion:

  1. I have confirmed that both us-east-1 & eu-west-1 support Pinpoint SMS & Voice. However since in your code you are using "eu-west-1" region then make sure that the region specified in your environment variables or .aws/credentails file also matches "eu-west-1" and not "us-east-1"

  2. From the error, since you are using Java.net, I suspect your corporate network is probably using a proxy. If that's the case, you'll need to configure the proxy server settings (proxy host, port, and username/password) through the ClientConfiguration object.

Here is working code that uses the AWS SDK for Java Version 2 . This works perfectly:

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

    }
}

In addition, do not forget to add these dependencies to your POM file:

 <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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM