Skip to content

Instantly share code, notes, and snippets.

@mchayapol
Created October 10, 2018 01:15
Show Gist options
  • Save mchayapol/fb2fa45d845e4710a99e87352eb89e75 to your computer and use it in GitHub Desktop.
Save mchayapol/fb2fa45d845e4710a99e87352eb89e75 to your computer and use it in GitHub Desktop.
Python Dialogflow - detect intent from texts
import json
import dialogflow_v2 as dialogflow
def detect_intent_texts(project_id, session_id, texts, language_code,verbose=False):
"""Returns the result of detect intent with texts as inputs.
Using the same `session_id` between requests allows continuation
of the conversaion."""
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
if verbose:
print('Session path: {}\n'.format(session))
for text in texts:
text_input = dialogflow.types.TextInput(
text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = session_client.detect_intent(
session=session, query_input=query_input)
if verbose:
print('=' * 20)
print('Query text: {}'.format(response.query_result.query_text))
print('Detected intent: {} (confidence: {})\n'.format(
response.query_result.intent.display_name,
response.query_result.intent_detection_confidence))
print('Fulfillment text: {}\n'.format(
response.query_result.fulfillment_text))
return response.query_result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment