如何在python中向Dialogflow事件添加参数

tim*_*imu 3 python struct protocol-buffers dialogflow-es

我正在尝试使用python dialogflow client v2(https://dialogflow.com/docs/reference/api-v2/rest/v2beta1/projects.agent.sessions/detectIntent#eventinput)发送EventInput,但是无法设置参数。

将其设置为类似对象会 {"result_guid": "49A8608C-4854-4964-81C3-0A75F912B994"}返回错误ValueError: Protocol message Struct has no "result_guid" field.

如何解决呢?

tim*_*imu 5

找到了解决方案。parameters必须是google.protobuf.Struct,因此类似下面的内容

from google.protobuf import struct_pb2

parameters = struct_pb2.Struct()
parameters["result_guid"] = "49A8608C-4854-4964-81C3-0A75F912B994"
event_input = dialogflow.types.EventInput(name=event_name, parameters=parameters, language_code=language_code)
Run Code Online (Sandbox Code Playgroud)