AWS Lex(v2)集成Lambda问题:持续显示‘Intent is fulfilled’
问题排查与修复方案
你的Lambda代码存在三个关键问题,导致Lex无法返回距离结果,仅显示“Intent is fulfilled”提示:
1. JSON语法错误
intent对象中"name": "DistanceIntent"语句末尾缺少逗号,会导致Lambda执行时抛出语法错误,Lex无法解析响应,只能显示默认完成提示。
2. 响应结构不符合Lex V2规范
Lex V2要求messages字段与sessionState同级,而非嵌套在sessionState内部。你的代码把messages放在sessionState里,Lex无法读取这部分内容,自然不会返回距离结果。
3. 缺失get_distance函数实现
代码中调用了get_distance但未定义该函数,Lambda执行时会抛出NameError,直接导致函数执行失败,Lex无法获取有效响应。
修正后的完整代码
import json import boto3 from boto3.dynamodb.conditions import Key def get_distance(source, destination): # 替换为你的DynamoDB表名和查询逻辑 dynamodb = boto3.resource('dynamodb') table = dynamodb.Table('CityDistances') # 根据你的表结构调整查询方式,示例为复合主键查询 response = table.get_item( Key={ 'Source': source, 'Destination': destination } ) return response.get('Item', {}).get('Distance', '未找到两座城市间的距离') def lambda_handler(event, context): try: destination = event["sessionState"]["intent"]["slots"]["Destination"]["value"]["interpretedValue"] source = event["sessionState"]["intent"]["slots"]["Source"]["value"]["interpretedValue"] distance = get_distance(source, destination) return { "sessionState": { "dialogAction": { "type": "Close" }, "intent": { "name": "DistanceIntent", "state": "Fulfilled" } }, "messages": [ { "contentType": "PlainText", "content": f"两座城市间的距离是:{distance}" } ] } except Exception as e: # 异常捕获,返回错误信息便于调试 return { "sessionState": { "dialogAction": { "type": "Close" }, "intent": { "name": "DistanceIntent", "state": "Failed" } }, "messages": [ { "contentType": "PlainText", "content": f"查询失败:{str(e)}" } ] }
额外调试建议
- 查看Lambda对应的CloudWatch日志,确认函数执行时的具体报错信息;
- 验证Lex Intent的槽位配置,确保
Source和Destination槽位已正确关联,且Fulfillment已绑定到该Lambda函数; - 单独测试
get_distance函数,确保能从数据源(如DynamoDB)正确获取距离值。
内容的提问来源于stack exchange,提问作者qpwoeiruty
相关产品推荐
相关产品推荐

