Pass Custom Context Variables to the Bot

With variables you can optionally pass custom context variables to the bot when initiating a bot session. See What’s a Variable?. Let’s say you want the bot to greet the customer with their name. First, configure a CustomerName context variable in the Bot Builder.

Pass custom context variables to the bot

Then create a variables list and add the context variable with the value. Include this in the initial BotSendMessageRequest() object when you start the session.

1List<AnyVariable> variables = Collections
2    .singletonList(new TextVariable()
3        .name("CustomerName") // Context variableName
4        .type(TypeEnum.TEXT)
5        .value("Jane Doe")// Context variableValue
6    );
7
8BotSendMessageRequest botSendInitMessageRequest = BotRequest
9    .withMessage(message)
10    .variables(variables)
11    .requestId(UUID.randomUUID().toString())
12    .build();

You can then configure the greeting message to show the customer’s name. Here is the bot’s response message using the CustomerName context variable with the value “Jane Doe”.

1Hi Jane Doe,
2I’m a demo bot for the user guide.
3Choose one of the options below
41.Order Status
52.Frequently Asked Questions
63.Transfer To Agent
74.End Chat