How to get a sessionId- dialogflow

3.6k Views Asked by At

I am trying to get sessionId from response object but is that an efficient way or are there any other possibilities to get the session Id or sessions object?

For example: request.body.session: projects/coffee-shop/agent/sessions/e6eb1812-9c3f-23fa-b590-f1656ee9a56e

How to get exact ID:e6eb1812-9c3f-23fa-b590-f1656ee9a56e instead of path. or How to get the sessions object in the path projects/coffee-shop/agent/**sessions**/e6eb1812-9c3f-23fa-b590-f1656ee9a56e

2

There are 2 best solutions below

2
On

Yes, you can grab the session id off the agent instance of WebhookClient:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);

    let sessionId = agent.session;
  }

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  agent.handleRequest(intentMap);
  });
0
On

Assuming we have request object in variable req, we can use below python code:

session_path = req['session']  

it will print session object path:
projects/coffee-shop/agent/sessions/e6eb1812-9c3f-23fa-b590-f1656ee9a56e

to get the exact id, you can just split it with / and take the last element of the list

session = req['session'].split('/')[-1]  

it will print exact session id:
e6eb1812-9c3f-23fa-b590-f1656ee9a56e