Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据传入请求的contentType在Amazon Lambda函数输出中指定contentType?_Python_Aws Lambda_Amazon Lex - Fatal编程技术网

Python 根据传入请求的contentType在Amazon Lambda函数输出中指定contentType?

Python 根据传入请求的contentType在Amazon Lambda函数输出中指定contentType?,python,aws-lambda,amazon-lex,Python,Aws Lambda,Amazon Lex,我正在使用AmazonLexbot,在我的pythonLambda函数中,我将以下内容作为输出返回: def close(session_attributes, fulfillment_state, message): response = { 'sessionAttributes': session_attributes, 'dialogAction': { 'type': 'Close', 'fu

我正在使用
Amazon
Lex
bot
,在我的
python
Lambda
函数中,我将以下内容作为输出返回:

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

    return response

def Test_Bot(intent_request):
    # bunch of other code processing

    # call to close() in final output return
    return close(
        session_attributes,
        'Fulfilled',
        {
            'contentType': 'PlainText',
            'content': 'Thanks for asking!'
        }
    )
以下是我对
Lambda
函数的输入:

def dispatch(intent_request):

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

    # Dispatch to your bot's intent handlers
    if intent_name == 'TestBot':
        return Test_Bot(intent_request)


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


# --- Main handler ---


def lambda_handler(event, context):

    return dispatch(event)
注意这不是完整代码,而是输入和输出代码的一部分

现在我面临的问题是,如果我通过
Lex
测试机器人控制台键入我的问题,那么我的代码从头到尾都能正常工作。但是如果我在我的问题中发言,那么代码就会在结尾处中断。现在如果我是正确的,因为
contentType
被设置为
纯文本
,所以当我
输入问题时,它工作正常,但是当我在
中发言时,它在
contentType
中期待
音频
,它不在那里,因此它会中断


因此,我的问题是如何访问传入请求的
contentType
?意味着如果用户正在键入,则上述响应格式中的
contentType
将设置为
纯文本
,当用户在中讲话时,则
contentType
将设置为
音频

通过使机器人直接从Lex通信到Lambda,您无法更改“outputDialogMode”的值,它必须在外部通过使用文本或音频的accept头调用Lex来完成。实际上,这意味着您不能使用内容后响应

我也遇到了这种情况,我找到的解决办法是用Polly动态创建一个音频文件,然后将其存储在S3中,然后在响应卡的attachmentLinkUrl字段中引用保存的文件


请参阅
sing_a_song
方法

您的机器人是否配置了语音输出?在控制台中,在设置>输出语音下进行检查