Я понял это после нескольких часов поиска по этой проблеме.Это код Python для отправки запроса в DialogFlow с использованием API V1.
Чтобы использовать API V2, для него есть отдельный клиентский код, и ответ от DialogFlow очень сильно изменился по сравнению с V1.
import random
import string
def randomString(stringLength=10):
"""Generate a random string of letters, digits and special characters """
password_characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(password_characters) for i in range(stringLength))
def detect_intent_texts(project_id, session_id, texts, language_code):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversation."""
import dialogflow_v2 as dialogflow
import json
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
print('Session path: {}\n'.format(session))
text_input = dialogflow.types.TextInput(text=texts, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(session=session, query_input=query_input)
test = response.query_result
print(test)
'''
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(response.query_result,response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(response.query_result.fulfillment_text))'''
detect_intent_texts(project_ID,randomString(),'Input query text', 'language code'[doc][1])
Ответ от DialogFlow сохраняется в переменной
test
Примечание. Он больше не в формате Json.Это объект.Чтобы получить доступ к значениям, вы должны использовать параметры.Пример:
test = response.query_result.parameters
from google.protobuf.json_format import MessageToDict
output_entities = MessageToDict(test)
print(output_entities)
Теперь сущности хранятся в переменной
output_entities