1
votes

I've been working with the Microsoft Bot Framework (V.4 - node.js) for a while now and I'm starting to get the hang of things. However, I'm currently trying to figure out a way to use a variable across multiple dialogs. In one of these dialogs I make a JSON request to a REST API to fetch some data. I can use this data anywhere within the dialog, but I want to use this data in another dialog. Is there an easy to to do this?

I also know it's possible to send data along when ending a dialog like so:

 return await step.endDialog(#YourDataHere);

But found no option to send data along when starting a new dialog.

I've looked into the example: https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/javascript_nodejs/05.multi-turn-prompt But this only uses 1 dialog.

I've also looked into the code given in V3: https://docs.microsoft.com/en-us/azure/bot-service/nodejs/bot-builder-nodejs-dialog-manage-conversation-flow?view=azure-bot-service-3.0 This error message. Following this link gives a blank page without any help documentation for V 4.

Any suggestions on how to make it so that I can use my data across all the dialogs?

1

1 Answers

3
votes

You can pass data to a new dialog as the second option using

await dc.beginDialog(DIALOG_NAME, your_data);

For me, I am usually passing the LUIS recognizerResult in this slot, but in some cases where I don't need to refer back to the captured intent and entities I have passed a simple variable.

You can then access this in the dialog as part of the context. I use "step" as the context variable name, so you can find this value in step._info.options (not sure if the underscore is required).

If you're passing a simple value, it is available directly (i.e. yourData = step._info.options). If it's a JSON object, you just reference the value you're looking for as normal. For example, to get an entity I'm using myEntity = step._info.options.entities.myEntityName[0].

Do be aware of your variable scope in case you might need to reuse this value multiple times. It may be best to save this in conversationState especially if you are not immediately flowing from one dialog into the next.