Esempio n. 1
0
        public async Task AskFilm(IDialogContext context, LuisResult result)
        {
            string               replyMessage  = "";
            HttpClient           client        = new HttpClient();
            string               themoviedbUrl = "https://www.themoviedb.org/movie/";
            string               baseUri       = "https://api.themoviedb.org/3";
            string               imageUrl      = "https://image.tmdb.org/t/p/w600_and_h900_bestv2";
            string               apiKey        = "?api_key=e01be34ceffec7c3b4fc1c591884c2fc";
            EntityRecommendation rec;

            ResultModel.RootObject rootObject = new ResultModel.RootObject();

            if (result.TryFindEntity("Film", out rec))
            {
                string filmName = rec.Entity;
                replyMessage = $"Ok, let me think..";
                await context.PostAsync(replyMessage);

                Console.WriteLine(filmName);
                string searchQuery = await client.GetStringAsync(new Uri(
                                                                     baseUri
                                                                     + "/search/movie"
                                                                     + apiKey
                                                                     + "&query="
                                                                     + filmName
                                                                     ));

                rootObject = JsonConvert.DeserializeObject <ResultModel.RootObject>(searchQuery);
                if (rootObject.total_results > 0)
                {
                    int searchFilmId = rootObject.results[0].id;

                    string recommendationQuery = await client.GetStringAsync(new Uri(
                                                                                 baseUri
                                                                                 + $"/movie/{searchFilmId}/recommendations"
                                                                                 + apiKey
                                                                                 ));

                    rootObject = JsonConvert.DeserializeObject <ResultModel.RootObject>(recommendationQuery);
                    int cardsLength;
                    if (rootObject.total_results > MAX_DECK_SIZE)
                    {
                        cardsLength = MAX_DECK_SIZE;
                    }
                    else if (rootObject.total_results > 0 && rootObject.total_results < MAX_DECK_SIZE)
                    {
                        cardsLength = rootObject.total_results;
                    }
                    else
                    {
                        cardsLength = 0;
                    }

                    var reply = context.MakeMessage();
                    reply.AttachmentLayout = "carousel";
                    reply.Attachments      = new List <Attachment>();
                    for (int i = 0; i < cardsLength; i++)
                    {
                        List <CardImage>   cardImages  = new List <CardImage>();
                        List <CardAction>  cardButtons = new List <CardAction>();
                        ResultModel.Result resultFilm  = rootObject.results[i];
                        CardImage          ci          = new CardImage($"{imageUrl}{resultFilm.poster_path}");
                        cardImages.Add(ci);
                        CardAction ca = new CardAction()
                        {
                            Title = resultFilm.title,
                            Type  = "openUrl",
                            Value = $"{themoviedbUrl}{resultFilm.id}"
                        };
                        CardAction button = new CardAction()
                        {
                            Title = $"Add to watch list",
                            Type  = "postBack",
                            Value = "<ADD FILM>{"
                                    + $"id: {resultFilm.id},"
                                    + $"title: \"{resultFilm.title}\""
                                    + "}"
                        };
                        cardButtons.Add(button);
                        ThumbnailCard tc = new ThumbnailCard()
                        {
                            Title    = resultFilm.title,
                            Subtitle = resultFilm.overview,
                            Images   = cardImages,
                            Tap      = ca,
                            Buttons  = cardButtons
                        };
                        reply.Attachments.Add(tc.ToAttachment());
                    }

                    // send the cards
                    await context.PostAsync(reply);

                    context.Wait(MessageReceived);
                }
            }
            else   // No film entity detected
            {
                replyMessage = "Please name a film for me to work off";
            }

            await context.PostAsync(replyMessage);

            context.Wait(MessageReceived);
        }
Esempio n. 2
0
        public static async void RemoveFilmAsync(Activity activity, ConnectorClient connector)
        {
            HttpClient client    = new HttpClient();
            string     film      = activity.Text.Substring(13);
            JObject    filmJson  = JObject.Parse(film);
            string     filmTitle = (string)filmJson["title"];
            int        filmId    = (int)filmJson["id"];
            Activity   reply     = activity.CreateReply($"Removing \"{filmTitle}\" from your watch list..");
            await connector.Conversations.SendToConversationAsync(reply);

            UserResult userResultModel = new UserResult
            {
                userId   = activity.From.Id,
                resultId = filmId
            };

            await connector.Conversations.SendToConversationAsync(reply);

            List <UserResult> userResultList = await AzureManager.AzureManagerInstance.GetUserResults();

            List <UserResult> searchResultList = userResultList
                                                 .Where(o => o.resultId == filmId)
                                                 .Where(o => o.userId.Equals(activity.From.Id))
                                                 .ToList();
            List <UserResult> personalizedList = userResultList
                                                 .Where(o => o.userId.Equals(activity.From.Id))
                                                 .ToList();

            if (searchResultList.Count > 0)
            {
                UserResult toBeDeleted = searchResultList[0];
                await AzureManager.AzureManagerInstance.DeleteUserResult(toBeDeleted);

                reply = activity.CreateReply($"Deleted {toBeDeleted.id}! Here is your to-watch list:");

                // Display to-watch list
                reply.AttachmentLayout = "carousel";
                reply.Attachments      = new List <Attachment>();
                foreach (UserResult userResult in personalizedList)
                {
                    string apiQuery = await client.GetStringAsync(new Uri(
                                                                      BASE_URI
                                                                      + $"/movie/{userResult.resultId}"
                                                                      + API_KEY
                                                                      ));

                    ResultModel.Result resultObject = JsonConvert.DeserializeObject <ResultModel.Result>(apiQuery);
                    List <CardImage>   cardImages   = new List <CardImage>();
                    List <CardAction>  cardButtons  = new List <CardAction>();
                    CardImage          ci           = new CardImage($"{IMAGE_URL}{resultObject.poster_path}");
                    cardImages.Add(ci);
                    CardAction ca = new CardAction()
                    {
                        Title = resultObject.title,
                        Type  = "openUrl",
                        Value = $"{THE_MOVIE_DB_URL}{resultObject.id}"
                    };
                    CardAction button = new CardAction()
                    {
                        Title = $"Watched it",
                        Type  = "postBack",
                        Value = "<REMOVE FILM>{"
                                + $"id: {resultObject.id},"
                                + $"title: \"{resultObject.title}\""
                                + "}"
                    };
                    cardButtons.Add(button);
                    ThumbnailCard tc = new ThumbnailCard()
                    {
                        Title    = resultObject.title,
                        Subtitle = resultObject.overview,
                        Images   = cardImages,
                        Tap      = ca,
                        Buttons  = cardButtons
                    };
                    reply.Attachments.Add(tc.ToAttachment());
                }
            }
            else
            {
                reply = activity.CreateReply("Oh no, something went wrong :(");
            }

            await connector.Conversations.SendToConversationAsync(reply);
        }