如何检测从 Dialogflow 发送到 Python 后端的消息的上下文?

How to detect the Context of the message sent from Dialogflow to Python back end?

我正在使用以下 detect_intent 函数:

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = 'private_key.json'

DIALOGFLOW_PROJECT_ID = 'kuku-lulu'

DIALOGFLOW_LANGUAGE_CODE = 'en'

SESSION_ID = 'me'

def detect_intent(text):

    session_client = dialogflow.SessionsClient()

    session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)

    text_input = dialogflow.types.TextInput(text = text, language_code = DIALOGFLOW_LANGUAGE_CODE)

    query_input = dialogflow.types.QueryInput(text = text_input)

    try:

        response = session_client.detect_intent(session = session, query_input = query_input)

    except InvalidArgument:

        raise

    # print("Query text:", response.query_result.query_text)

    # print("Detected intent:", response.query_result.intent.display_name)

    # print("Detected intent confidence:", response.query_result.intent_detection_confidence)

    # print("Fulfillment text:", response.query_result.fulfillment_text)

    return {'sc': session_client, 'intent_name': response.query_result.intent.display_name}

如何从 Dialogflow 中检测上下文?

response.query_result.output_context 字段应该是一组当前活动的上下文,每个上下文都是一个 Context 对象。

您可以读取此信息并将其作为下一个请求的输入上下文发回。