

终止支持通知：2025年9月15日， AWS 我们将停止对Amazon Lex V1的支持。2025 年 9 月 15 日之后，您将无法再访问 Amazon Lex V1 控制台或 Amazon Lex V1 资源。如果您使用的是 Amazon Lex V2，请改为参阅 [Amazon Lex V2 指南](https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html)。

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

# 使用语音输入测试自动程序 (AWS CLI)
<a name="gs-create-test-speech"></a>

要使用音频文件测试自动程序，请使用 [PostContent](API_runtime_PostContent.md) 操作。您可以使用 Amazon Polly text-to-speech 操作生成音频文件。

要运行本练习中的命令，您需要知道将在其中运行 Amazon Lex 和 Amazon Polly 命令的区域。有关 Amazon Lex 区域的列表，请参阅[运行时服务配额](gl-limits.md#gl-limits-runtime)。有关 Amazon Polly 区域的列表，请参阅《Amazon Web Services 一般参考》**中的 [AWS 区域和端点](https://docs.aws.amazon.com/general/latest/gr/rande.html#pol_region)。

**注意**  
以下 AWS CLI 示例是针对 Unix、Linux 和 macOS 进行格式化的。对于 Windows，请将 `"\$LATEST"` 更改为 `$LATEST` 并将每行末尾的反斜杠 (\$1) 继续符替换为脱字号 (^)。

**使用语音输入测试自动程序 (AWS CLI)**

1. 在中 AWS CLI，使用 Amazon Polly 创建音频文件。此示例的格式适用于 Unix、Linux 和 macOS。对于 Windows，请将每行末尾的反斜杠 (\$1) Unix 行继续符替换为脱字号 (^)。

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "i would like to order flowers" \
       --voice-id "Salli" \
       IntentSpeech.mpg
   ```

1. 要将音频文件发送到 Amazon Lex，请运行以下命令。Amazon Lex 将来自响应的音频保存在指定的输出文件中。

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream IntentSpeech.mpg \
       IntentOutputSpeech.mpg
   ```

   Amazon Lex 用对第一个插槽的请求进行响应。它将音频响应保存在指定的输出文件中。

   ```
   {
       "contentType": "audio/mpeg", 
       "slotToElicit": "FlowerType", 
       "dialogState": "ElicitSlot", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "i would like to order some flowers", 
       "slots": {
           "PickupDate": null, 
           "PickupTime": null, 
           "FlowerType": null
       }, 
       "message": "What type of flowers would you like to order?"
   }
   ```

1. 要订购玫瑰，请创建以下音频文件并将其发送给 Amazon Lex：

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "roses" \
       --voice-id "Salli" \ 
       FlowerTypeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream FlowerTypeSpeech.mpg \
       FlowerTypeOutputSpeech.mpg
   ```

1. 要设置送货日期，请创建以下音频文件并将其发送给 Amazon Lex：

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "tuesday" \
       --voice-id "Salli" \ 
       DateSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream DateSpeech.mpg \
       DateOutputSpeech.mpg
   ```

1. 要设置送货时间，请创建以下音频文件并将其发送给 Amazon Lex：

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "10:00 a.m." \
       --voice-id "Salli" \
       TimeSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream TimeSpeech.mpg \
       TimeOutputSpeech.mpg
   ```

1. 要确认送货，请创建以下音频文件并将其发送给 Amazon Lex：

   ```
   aws polly synthesize-speech \
       --region region \
       --output-format pcm \
       --text "yes" \
       --voice-id "Salli" \
       ConfirmSpeech.mpg
   ```

   ```
   aws lex-runtime post-content \
       --region region \
       --bot-name OrderFlowersBot \
       --bot-alias "\$LATEST" \
       --user-id UserOne \
       --content-type "audio/l16; rate=16000; channels=1" \
       --input-stream ConfirmSpeech.mpg \
       ConfirmOutputSpeech.mpg
   ```

   确认送货后，Amazon Lex 会发送一个响应来确认履行意图：

   ```
   {
       "contentType": "text/plain;charset=utf-8", 
       "dialogState": "ReadyForFulfillment", 
       "intentName": "OrderFlowers", 
       "inputTranscript": "yes", 
       "slots": {
           "PickupDate": "2017-05-16", 
           "PickupTime": "10:00", 
           "FlowerType": "roses"
       }
   }
   ```

## 下一个步骤
<a name="gs-cli-next-exercise-2"></a>

[练习 2：添加新表达 (AWS CLI)](gs-cli-update-utterance.md)