Amazon Lex和Lambda函数中的SSML请求错误无效

时间:2017-06-16 21:27:19

标签: python aws-lambda amazon-lex

我有这个测试lambda function代码,每当我使用Amazon Lex的音频功能时,我都会收到错误 - An error has occurred: Invalid SSML request (Service: AmazonParrot; Status Code: 400; Error Code: InvalidSsmlException

我的代码是:

def close(session_attributes, fulfillment_state, message):
    response = {
        'sessionAttributes': session_attributes,
        'dialogAction': {
            'type': 'Close',
            'fulfillmentState': fulfillment_state,
            'message': message
        }
    }

    return response

def testing(intent_request, outputDialogMode):

    slots = intent_request['currentIntent']['slots']
    name = slots['name']

    session_attributes = intent_request['sessionAttributes'] if 
    intent_request['sessionAttributes'] is not None else {}

    if outputDialogMode == 'Text':
        return close(
            session_attributes,
            'Fulfilled',
            {
                'contentType': 'PlainText',
                'content': 'Hi {}'.format(name)
            }
        )
    elif outputDialogMode == 'Voice':
        return close(
            session_attributes,
            'Fulfilled',
            {
                'contentType': 'SSML',
                'content': 'Hi {}'.format(name)
            }
        )

def dispatch(intent_request):
    """
    Called when the user specifies an intent for this bot.
    """
    logger.debug('input_request={}'.format(intent_request))



    intent_name = intent_request['currentIntent']['name']
    outputDialogMode = intent_request['outputDialogMode']

    # Dispatch to your bot's intent handlers
    if intent_name == 'testing':
        return testing(intent_request, outputDialogMode)


    raise Exception('Intent with name ' + intent_name + ' not supported')


# --- Main handler ---


def lambda_handler(event, context):
    """
    Route the incoming request based on intent.
    The JSON body of the request is provided in the event slot.
    """
    # By default, treat the user request as coming from the America/New_York time zone.
    os.environ['TZ'] = 'America/New_York'
    time.tzset()

    return dispatch(event)

根据this document,支持的contentType支持PlainText SSML <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/back2" tools:context="com.domain.appname.MainActivity"> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="230dp" app:srcCompat="@drawable/head" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="App Title" android:textAlignment="center" android:textSize="24sp" android:textStyle="bold" android:layout_marginTop="15dp" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="20dp" android:text="App Description" android:textSize="20sp"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="15dp" android:text="List Page" android:layout_marginBottom="15dp" android:textSize="20sp" style="@style/Widget.AppCompat.Button.Colored" android:onClick="switcher"/> </LinearLayout> </ScrollView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:orientation="vertical" > <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" ads:adSize="SMART_BANNER" ads:adUnitId="@string/banner_ad_unit_id" > </com.google.android.gms.ads.AdView> </LinearLayout> </LinearLayout> 。当我输入输入时,它可以正常工作,但是当我说话时,它会在最后输出错误。可能是什么原因?

1 个答案:

答案 0 :(得分:1)

语音响应中的Content必须格式化为SSML

尝试将Content更改为'<speak>Hi {}</speak>'.format(name)