6

I desperatly try to set parameters in a

dialogflow.types.EventInput

in python.

This doc says the parameters need to be of type Struct.

I read here that the parameters needs to be a google.protobuf.Struct. But it does not work for me.

Is there another Struct type equivalent in python?

If i send the EventInput without parameters, the intent is detected correctly.

I tried this so far:

import dialogflow_v2 as dialogflow
session_client = dialogflow.SessionsClient()

session = session_client.session_path(project_id, session_id)
parameters = struct_pb2.Struct()
parameters['given-name'] = 'Jeff'
parameters['last-name'] = 'Bridges'

event_input = dialogflow.types.EventInput(         
    name='greetPerson',
    language_code='de',
    parameters=parameters)

query_input = dialogflow.types.QueryInput(event=event_input)

response = session_client.detect_intent(
    session=session, query_input=query_input)

Anybody having experience with this usecase?

Things i also tried:

  1. Pass a class named p yields:

    Parameter to MergeFrom() must be instance of same class: expected Struct got p. for field EventInput.parameters

  2. Pass a dict:

    parameters = {
        'given-name': 'Jeff',
        'last-name': 'Bridges'} 
    

    yields:

    Protocol message Struct has no "given-name" field.

  3. Generate Struct with constructor:

    from google.protobuf.struct_pb2 import Struct, Value
    parameters = Struct(fields={
        'given-name':Value(string_value='Jeff'),
        'last-name':Value(string_value='Bidges')
    })
    

    yields sometimes:

    Exception in thread ptvsd.stopping (most likely raised during interpreter shutdown):

This is the parameter section if my intent

/EventInput

TVK
  • 1,042
  • 7
  • 21

2 Answers2

8

This is how I did this:

import dialogflow
from google.protobuf import struct_pb2

session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)

parameters = struct_pb2.Struct()
parameters["given-name"] = 'Jeff'
parameters["last-name"] = 'Bridges'

query_input = {
    'event': {
        "name": "greetPerson",
        "parameters": parameters,
        "language_code": "de"
    }
}

response = session_client.detect_intent(
    session=session,
    query_input=query_input)

Note:
In dialogflow console, you must give default values of parameters as #even_name.parameter_name.
In this case for parameter given-name it would be #greetPerson.given-name and for last-name it would be #greetPerson.last-name.

Docs Reference:
We are using DetectIntent, in which we are using QueryInput, in which finally we are using EvenInput

Hope it helps.

sid8491
  • 6,622
  • 6
  • 38
  • 64
  • Wow! Setting the deafault exactly as you mentioned did the magic! Can you please let me know if there is some documentation, where I could have read about this requirement? – TVK Jan 16 '19 at 07:52
  • 1
    @TVK i too banged my head over this, tried different things, then searched for the exact documents and format which it is expecting and got it worked. i have added doc reference in the answer as well. – sid8491 Jan 16 '19 at 08:38
  • Thx i read all of these before, but they dont mention that a default value is required. Only doc mentioning sth like that i just found was https://dialogflow.com/docs/concepts/slot-filling#parameter_values_from_contexts – TVK Jan 16 '19 at 08:44
  • oh yes, about that i came to know when i was working on slot-filling with help of doc that you mentioned and `understanding slot-filling` video tutorial from [this course](https://www.udemy.com/bots-with-dialogflow/) – sid8491 Jan 16 '19 at 09:00
  • How can i pass parameter for non event call in queryInput in android? @sid8491 – Subin Babu Jun 15 '19 at 06:33
0

Information in accepted answer is incorrect.

You don't have to provide default values.

You can reference event parameters directly in Value column of Parameters table.

To reference an event parameter in the parameter table or a response, use the following format: #event-name.parameter-name.

dialogflow docs

Therefore, putting #greetPerson.given-name in Value would be enough.