Retrieve User Context

Problem

You need to retrieve data from the user’s context for use in your existing flow.

Solution

There are two ways that you can retrieve and store input as context. 1. Use the Change node to set the taken input and store it for later use as a $referenceable variable. Click here for an example using a Change node. 2. Use the Switch node to check if expected context is already there. If the context is null, then you can use a Change node as described previously, otherwise, you can use the context that is there.

Discussion

User context lives at msg.payload.context. The change node can be used to set the value msg.payload.user.name, to msg.payload.context.name, so that it can be referenced as $name. For example, create a Dialogue node which asks the user for their name, connect it to an Input node, and connect that Input node to a setContext node that looks something like this:

Now connect that setContext node to a Change node:

You can now reference the user’s name by calling $name. For example in a Dialogue node placed after the Change node, you could now put:

If msg.payload.context.name is not always available a switch node checking that it is not null can be use to switch up what message is used:

Example using a Switch node

Save user name

[{"id":"e7f9ac15.f89fa8","type":"start","z":"36a987e3.d6314","name":"Start","x":160,"y":240,"wires":[["8a9e250.5133cd8"]]},{"id":"59d7df2f.898b4","type":"change","z":"36a987e3.d6314","name":"","rules":[{"t":"set","p":"payload.user.name","pt":"msg","to":"payload.context.name","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":500,"y":220,"wires":[["abf6cd38.6e3088"]]},{"id":"abf6cd38.6e3088","type":"dialogue","z":"36a987e3.d6314","name":"","message":"Hi there $name!","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":760,"y":220,"wires":[[]]},{"id":"8a9e250.5133cd8","type":"switch","z":"36a987e3.d6314","name":"","property":"payload.context.name","propertyType":"msg","rules":[{"t":"nnull"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":290,"y":240,"wires":[["59d7df2f.898b4"],["f7000c3e.64262"]]},{"id":"f7000c3e.64262","type":"dialogue","z":"36a987e3.d6314","name":"","message":"Hi there!","messageType":"str","displayTimer":1.5,"enableDisplay":false,"x":760,"y":260,"wires":[[]]}]