public async Task StartAsync(IDialogContext context) { await context.PostAsync($"Welcome to Trivia, Let's play..."); // post the question and choices as a hero card _game = new TriviaGame(""); await context.PostAsync(_game.CurrentQuestion().Question); await context.PostAsync(MakeChoiceCard(context, _game.CurrentQuestion())); // wait for input context.Wait(MessageReceivedAsync); }
/// <summary> /// Here we'll check the users answer and post the next question until there are no more questions /// </summary> /// <param name="context">The current chat context</param> /// <param name="result">The IAwaitable result</param> /// <returns></returns> private async Task MessageReceivedAsync(IDialogContext context, IAwaitable <object> result) { var activity = (IMessageActivity)await result; int usersAnswer = -1; if (int.TryParse(activity.Text, out usersAnswer)) { await context.PostAsync($"You chose: {activity.Text}"); if (_game.Answer(usersAnswer)) { await context.PostAsync("Correct!"); } else { await context.PostAsync("Sorry, that's wrong :-("); } await context.PostAsync($"Your score is: {_game.Score()}/{_game._questions.Count}. Next question!"); TriviaQuestion nextQuestion = _game.MoveToNextQuestion(); if (nextQuestion != null) { // post the question in separate message so that it doesn't get cut off await context.PostAsync(_game.CurrentQuestion().Question); await context.PostAsync(MakeChoiceCard(context, nextQuestion)); context.Wait(MessageReceivedAsync); } else { await context.PostAsync("That's it! Thanks for playing :-)"); // see if the user will take our survey PromptDialog.Confirm(context, AfterAskAboutSurvey, new PromptOptions <string>( "Would you like to take a survey?", "Sorry, I didnt't get that", "Hmm...it seems I am having some difficult, let's forget about that survey.", null, 3)); } } else { try { // send to luis to see if we can pick up the users intet await context.Forward(new MyLuisDialog(), AfterLuis, activity, CancellationToken.None); } catch (Exception e) { await context.PostAsync($"Tried to start LUIS but got: {e.Message}"); } } }
/// <summary> /// Here we'll set the players name returned from the prompt and then initialize the game /// </summary> /// <param name="context"></param> /// <param name="result"></param> /// <returns></returns> private async Task AfterName(IDialogContext context, IAwaitable <object> result) { // we stored the user's name in the bot session data in RootDialog, now we will retrieve it IMessageActivity activity = (IMessageActivity)await result; StateClient stateClient = activity.GetStateClient(); // get the current user data BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id); // upate/set a property called 'name' string name = userData.GetProperty <string>("Name"); _game = new TriviaGame(name); await context.PostAsync($"Ready or not, {name}, Let's play!"); // post the question in separate message so that it doesn't get cut off await context.PostAsync(_game.CurrentQuestion().Question); // most the question and choices as a hero card await context.PostAsync(MakeChoiceCard(context, _game.CurrentQuestion())); // wait for the answer context.Wait(MessageReceivedAsync); }