Пример #1
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            if (ConversationData.PromptedUserForName)
            {
                ConversationData.PromptedUserForName = false;
                await turnContext.SendActivityAsync($"Bye");
            }
            else
            {
                // preserve user input.
                var utterance = turnContext.Activity.Text;
                // make empty local logitems list.
                UtteranceLog logItems = null;

                // see if there are previous messages saved in storage.
                try
                {
                    string[] utteranceList = { "UtteranceLog" };
                    logItems = _myStorage.ReadAsync <UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;
                }
                catch
                {
                    // Inform the user an error occured.
                    await turnContext.SendActivityAsync("Sorry, something went wrong reading your stored messages!");
                }
                // If no stored messages were found, create and store a new entry.
                if (logItems is null)
                {
                    // add the current utterance to a new object.
                    logItems = new UtteranceLog();
                    logItems.UtteranceList.Add(utterance);
                    // set initial turn counter to 1.
                    logItems.TurnNumber++;

                    // Show user new user message.
                    //  await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                    // Create Dictionary object to hold received user messages.
                    var changes = new Dictionary <string, object>();
                    {
                        changes.Add("UtteranceLog", logItems);
                    }
                    try
                    {
                        // Save the user message to your Storage.
                        await _myStorage.WriteAsync(changes, cancellationToken);
                    }
                    catch
                    {
                        // Inform the user an error occured.
                        await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                    }
                }
                // Else, our Storage already contained saved user messages, add new one to the list.
                else
                {
                    // add new message to list of messages to display.
                    logItems.UtteranceList.Add(utterance);
                    // increment turn counter.
                    logItems.TurnNumber++;

                    // show user new list of saved messages.
                    //  await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                    // Create Dictionary object to hold new list of messages.
                    var changes = new Dictionary <string, object>();
                    {
                        changes.Add("UtteranceLog", logItems);
                    };

                    try
                    {
                        // Save new list to your Storage.
                        await _myStorage.WriteAsync(changes, cancellationToken);
                    }
                    catch
                    {
                        // Inform the user an error occured.
                        await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                    }
                }
                await _myTranscripts.LogActivityAsync(turnContext.Activity);

                List <string> storedTranscripts = new List <string>();
                PagedResult <Microsoft.Bot.Builder.TranscriptInfo> pagedResult = null;
                var pageSize = 0;
                do
                {
                    pagedResult = await _myTranscripts.ListTranscriptsAsync("emulator", pagedResult?.ContinuationToken);

                    pageSize = pagedResult.Items.Count();

                    // transcript item contains ChannelId, Created, Id.
                    // save the channelIds found by "ListTranscriptsAsync" to a local list.
                    foreach (var item in pagedResult.Items)
                    {
                        storedTranscripts.Add(item.Id);
                    }
                } while (pagedResult.ContinuationToken != null);


                Logger.LogInformation("Running dialog with Message Activity.");

                // Run the Dialog with the new message Activity.
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty <DialogState>(nameof(DialogState)), cancellationToken);
            }
        }
Пример #2
0
        /* Method from Microsoft Bot Framework doccumentation*/
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // preserve user input.
            var utterance = turnContext.Activity.Text;
            // make empty local logitems list.
            UtteranceLog logItems = null;

            // see if there are previous messages saved in storage.
            string[] utteranceList = { "UtteranceLog" };
            logItems = query.ReadAsync <UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;


            // If no stored messages were found, create and store a new entry.
            if (logItems is null)
            {
                // add the current utterance to a new object.
                logItems = new UtteranceLog();
                logItems.UtteranceList.Add(utterance);
                // set initial turn counter to 1.
                logItems.TurnNumber++;

                // Create Dictionary object to hold received user messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                }
                try
                {
                    // Save the user message to your Storage.
                    await query.WriteAsync(changes, cancellationToken);
                }
                catch {
                }
            }
            // Else, our Storage already contained saved user messages, add new one to the list.
            else
            {
                // add new message to list of messages to display.
                logItems.UtteranceList.Add(utterance);
                // increment turn counter.
                logItems.TurnNumber++;

                // Create Dictionary object to hold new list of messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                };

                try
                {
                    // Save new list to your Storage.
                    await query.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    //await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                }
            }

            Logger.LogInformation("Running dialog with Message Activity.");

            // Run the Dialog with the new message Activity.
            await Dialog.RunAsync(turnContext, ConversationState.CreateProperty <DialogState>("DialogState"), cancellationToken);
        }
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // preserve user input.
            var utterance = turnContext.Activity.Text;
            // make empty local logitems list.
            UtteranceLog logItems = null;
            // Object to help in manipulating the search results object.
            StringBuilder searchresults = new StringBuilder();

            // see if there are previous messages saved in storage.
            try
            {
                string[] utteranceList = { "UtteranceLog" };
                logItems = _myStorage.ReadAsync <UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;
            }
            catch
            {
                // Inform the user an error occured.
                await turnContext.SendActivityAsync(
                    Speak($"Echo: Sorry, something went wrong reading your stored messages!"), cancellationToken);
            }

            // If no stored messages were found, create and store a new entry.
            if (logItems is null)
            {
                // add the current utterance to a new object.
                logItems = new UtteranceLog();
                logItems.UtteranceList.Add(utterance);
                // set initial turn counter to 1.
                logItems.TurnNumber++;

                await turnContext.SendActivityAsync(Speak($"Echo: {turnContext.Activity.Text}"), cancellationToken);

                // Create Dictionary object to hold received user messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                }
                try
                {
                    // Save the user message to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    ProcessSpeechForResults(turnContext,
                                            "Echo: Sorry, something went wrong reading your stored messages!", cancellationToken);
                }
            }
            // Else, our Storage already contained saved user messages, add new one to the list.
            else
            {
                // add new message to list of messages to display.
                logItems.UtteranceList.Add(utterance);
                // increment turn counter.
                logItems.TurnNumber++;

                //Pass the utterance to the LUIS endpoint and furthur to Azure Search for processing.
                DocumentSearchResult <dynamic> searchResults = await ProcessUserUtterance(utterance);

                //After LUIS and Azure Search processing, provide results if any back to the Bot.

                if (searchResults != null)
                {
                    if (searchResults.Results.Count > 0)
                    {
                        searchResults.Results.ToList <SearchResult <dynamic> >()
                        .ForEach(x =>
                        {
                            searchresults.Append("Bike " + x.Document["BikeName"].ToString() + " with price "
                                                 + x.Document["BaseRate"].ToString() + " Dollars and Color "
                                                 + x.Document["Color"].ToString() + ". ");
                        }
                                 );
                        ProcessSpeechForResults(turnContext,
                                                searchresults.ToString(), cancellationToken);
                    }
                    else
                    {
                        ProcessSpeechForResults(turnContext,
                                                "Echo: Sorry, unable to find any bikes. Please try a different query.", cancellationToken);
                    }
                }
                else
                {
                    ProcessSpeechForResults(turnContext,
                                            "Echo: Sorry, unable to find any bikes. Please try a different query.", cancellationToken);
                }

                // Create Dictionary object to hold new list of messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                };

                try
                {
                    // Save new list to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    ProcessSpeechForResults(turnContext,
                                            "Echo: Sorry, something went wrong reading your stored messages!", cancellationToken);
                }
            }
        }
Пример #4
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            Logger.LogInformation("Running dialog with Message Activity.");

            // preserve user input.
            var utterance = turnContext.Activity.Text;
            // make empty local logitems list.
            UtteranceLog logItems = null;

            // see if there are previous messages saved in storage.
            try
            {
                string[] utteranceList = { "UtteranceLog" };
                logItems = _myStorage.ReadAsync <UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;
            }
            catch
            {
                // Inform the user an error occured.
                await turnContext.SendActivityAsync("Sorry, something went wrong reading your stored messages!");
            }

            // If no stored messages were found, create and store a new entry.
            if (logItems is null)
            {
                // add the current utterance to a new object.
                logItems = new UtteranceLog();
                logItems.UtteranceList.Add(utterance);
                // set initial turn counter to 1.
                logItems.TurnNumber++;

                // Show user new user message.
                await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                // Create Dictionary object to hold received user messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                }
                try
                {
                    // Save the user message to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                }
            }
            // Else, our Storage already contained saved user messages, add new one to the list.
            else
            {
                // add new message to list of messages to display.
                logItems.UtteranceList.Add(utterance);
                // increment turn counter.
                logItems.TurnNumber++;

                // show user new list of saved messages.
                await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                // Create Dictionary object to hold new list of messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                };

                try
                {
                    // Save new list to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                }
            }



            AddConversationReference(turnContext.Activity as Activity);

            var conversationStateAccessors = ConversationState.CreateProperty <ConversationData>(nameof(ConversationData));
            var conversationData           = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData());

            var userStateAccessors = UserState.CreateProperty <UserProfile>(nameof(UserProfile));
            var userProfile        = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());

            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 = turnContext.Activity.Text?.Trim();

                    // Acknowledge that we got their name.
                    await turnContext.SendActivityAsync($"Thanks, {userProfile.Name}!");
                }
                else
                {
                    // Prompt the user for their name.
                    await turnContext.SendActivityAsync($"Hello! What is your name?");

                    // Set the flag to true, so we don't prompt in the next turn.
                    conversationData.PromptedUserForName = true;
                }
            }
            else
            {
                // Run the Dialog with the new message Activity.
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty <DialogState>("DialogState"), cancellationToken);
            }
        }
        // Echo back user input.
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // preserve user input.
            var utterance = turnContext.Activity.Text;
            // make empty local logitems list.
            UtteranceLog logItems = null;

            // see if there are previous messages saved in storage.
            try
            {
                string[] utteranceList = { "UtteranceLog" };
                logItems = _myStorage.ReadAsync <UtteranceLog>(utteranceList).Result?.FirstOrDefault().Value;
            }
            catch
            {
                // Inform the user an error occured.
                await turnContext.SendActivityAsync("Sorry, something went wrong reading your stored messages!");
            }

            // If no stored messages were found, create and store a new entry.
            if (logItems is null)
            {
                // add the current utterance to a new object.
                logItems = new UtteranceLog();
                logItems.UtteranceList.Add(utterance);
                // set initial turn counter to 1.
                logItems.TurnNumber++;

                // Show user new user message.
                await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                // Create Dictionary object to hold received user messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                }
                try
                {
                    // Save the user message to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                }
            }
            // Else, our Storage already contained saved user messages, add new one to the list.
            else
            {
                // add new message to list of messages to display.
                logItems.UtteranceList.Add(utterance);
                // increment turn counter.
                logItems.TurnNumber++;

                // show user new list of saved messages.
                await turnContext.SendActivityAsync($"{logItems.TurnNumber}: The list is now: {string.Join(", ", logItems.UtteranceList)}");

                // Create Dictionary object to hold new list of messages.
                var changes = new Dictionary <string, object>();
                {
                    changes.Add("UtteranceLog", logItems);
                };

                try
                {
                    // Save new list to your Storage.
                    await _myStorage.WriteAsync(changes, cancellationToken);
                }
                catch
                {
                    // Inform the user an error occured.
                    await turnContext.SendActivityAsync("Sorry, something went wrong storing your message!");
                }
            }
        }