You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

代码运行正常但无法获取dialogAction响应,返回Null问题求助

问题诊断与修复方案

我一眼就发现问题所在了——你的lambda_handler函数最后只是调用了getdata(),但没有返回这个函数生成的响应对象!Lambda函数需要明确返回处理后的结果,否则默认会返回Null,自然就拿不到dialogAction的内容了。

修正后的代码

import json
import requests

def getdata(intent_name, fulfillment_state, message):
    response = {
        'dialogAction': {
            'type': 'Close',
            'intentName': intent_name,
            'fulfillmentState': fulfillment_state,
            'message': message
        }
    }
    return response

def lambda_handler(event,context):
    payload = {'userId':4,'type':'PARENT'}
    r = requests.post("http://ec2-54-226-57-153.compute-1.amazonaws.com:8080/Tracking/rest/api", data=json.dumps(payload), headers = {'Content-Type': 'application/json','Accept': 'application/json'})
    print(r.content)
    # 关键:将getdata的返回值作为Lambda函数的最终返回结果
    return getdata( 'currentIntent', 'Fulfilled', { 'contentType': 'PlainText', 'content': 'message' } )

进阶优化建议

你还可以根据接口调用的实际结果动态调整返回的消息内容,让交互更智能:

def lambda_handler(event,context):
    payload = {'userId':4,'type':'PARENT'}
    try:
        r = requests.post("http://ec2-54-226-57-153.compute-1.amazonaws.com:8080/Tracking/rest/api", data=json.dumps(payload), headers = {'Content-Type': 'application/json','Accept': 'application/json'})
        r.raise_for_status()  # 自动检测HTTP请求是否成功
        api_response = r.json()
        # 用接口返回的数据构造响应消息
        return getdata( 'currentIntent', 'Fulfilled', { 'contentType': 'PlainText', 'content': f"接口返回结果:{json.dumps(api_response)}" } )
    except Exception as e:
        print(f"请求出错:{str(e)}")
        # 调用失败时返回错误提示
        return getdata( 'currentIntent', 'Failed', { 'contentType': 'PlainText', 'content': f"调用接口失败:{str(e)}" } )

内容的提问来源于stack exchange,提问作者Pulkit

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:02:39