使用带有cURL的Alexa语音服务RestAPI

时间:2015-08-07 15:30:20

标签: rest curl amazon-echo alexa-voice-service

我喜欢带有curl的Alexa语音API(https://developer.amazon.com/public/solutions/alexa/alexa-voice-service/rest/speechrecognizer-requests)。语音识别器API调用比我以前使用的更复杂,并且需要附加包含语音样本的MP3文件。任何人都可以建议如何用curl构建以下内容? (给定链接还有更多信息)

POST /v1/avs/speechrecognizer/xxxxxxxxxxxx HTTP/1.1

Host: access-alexa-na.amazon.com
Authorization: Bearer xxxxxxxxxxxx
Content-Type: multipart/form-data; boundary=boundary_term
Transfer-Encoding: chunked

--boundary_term
Content-Disposition: form-data; name="request"
Content-Type: application/json; charset=UTF-8

{
    "messageHeader": {
        "deviceContext": [
            {
                "name": "playbackState",
                "namespace": "AudioPlayer"
                "payload": {
                    "streamId": "xxxxxxxxxxxx",
                    "offsetInMilliseconds": "xxxxxxxxxxxx",
                    "playerActivity": "xxxxxxxxxxxx"
                }
            },
            {
                ...
            },
            ...
        ]
    },
    "messageBody": {
        "profile": "alexa-close-talk",
        "locale": "en-us",
        "format": "audio/L16; rate=16000; channels=1"
    }
}

--boundary_term
Content-Disposition: form-data; name="audio"
Content-Type: audio/L16; rate=16000; channels=1

...encoded_audio_data...

--boundary_term--

1 个答案:

答案 0 :(得分:0)

我不是bash专家,但我是如何使用cURL与AVS进行交互的。我生成一个包含多部分主体内容的文件,其中包含二进制音频数据并将其传递给cURL。

############################################################
# First we creat a bunch of variables to hold data.
############################################################

# Auth token
TOKEN="Atza|IQEBLjAsAhR..."

# Boundary
BOUNDARY="BOUNDARY1234"
BOUNDARY_DASHES="--"

# Newline characters
NEWLINE='\r\n';

# Metadata headers
METADATA_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"metadata\"";
METADATA_CONTENT_TYPE="Content-Type: application/json; charset=UTF-8";

# Audio headers
AUDIO_CONTENT_TYPE="Content-Type: audio/L16; rate=16000; channels=1";
AUDIO_CONTENT_DISPOSITION="Content-Disposition: form-data; name=\"audio\"";

# Metadata JSON body
METADATA="{\
\"messageHeader\": {},\
\"messageBody\": {\
\"profile\": \"alexa-close-talk\",\
\"locale\": \"en-us\",\
\"format\": \"audio/L16; rate=16000; channels=1\"\
}\
}"

############################################################
# Then we start composing the body using the variables.
############################################################

# Compose the start of the request body
POST_DATA_START="
${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}${METADATA_CONTENT_DISPOSITION}${NEWLINE}\
${METADATA_CONTENT_TYPE}\
${NEWLINE}${NEWLINE}${METADATA}${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${NEWLINE}\
${AUDIO_CONTENT_DISPOSITION}${NEWLINE}${AUDIO_CONTENT_TYPE}${NEWLINE}"

# Compose the end of the request body
POST_DATA_END="${NEWLINE}${NEWLINE}${BOUNDARY_DASHES}${BOUNDARY}${BOUNDARY_DASHES}${NEWLINE}"

# Now we create a request body file to hold everything including the binary audio data.

# Write metadata to body file
echo -e $POST_DATA_START > multipart_body.txt

# Append binary audio data to body file
cat hello.wav >> multipart_body.txt

# Append closing boundary to body file
echo -e $POST_DATA_END >> multipart_body.txt

############################################################
# Finally we get to compose the cURL request command
# passing it the generated request body file as the multipart body.
############################################################

# Compose cURL command and write to output file
curl -X POST \
  -H "Authorization: Bearer ${TOKEN}"\
  -H "Content-Type: multipart/form-data; boundary=${BOUNDARY}"\
  --data-binary @foo.txt\
  https://access-alexa-na.amazon.com/v1/avs/speechrecognizer/recognize\
  > response.txt

音频必须单声道以16k Hz 采样,采用16位PCM 进行采样。否则AVS不会发回任何内容。

有关详细信息,请查看我的Alexa Voice Service (AVS) with cURL博文。