问题
how to get the value "yes" or "no" stored into a variable? i am trying to store the value yes or no when a user clicks the button but it is not taking the value into k
var activity1 = MessageFactory.SuggestedActions(
new CardAction[]
{
new CardAction(title: "YES", type: ActionTypes.ImBack,value:"yes"),
new CardAction( title: "NO", type: ActionTypes.ImBack, value: "no")
}, text: "Do you want to continue Shopping?");
var k=await context.SendActivityAsync(activity1);
回答1:
Unfortunately, context.SendActivityAsync() does not return the value that the user input, it simply sends the activity without even waiting for the user to respond.
There's a couple of options to accomplish what you're looking for.
1. Keep Your Code As-Is
You can keep your code mostly like it is and access the value of the user input within OnTurnAsync()
with something like:
var k = turnContext.Activity.Text
What you do from there is up to you. The Simple Prompt Sample shows you how you can do this, too.
2. Use a Waterfall Dialog
I'd recommend using a ChoicePrompt
, instead of CardAction
s.
Relevant code for you would be something like (note that this would be in addition to other necessary WaterfallDialog code):
private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
return await stepContext.PromptAsync(
"choicePrompt",
new PromptOptions
{
Prompt = MessageFactory.Text("Select an Action"),
Choices = new List<Choice> { new Choice("Yes"), new Choice("No") },
});
}
private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
{
var k = ((FoundChoice)stepContext.Result).Value.ToString();
return await stepContext.NextAsync();
}
The Basic Bot Sample has very good examples of using Waterfall Dialogs.
Other References
- Prompting Users for Input
- Sequential Conversation Flow
- Getting Input from Dialog Prompts
来源:https://stackoverflow.com/questions/55045440/getting-value-stored-in-a-variable-in-botframework-suggested-actions