Tyler Liu
Tyler Liu

Reputation: 1069

Microsoft Bot Framework using variables in Prompts.choice

My current code for asking a yes/no question is

 builder.Prompts.choice(session, q15, "Yes|No", { listStyle: builder.ListStyle.button });

I want to store the strings "Yes" and "No" in a JSON file and access them by their variable names, instead of hardcoding it. How can I do that?

Upvotes: 1

Views: 138

Answers (1)

Gary Liu
Gary Liu

Reputation: 13918

As the definition of choice():

choice(session: Session, prompt: TextOrMessageType, choices: string|Object|string[]|IChoice[], options?: IPromptChoiceOptions): void;

You can pass a string array as the choices. So please consider the following code snippet:

//assume you have read the json string from a file and use it as following
const json_string = `{
    "VARIABLE_YES":"Yes",
    "VARIABLE_NO":"No"
}`;

const json_obj = JSON.parse(json_string);
builder.Prompts.choice(session, 'Make a choice', [json_obj.VARIABLE_YES, json_obj.VARIABLE_NO], {
    listStyle: builder.ListStyle.button
});

Upvotes: 1

Related Questions