3

I am moving from dialogflow V1 to V2. Using the dialogflow python SDK I receive a DetectIntentResponse struct object that should have the information inside that I need.

After some time of trying to find documentation and trying to inspect this object I need your help. This object is so far out of my league ...

For documentation, thats how I get the response object:

import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
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)

How can I parse the response?

e.g. I get some parameter struct by using response.query_result.parameters But how do I get this list?

Maybe I can transform the response into json (that would make things quite easy)?

I need dicts, lists, strings ... :)

caliph
  • 1,389
  • 3
  • 27
  • 52

3 Answers3

5

You can use MessageToJson in google.protobuf project. (Google's Protocol Buffer)

#import the function
from google.protobuf.json_format import MessageToJson

#...after getting the response = session_client.detect_intent(...)
json_response = MessageToJson(response)

You can convert various types included in the DetectIntentResponse such as QueryResult by MessageToJson(response.query_result) etc. to get a specific response.

My answer is specific to DialogFlow however I took the hint from this answer at SO which answers a much general question related to python.

Please note that I have used google.protobuf.json_format.MessageToJson specifically because the DialogFlow API V2 returns class objects defined by Google. I cannot guarantee that this will work with other Chatbot APIs (may be I need to explore it too).

Najam
  • 141
  • 1
  • 8
  • 1
    This is the answer - why oh why doesn't Google put this on their own documentation page? Parsing something to JSON is among the most basic things we want to do! – tgmerritt Mar 08 '20 at 18:19
0

The response we get after calling detect_intent() function is of type <class 'google.cloud.dialogflow_v2.types.DetectIntentResponse'>

Here is a sample response after calling detect_intent() function:

query_result {
  query_text: "testing testing 123 abc@gmail.com"
  action: "test"
  parameters {
    fields {
      key: "email"
      value {
        string_value: "abc@gmail.com"
      }
    }
    fields {
      key: "number-integer"
      value {
        list_value {
          values {
            number_value: 123.0
          }
        }
      }
    }
  }
  all_required_params_present: true
  fulfillment_text: "testing invoked"
  fulfillment_messages {
    text {
      text: "testing invoked"
    }
  }
  output_contexts {
    name: "projects/*****/agent/sessions/session-test/contexts/testing-context"
    lifespan_count: 5
    parameters {
      fields {
        key: "email"
        value {
          string_value: "abc@gmail.com"
        }
      }
      fields {
        key: "email.original"
        value {
          string_value: "abc@gmail.com"
        }
      }
      fields {
        key: "number-integer"
        value {
          list_value {
            values {
              number_value: 123.0
            }
          }
        }
      }
      fields {
        key: "number-integer.original"
        value {
          string_value: "123"
        }
      }
    }
  }
  intent {
    name: "projects/*****/agent/intents/*****"
    display_name: "test"
  }
  intent_detection_confidence: 1.0
  language_code: "en"
}

You can easily parse values using below code:

Query Result Type : type(response) --> <class 'google.cloud.dialogflow_v2.types.DetectIntentResponse'>
Query text : response.query_result.query_text --> testing testing 123 abc@gmail.com
Detected intent : response.query_result.intent.display_name --> test
Fulfillment text : response.query_result.fulfillment_text --> testing invoked
Parameters : response.query_result.parameters --> fields {
  key: "email"
  value {
    string_value: "abc@gmail.com"
  }
}
fields {
  key: "number-integer"
  value {
    list_value {
      values {
        number_value: 123.0
      }
    }
  }
}

Hope it helps.

sid8491
  • 6,622
  • 6
  • 38
  • 64
0

I solved my problem using the property ".pb". Here is my code:

...
json_response = MessageToJson(response._pb)
...