Search code examples
pythondialogflow-cx

How to start a conversation using an event in Dialogflow cx sending by python


I'm trying to start a conversation from dialogflow cx python API. I have seen this question Start a conversation at the beginning of a flow using flow ID that solves the problem using Node.js but I'm not able to replicate in python. In my code I have:

text_input = session.TextInput(text=msg)
query_input = session.QueryInput(text=text_input, language_code=language_code)
request = session.DetectIntentRequest(session=session_path, query_input=query_input)
response = session_client.detect_intent(request=request)

I would like to change session.TextInput() to session.EventInput, for example, as here but it does not work with dialogflow CX and the library dialogflowcx_v3beta1


Solution

  • To use an event as query_input, you should use type EventInput() in your QueryInput(). See code below on how to implement it.

    from google.cloud import dialogflowcx_v3beta1 as dialogflow
    from google.cloud.dialogflowcx_v3beta1 import types
    
    import uuid
    
    project_id = "your-project"
    location = "us-central1" # my project is located here hence us-central1
    session_id = uuid.uuid4()
    agent_id = "999999-aaaa-aaaa" # to get your agent_id see https://stackoverflow.com/questions/65389964/where-can-i-find-the-dialogflow-cx-agent-id
    session_path = f"projects/{project_id}/locations/{location}/agents/{agent_id}/sessions/{session_id}"
    
    
    api_endpoint = f"{location}-dialogflow.googleapis.com"
    client_options = {"api_endpoint": api_endpoint}
    client = dialogflow.services.sessions.SessionsClient(client_options=client_options)
    
    event = "custom_event"
    event_input = types.EventInput(event=event)
    query_input = types.QueryInput(event=event_input,language_code="en-US")
    request = types.DetectIntentRequest(
            session=session_path,query_input=query_input
            )
    response = client.detect_intent(request=request)
    
    print(response.query_result.response_messages[0])
    

    Custom event:

    enter image description here

    Output using the custom event created:

    enter image description here