1

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

sid8491
  • 6,622
  • 6
  • 38
  • 64
manu
  • 21
  • 1
  • 4
  • show us your code how are you doing it, then only we can tell – sid8491 Oct 05 '18 at 05:14
  • Sorry i cant show you entire code but this is how i am trying to access session: request.body.session gives me this 'projects/coffe-shop/agent/sessions/e6eb1812-9c3f-23fa-b590-f1656ee9a56e'. so this would be the sesson id: e6eb1812-9c3f-23fa-b590-f1656ee9a56e but its wierd that we have to parse this string to get session id. – manu Oct 05 '18 at 17:22
  • Why can't you just keep the whole string? It's a bit convoluted but it does have the unique identifier you want – Nick Felker Oct 05 '18 at 17:31
  • where ever I search for how to get a session Id no one has a clean way to get it so just trying because the application we are trying to do need just the session Id, not the whole string. there must be someway! – manu Oct 05 '18 at 18:11

2 Answers2

4

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);
  });
Sarah Dwyer
  • 529
  • 5
  • 22
2

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

sid8491
  • 6,622
  • 6
  • 38
  • 64