user1700890
user1700890

Reputation: 7742

Dialogflow API access follow up intent in Python

I am following the following tutorial to understand Dialogflow Python API.

Here is how my adaptation looks:

import dialogflow_v2 as dialogflow
import json
from google.api_core.exceptions import InvalidArgument
from google.oauth2 import service_account

dialogflow_key = json.load(open(r'path_to_json_file.json'))
credentials = (service_account.Credentials.from_service_account_info(dialogflow_key))
session_client = dialogflow.SessionsClient(credentials=credentials)


DIALOGFLOW_LANGUAGE_CODE = 'en-US'
DIALOGFLOW_PROJECT_ID = 'some_project_id'
SESSION_ID = 'current-user-id'
session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)

text_to_be_analyzed =  "mobile data"
text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, 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)

Here is what program outputs:

Query text: mobile data
Detected intent: support.problem
Detected intent confidence: 0.41999998688697815
Fulfillment text: Make sure mobile data is enabled and Wi-Fi is turned off.

Now my intent support.problem has follow up intent support.problem-yes, where customer replies Done it and gets back another response Let us try another step

How do I pass text/query to follow up intent and how do I get response in Python?

Upvotes: 1

Views: 1220

Answers (1)

Prisoner
Prisoner

Reputation: 50741

The response.query_result object should also contain an output_context field, which should be an array of Context objects. This array should be passed in query_parameters.context you pass to detect_intent().

You should be able to create a dictionary with the query parameters fields (including one for context) that you pass to session_client.detect_intent.

Upvotes: 2

Related Questions