private void SaveLog(Activity activity) { // ************************* // Log to Database // ************************* // Instantiate the BotData dbContext Models.BotDataEntities DB = new Models.BotDataEntities(); // Create a new UserLog object Models.UserLog NewUserLog = new Models.UserLog(); // Set the properties on the UserLog object NewUserLog.Channel = activity.ChannelId; NewUserLog.UserID = activity.From.Id; NewUserLog.UserName = activity.From.Name; NewUserLog.created = DateTime.UtcNow; NewUserLog.Message = activity.Text.Truncate(500); try { // Add the UserLog object to UserLogs DB.UserLogs.Add(NewUserLog); } catch (Exception ex) { throw ex; } // Save the changes to the database DB.SaveChanges(); }
private async Task PlayAgainAsync(IDialogContext context, IAwaitable <bool> result) { // Generate new random number Random random = new Random(); this.intNumberToGuess = random.Next(1, 6); // Reset attempts this.intAttempts = 1; // Get the response from the user var confirm = await result; if (confirm) // They said yes { // Start a new Game // Create a response // This time call the ** ShowButtons ** method Activity replyToConversation = ShowButtons(context, "Hi Welcome! - Guess a number between 1 and 5 \n\n Type 'High Scores' to see high scores"); // ************************* // Log to Database // ************************* // Instantiate the BotData dbContext Models.BotDataEntities DB = new Models.BotDataEntities(); // Create a new UserLog object Models.UserLog NewUserLog = new Models.UserLog(); // Set the properties on the UserLog object NewUserLog.Channel = replyToConversation.ChannelId; NewUserLog.UserID = replyToConversation.From.Id; NewUserLog.UserName = replyToConversation.From.Name; NewUserLog.created = DateTime.UtcNow; // This logs the message being sent to the user NewUserLog.Message = replyToConversation.Text.Truncate(500); // Add the UserLog object to UserLogs DB.UserLogs.Add(NewUserLog); // Save the changes to the database DB.SaveChanges(); await context.PostAsync(replyToConversation); context.Wait(MessageReceivedAsync); } else // They said no { await context.PostAsync("Goodbye!(heart)"); context.Wait(MessageReceivedAsync); } }
public virtual async Task MessageReceivedAsync( IDialogContext context, IAwaitable <IMessageActivity> argument) { // Set BaseURL context.UserData.TryGetValue <string>( "CurrentBaseURL", out strBaseURL); int intGuessedNumber; // Get the text passed var message = await argument; // See if a number was passed if (!int.TryParse(message.Text, out intGuessedNumber)) { // A number was not passed // Create a reply Activity Activity replyToConversation = (Activity)context.MakeMessage(); replyToConversation.Recipient = replyToConversation.Recipient; replyToConversation.Type = "message"; string strNumberGuesserCard = String.Format(@"{0}/{1}", strBaseURL, "Images/NumberGuesserCard.png?tick=123"); List <CardImage> cardImages = new List <CardImage>(); cardImages.Add(new CardImage(url: strNumberGuesserCard)); // Create the Buttons // Call the CreateButtons utility method List <CardAction> cardButtons = CreateButtons(); // Create the Hero Card // Set the image and the buttons HeroCard plCard = new HeroCard() { Images = cardImages, Buttons = cardButtons, }; // Create an Attachment by calling the // ToAttachment() method of the Hero Card Attachment plAttachment = plCard.ToAttachment(); // Attach the Attachment to the reply replyToConversation.Attachments.Add(plAttachment); // set the AttachmentLayout as 'list' replyToConversation.AttachmentLayout = "list"; // Send the reply // Create text for a reply message replyToConversation.Text = ($"Hello {message.From.Name}!.Below is simple game.Take it easy!"); await context.PostAsync(replyToConversation); context.Wait(MessageReceivedAsync); } // This code will run when the user has entered a number if (int.TryParse(message.Text, out intGuessedNumber)) { // A number was passed // See if it was the correct number if (intGuessedNumber != this.intNumberToGuess) { // The number was not correct this.intAttempts++; // Create a response // This time call the ** ShowButtons ** method Activity replyToConversation = ShowButtons(context, "Not correct. Guess again."); // ************************* // Log to Database // ************************* // Instantiate the BotData dbContext Models.BotDataEntities DB = new Models.BotDataEntities(); // Create a new UserLog object Models.UserLog NewUserLog = new Models.UserLog(); // Set the properties on the UserLog object NewUserLog.Channel = replyToConversation.ChannelId; NewUserLog.UserID = replyToConversation.From.Id; NewUserLog.UserName = replyToConversation.From.Name; NewUserLog.created = DateTime.UtcNow; // This logs the message being sent to the user NewUserLog.Message = replyToConversation.Text.Truncate(500); // Add the UserLog object to UserLogs DB.UserLogs.Add(NewUserLog); // Save the changes to the database DB.SaveChanges(); await context.PostAsync(replyToConversation); context.Wait(MessageReceivedAsync); } else { // Game completed StringBuilder sb = new StringBuilder(); sb.Append("Congratulations! "); sb.Append("The number to guess was {0}. "); sb.Append("You needed {1} attempts. "); sb.Append("Would you like to play again?"); string CongratulationsStringPrompt = string.Format(sb.ToString(), this.intNumberToGuess, this.intAttempts); // ************************* // Log to Database // ************************* // Create a reply Activity Activity replyToConversation = (Activity)context.MakeMessage(); // Instantiate the BotData dbContext Models.BotDataEntities DB = new Models.BotDataEntities(); // Create a new UserLog object Models.UserLog NewUserLog = new Models.UserLog(); // Set the properties on the UserLog object NewUserLog.Channel = replyToConversation.ChannelId; NewUserLog.UserID = replyToConversation.From.Id; NewUserLog.UserName = replyToConversation.From.Name; NewUserLog.created = DateTime.UtcNow; // This logs the message being sent to the user NewUserLog.Message = CongratulationsStringPrompt.Truncate(500); // Log the number of turns it took to win NewUserLog.CountOfTurnsToWin = this.intAttempts; // Log the name of the user who won NewUserLog.WinnerUserName = replyToConversation.Recipient.Name; // Add the UserLog object to UserLogs DB.UserLogs.Add(NewUserLog); // Save the changes to the database DB.SaveChanges(); // Put PromptDialog here PromptDialog.Confirm( context, PlayAgainAsync, CongratulationsStringPrompt, "Didn't get that!"); } } }