/// <summary> /// Once the user has made a selection then direct the user to the relevant dialog /// </summary> /// <param name="context"></param> /// <param name="argument"></param> /// <returns></returns> private async Task UserMadeSelection(IDialogContext context, IAwaitable <Triggers> argument) { // Await the user's interaction var arg = await argument; switch (arg) { case Triggers.Root: await context.PostAsync("Returning to the root menu"); this.GiveUserChoices(context); break; case Triggers.Calculator: await context.PostAsync("Welcome to the calculator"); await context.PostAsync("Please post your calculation in the form X + Y"); var calculatorDialog = DialogFactory.MakeInstance <CalculatorDialog>(); context.Wait(calculatorDialog.PerformCalculation); break; default: break; } }
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { if (activity.Type == ActivityTypes.Message) { if (activity.Text == Triggers.Calculator.ToString()) { await Conversation.SendAsync(activity, () => DialogFactory.MakeInstance <CalculatorDialog>()); } else { await Conversation.SendAsync(activity, () => DialogFactory.MakeInstance <RootDialog>()); } } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }
/// <summary> /// Allow the user to perform a calculation /// </summary> /// <param name="context"></param> /// <param name="argument"></param> /// <returns></returns> public async Task PerformCalculation(IDialogContext context, IAwaitable <object> argument) { // Await the user's interaction var activity = await argument as Activity; // Split the data by spaces var data = activity.Text.Split(' '); // If the user has added a + symbol then they want to perform an addition // TODO: Parse the data using regex to create a pattern for performing more complicated calculations if (activity.Text.Contains("+")) { var count = 0; foreach (var item in data) { int num; // If the current item is a number then perform a calculation if (int.TryParse(item, out num)) { count += num; } } // Send the calculation back to the user await context.PostAsync($"I calculated that as: {count}"); // Add this method back to the call stack for the user to make another calculation context.Wait(this.PerformCalculation); } // If the user tries to do something that isn't supported in this Dialog then display the Choices available else { await context.PostAsync("I'm sorry. I haven't been taught how to do that yet. I will return you to the main menu."); DialogFactory.MakeInstance <RootDialog>().GiveUserChoices(context); } }