Ejemplo n.º 1
0
        public async Task MessageReceivedAsync(IDialogContext Context, IAwaitable <IMessageActivity> Argument)
        {
            // This task handles all of the message returning.  Can be considered the master function for the bot.
            var Message = await Argument;

            // Post the welcome message when somebody new joins the conversation
            if (Message.Type == ActivityTypes.ConversationUpdate)
            {
                await PostWelcomeMessage(Context, Message);
            }
            else if (Message.Type == ActivityTypes.Message)
            {
                // Here we have LUIS interpret the query
                LUISRequest Request = new LUISRequest();
                await Request.MakeRequest(Message.Text.ToString());

                // Now we parse the data to figure out what to search for.
                DataParser LUISData = new DataParser(Request.LUISResult);
                LUISData.LUISParse();

                // Check to see if LUIS understood the query
                if (LUISData.LUISParsed.entities.Count == 0)
                {
                    await Context.PostAsync("I'm sorry, I don't understand your message.");
                }
                else
                {
                    // It is time to search the ISBN database for the information.
                    SearchISBNdB Search  = new SearchISBNdB();
                    String       Results = Search.GetJSONData(LUISData.LUISParsed.topScoringIntent.intent, LUISData.LUISParsed.entities);

                    // Now parse it to make it readable.
                    DataParser ISBNData = new DataParser(Results);
                    ISBNData.DBParse(LUISData.LUISParsed.topScoringIntent.intent);

                    // Check the intent and post the appropriate card.
                    TextInfo Info = new CultureInfo("en-US", false).TextInfo;
                    if (LUISData.LUISParsed.topScoringIntent.intent == LUISConstants.SearchByAuthorForBooks)
                    {
                        await PostAuthorCard(Context, LUISData.LUISParsed.entities, ISBNData, Info);
                    }
                    else if (LUISData.LUISParsed.topScoringIntent.intent == LUISConstants.SearchByBookForAuthor)
                    {
                        await PostBookCard(Context, LUISData.LUISParsed.entities, ISBNData, Info);
                    }
                    else if (LUISData.LUISParsed.topScoringIntent.intent == LUISConstants.SearchByBookForSynopsis)
                    {
                        await PostSynopsisCard(Context, LUISData.LUISParsed.entities, ISBNData, Info);
                    }
                }
            }
            Context.Wait(MessageReceivedAsync);
        }
Ejemplo n.º 2
0
        public String GetJSONData(String Intent, List<Entity> Entities)
        {
            String Results = null;
            try
            {
                // To determine what kind of search we need to do, look at the Intent
                String ISBNUriStr = string.Empty;
                TextInfo Info = new CultureInfo("en-US", false).TextInfo;
                if (Intent == LUISConstants.SearchByAuthorForBooks)
                {
                    foreach (var Item in Entities)
                    {
                        if (Item.type == LUISConstants.Author)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/author/{0}", Info.ToTitleCase(Item.entity));
                            break;
                        }
                    }
                }
                else if (Intent == LUISConstants.SearchByBookForAuthor || Intent == LUISConstants.SearchByBookForSynopsis)
                {
                    foreach (var Item in Entities)
                    {
                        if (Item.type == LUISConstants.Book)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/books/{0}", Info.ToTitleCase(Item.entity));
                            break;
                        }
                    }
                }

                // Format the webrequest for the ISBN database.
                Uri Uri = new Uri(ISBNUriStr);

                WebRequest Http = WebRequest.Create(Uri);
                Http.Method = "GET";
                Http.ContentType = "application/json";
                Http.Headers["X-API-KEY"] = Keys.ISBNdBAccessKey;
                WebResponse Response = Http.GetResponse();
                Stream Stream = Response.GetResponseStream();

                StreamReader Reader = new StreamReader(Stream);
                Results = Reader.ReadToEnd();

                // For some unknown reason, when searching for books written by an author, some of the books will have synopsis or overview
                // details available, but when a book is searched these details are always absent.  So if the intent is SearchByBookForSynopsis
                // there must be another database search.
                // This seems to be an issue with the database itself, and the way it chooses to return data.
                if (Intent == LUISConstants.SearchByBookForSynopsis)
                {
                    // Grab the author of the book.
                    DataParser Data = new DataParser(Results);
                    Data.DBParse(Intent);

                    // Have to do this because the author data can be incomplete on some books.
                    foreach (var Book in Data.BooksReturned.books)
                    {
                        if (Book.authors.Count != 0)
                        {
                            ISBNUriStr = String.Format("https://api.isbndb.com/author/{0}", Book.authors[0]);
                            break;
                        }
                    }

                    // Now do the search for the author.
                    Uri = new Uri(ISBNUriStr);

                    Http = WebRequest.Create(Uri);
                    Http.Method = "GET";
                    Http.ContentType = "application/json";
                    Http.Headers["X-API-KEY"] = Keys.ISBNdBAccessKey;
                    Response = Http.GetResponse();
                    Stream = Response.GetResponseStream();

                    Reader = new StreamReader(Stream);
                    Results = Reader.ReadToEnd();
                }
                return Results;
            }
            catch (Exception e) { string exception = e.Message; return exception; }
        }