Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to use the session parameters in conversational agent playbook

I am just starting out with dialogflow and trying to build a flutter e-commerce app with dialogflow playbooks.

I want it to be personalized so that different brands can ask questions only related to their data and not others.

I am sending the userid via 

Future<String> sendMessage(String sessionId, String message) async {
try {
final dialogflow = await AuthClient.getDialogflowApi();
final sessionPath =
'projects/$projectId/locations/$location/agents/$agentId/sessions/$sessionId';
final queryInput = df.GoogleCloudDialogflowCxV3QueryInput(
languageCode: 'en',
text: df.GoogleCloudDialogflowCxV3TextInput(text: message),
);

logs show it as

{
"insertId": "2a73yadnmoh",
"jsonPayload": {
"queryInput": {
"text": {
"text": "what products do I have"
},
"languageCode": "en"
},
"session": "projects/***/locations/global/agents/***/sessions/userid_is_here"
},
"resource": {
"type": "global",
"labels": {
"project_id": "***"
}
},
"timestamp": "2025-01-29T19:25:27.261958Z",
"severity": "INFO",
"labels": {
"agent_id": "***",
"environment_id": "",
"session_id": "userid_is_here",
"location_id": "global"
},
"logName": "---",
"receiveTimestamp": "2025-01-29T19:25:27.977275078Z"
}

with the userId being sent as $sessionId

Now, the Dialogflow is directly connected to a playbook, which uses the Openapi tool to send the question to a cloud function, which gets the answer from my big query database.

I have only used playbooks until now with no flows etc


How do I get the sessionId and send it to the cloud function?

0 2 1,942
2 REPLIES 2

Hi @Tayyibl,

Welcome to Google Cloud Community!

It looks like you are trying to pass your session ID (which you are using as a user ID) from Dialogflow to your cloud function. This cloud function is being used in your Dialogflow playbook through the OpenAPI tool, allowing you to personalize the data returned from BigQuery based on the user.

Here are the potential ways that might help with your use case:

  • Use User ID as Session ID: In your Flutter app, pass your user ID as the sessionId when working with Dialogflow.
  • Access Session ID in Playbook: Utilize $session.id to access the sessionId in your Dialogflow CX playbook.
  • Pass Session ID to Your Cloud Function: Include your $session.id as a parameter in your OpenAPI tool request, using either the query parameter or request body method.
  • Custom BigQuery Queries: In your cloud function, take in and utilize the userId (session ID) to craft personalized BigQuery queries.
  • Error Handling: Implement proper error handling in your cloud function and Playbook in case your session ID is not present or there is any other issue.

You may refer to the documentation below, which offers information on Google Cloud’s Cloud Run, BigQuery, and Dialogflow Sessions:

Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.

Hi @MarvinLlamas ,

Thank you for your response.

Could you please clarify how to do these steps

  • Access Session ID in Playbook: Utilize $session.id to access the sessionId in your Dialogflow CX playbook.
  • Pass Session ID to Your Cloud Function: Include your $session.id as a parameter in your OpenAPI tool request, using either the query parameter or request body method.

Ive made changes to my open API and tried to send the session id along with the question

{
  "openapi": "3.0.0",
  "info": {
    "version": "1.0.0",
    "title": "search",
    "description": "Search information about orders"
  },
  "servers": [
    {
    }
  ],
  "paths": {
    "/getanswer": {
      "post": {
        "operationId": "getanswer",
        "description": "Search information about orders",
        "summary": "Search information about orders",
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/SearchRequest"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Search results.",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/SearchResult"
                }
              }
            }
          },
          "400": {
            "description": "Bad request (e.g., missing session_id or question).",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "response": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal server error.",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "response": {
                      "type": "string"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "SearchRequest": {
        "type": "object",
        "properties": {
          "question": {
            "type": "string"
          },
          "session_id": {
            "type": "string"
          }
        },
        "required": [
          "question",
          "session_id"
        ]
      },
      "SearchResult": {
        "type": "object",
        "properties": {
          "response": {
            "type": "string"
          }
        }
      }
    }
  }
}
 
I've also edited my examples and made some others with the requestBody as
{
  "session_id": "$session.id",
  "question": "what is your best selling product"
}

but when I do this the cloud function doesn't get the session id dynamically it just gets the string '$session.id'

I also tried the example 
{
  "session_id": "1234abcd",
  "question": "what is your best selling product"
}
 
trying to mimic the id, but it still only sent the string '1234abcd'.
 
I don't have any flows currently, the chat is connected to the default generative playbook which then connects to the Product Inquiry playbook, which uses the tool to give the answers.
 
 
If you could please guide on what I should do to send the session id to the cloud function?