/// <summary> /// Every conversation turn for our Echo Bot will call this method. /// There are no dialogs used, since it's "single turn" processing, meaning a single /// request and response. /// </summary> /// <param name="turnContext">A <see cref="ITurnContext"/> containing all the data needed /// for processing this conversation turn. </param> /// <param name="cancellationToken">(Optional) A <see cref="CancellationToken"/> that can be used by other objects /// or threads to receive notice of cancellation.</param> /// <returns>A <see cref="Task"/> that represents the work queued to execute.</returns> /// <seealso cref="BotStateSet"/> /// <seealso cref="ConversationState"/> /// <seealso cref="IMiddleware"/> public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken)) { if (turnContext == null) { throw new ArgumentNullException(nameof(turnContext)); } // Handle Message activity type, which is the main activity type for shown within a conversational interface // Message activities may contain text, speech, interactive cards, and binary or unknown attachments. // see https://aka.ms/about-bot-activity-message to learn more about the message and other activity types if (turnContext.Activity.Type is ActivityTypes.Message) { // Get the state properties from the turn context. UserProfile userProfile = await _accessors.UserProfileAccessor.GetAsync(turnContext, () => new UserProfile()); ConversationData conversationData = await _accessors.ConversationDataAccessor.GetAsync(turnContext, () => new ConversationData()); // Simplify request for eaasier comparison var requestContent = turnContext.Activity.Text.ToLower().Trim(); if (string.IsNullOrEmpty(userProfile.Name)) { // First time around this is set to false, so we will prompt user for name. if (conversationData.PromptedUserForName) { // Set the name to what the user provided. userProfile.Name = requestContent; // Acknowledge that we got their name. await turnContext.SendActivityAsync($"Thanks {userProfile.Name}."); // Reset the flag to allow the bot to go though the cycle again. conversationData.PromptedUserForName = false; } else { // Prompt the user for their name. await turnContext.SendActivityAsync($"What is your name?"); // Set the flag to true, so we don't prompt in the next turn. conversationData.PromptedUserForName = true; } // Save user state and save changes. await _accessors.UserProfileAccessor.SetAsync(turnContext, userProfile); await _accessors.UserState.SaveChangesAsync(turnContext); // Update conversation state and save changes. await _accessors.ConversationDataAccessor.SetAsync(turnContext, conversationData); await _accessors.ConversationState.SaveChangesAsync(turnContext); return; } // TODO CHECK, IF THERE WAS A QUESTION, WHERE NUMBER MUST BE PROVIDED AFTER // Start with those numbers if (UtilRequest.IsNumber(requestContent)) { // Take the input from the user and create the appropriate response. var reply = Images.ProcessInput(turnContext); // Respond to the user. await turnContext.SendActivityAsync(reply, cancellationToken); // Show a custom dialog with images upload await Images.DisplayOptionsAsync(turnContext, cancellationToken); return; } // Start with checking questions about the time string responseMessage = _questionsAboutTime.Respond(requestContent); // Start operating with D20 if (responseMessage == null) { responseMessage = _d20.Respond(requestContent); } // Start operating with those retorts if (responseMessage == null) { responseMessage = _retorts.Respond(requestContent); } // Start checking tranalation if (responseMessage == null) { responseMessage = _translation.Respond(requestContent); } // If answer was given as text if (responseMessage != null) { await turnContext.SendActivityAsync($"{responseMessage}"); // Show additional meta data about the message, if debug is on if (_debug) { Debug debug = new Debug(conversationData, userProfile, _accessors); debug.ShowMetaDataAsync(turnContext); } return; } // Start operating with images if (responseMessage == null && _images.IsToProcess(requestContent)) { await _images.ShowImage(turnContext.Activity, requestContent); return; } if (responseMessage == null) { await turnContext.SendActivityAsync(responseMessage ?? $"Turn: You sent '{turnContext.Activity.Text}'\n"); return; } } else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate) { if (turnContext.Activity.MembersAdded != null) { // Send a welcome message to the user and tell them what actions they may perform to use this bot await SendWelcomeMessageAsync(turnContext, cancellationToken); } } else { await turnContext.SendActivityAsync($"{turnContext.Activity.Type} event detected"); } }