【发布时间】:2021-03-29 15:18:50
【问题描述】:
我正在创建一个应用程序,用于使用 Amazon Alexa 搜索航班连接。我正在尝试将包含机场信息的 json 文件加载到变量中,以便稍后从中搜索数据。我试图将它加载到一个全局变量中,但没有奏效。然后我尝试将它加载到 session_attributes 中,但这也失败了。 json 文件包含 12668 个条目。我究竟做错了什么?什么是这样做的好方法?将 json 加载到变量中是否需要花费太多时间,Alexa 会因此退出?
json 文件位于 Alexa 开发者控制台的 lambda 文件夹中,包含代码的文件也位于该文件夹中。
Alexa 回复“请求技能的响应有问题”
"error": {
"type": "INVALID_RESPONSE",
"message": "An exception occurred while dispatching the request to the skill."
}
这是我的基本代码:
airportdata = []
def init():
airportdata = load_airport_data()
def load_airport_data():
with open('airportdata.json', encoding="utf8") as json_file:
return json.load(json_file)
class LaunchRequestHandler(AbstractRequestHandler):
def can_handle(self, handler_input):
return ask_utils.is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
# init()
session_attr = handler_input.attributes_manager.session_attributes
session_attr['airportdata'] = load_airport_data()
speak_output = "Welcome to the App. From where will you departure?"
return (
handler_input.response_builder
.speak(speak_output)
.ask(speak_output)
.response
)
编辑:原来使用的路径不正确。它应该是“/var/task/airportdata.json”,而不仅仅是 json 文件的名称。 lambda 的文件被复制到那里。然后它按预期工作。
【问题讨论】:
-
你试过说“Alexa,加载 JSON 文件”吗?
-
@jakub 不,我只运行了技能调用命令。然后出现错误。