Search code examples
pythonpython-3.xdialogflow-eschatbot

Obtain Intents, Entities and the training data from Dialogflow in the python environment


I want to know if there is some way through which I can obtain all the Intents(its corresponding questions), Entities and the training data(I defined in Google Dialogflow) programmatically using python.

Following is the code(works fine) through which i get a response from dialogflow.

GOOGLE_APPLICATION_CREDENTIALS = '###'

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] =
GOOGLE_APPLICATION_CREDENTIALS

def dialogflow_api_response(text):
  session_client = dialogflow.SessionsClient()

  session = session_client.session_path('###', '##')

  text_input = dialogflow.types.TextInput(text=text, language_code='en')
  query_input = dialogflow.types.QueryInput(text=text_input)
  response = session_client.detect_intent(session=session, query_input=query_input)
  jsonObj = MessageToJson(response)
  x = json.loads(jsonObj)

If there is, please point me in the right direction.

Thank you


Solution

  • Yes it is possible to get all the intents, entities, unserEntities and contexts from Dialogflow agent using Dilogflow API as JSON object. Here's the code to get the list of intents:

    def list_intents(project_id):
       import dialogflow_v2 as dialogflow
       intents_client = dialogflow.IntentsClient()
    
       parent = intents_client.project_agent_path(project_id)
    
       intents = intents_client.list_intents(parent)
    
       for intent in intents:
           print('=' * 20)
           print('Intent name: {}'.format(intent.name))
           print('Intent display_name: {}'.format(intent.display_name))
           print('Action: {}\n'.format(intent.action))
           print('Root followup intent: {}'.format(
               intent.root_followup_intent_name))
           print('Parent followup intent: {}\n'.format(
              intent.parent_followup_intent_name))
    
           print('Input contexts:')
           for input_context_name in intent.input_context_names:
               print('\tName: {}'.format(input_context_name))
    
           print('Output contexts:')
           for output_context in intent.output_contexts:
               print('\tName: {}'.format(output_context.name))
    
           for training_phrase in intent.training_phrases:
                train_phrase = training_phrase.parts[0].text
                print(train_phrase)  
    

    For further information you can refer to DF official gitHub here