public async Task GetBingImageUrlTest()
        {
            string bingMapsKey = Configuration["BingMapsAPI"];

            System.Console.WriteLine("BingMapsKey: " + bingMapsKey);
            BingMapService      service = new BingMapService(bingMapsKey);
            DailyChallengeEntry entry   = await service.GetLocationDetails("Nevada", null);

            Assert.IsTrue(entry.latitude != 0 && entry.longitude != 0);
        }
Esempio n. 2
0
        private async Task <DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var command = stepContext.Result.ToString();

            if (command.ToLower().Contains("choose image"))
            {
                int imageIndex = await GetImageIndex(stepContext);

                BingImageService    imageService = new BingImageService();
                DailyChallengeImage image        = await tableService.getDailyChallengeImage();

                BingMapService mapService = new BingMapService(Configuration["BingMapsAPI"]);
                Logger.LogInformation("Image Text: " + image.ImageText);
                DailyChallengeEntry challengeEntry = await mapService.GetLocationDetails(image.ImageText, Logger);

                if (challengeEntry == null)
                {
                    Logger.LogError("Unable to retrieve details of image");
                    throw new Exception("Unable to retrieve details from Google");
                }
                Logger.LogInformation("Image Response: " + challengeEntry.imageResponse);
                Logger.LogInformation("Longitude: " + challengeEntry.longitude);
                Logger.LogInformation("Latitude: " + challengeEntry.latitude);
                Logger.LogInformation("Latitude: " + challengeEntry.distanceFrom);

                var dailyChallenge = await tableService.GetDailyChallenge();

                dailyChallenge.photoUrl          = image.Url;
                dailyChallenge.text              = image.ImageText;
                dailyChallenge.latitude          = challengeEntry.latitude;
                dailyChallenge.longitude         = challengeEntry.longitude;
                dailyChallenge.extractedLocation = challengeEntry.imageResponse;
                dailyChallenge.entries           = new List <DailyChallengeEntry>();
                dailyChallenge.publishedTime     = DateTime.Now;
                dailyChallenge.currentStatus     = DailyChallengeStatus.Guessing;
                await tableService.SaveDailyChallenge(dailyChallenge);

                IMessageActivity reply = MessageFactory.Attachment(new List <Attachment>());

                reply.Attachments.Add(AttachmentHelper.ImageChosen(dailyChallenge.photoUrl));
                var activity = (Activity)reply;

                await stepContext.Context.SendActivityAsync((Activity)reply);

                return(await stepContext.EndDialogAsync(cancellationToken));

                //return await stepContext.ReplaceDialogAsync(nameof(ChallengeGuesserDialog), promptOptions, cancellationToken);
            }
            else if (command.ToLower().Contains("try another image"))
            {
                int imageIndex = await IncrementAndReturnImageIndex();
            }

            else if (command.ToLower().Contains("switch to google"))
            {
                try
                {
                    var reply      = MessageFactory.Attachment(new List <Attachment>());
                    var attachment = await GetGoogleImageChoiceAttachment();
                    await UpdateImageSource(ImageSource.Google);

                    reply.Attachments.Add(attachment);
                }
                catch (Exception exp)
                {
                    Logger.LogError(exp, $"Could not set Google Image: {exp.Message} - {exp.StackTrace}", null);
                    throw exp;
                }
            }
            else if (command.ToLower().Contains("switch to bing"))
            {
                var reply      = MessageFactory.Attachment(new List <Attachment>());
                int imageIndex = await GetImageIndex(stepContext);
                await UpdateImageSource(ImageSource.Bing);

                var attachment = await GetBingImageChoiceAttachment(imageIndex);

                // reply.AttachmentLayout = AttachmentLayoutTypes.Carousel;
                reply.Attachments.Add(attachment);
            }
            else
            {
                await stepContext.Context.SendActivityAsync(MessageFactory.Text("Sorry, not sure about that"), cancellationToken);
            }

            return(await stepContext.BeginDialogAsync(nameof(MainDialog), null, cancellationToken));
        }
Esempio n. 3
0
        private async Task <DialogTurnResult> ActStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            BingMapService mapService = new BingMapService(Configuration["BingMapsAPI"]);

            string             guessText = stepContext.Result.ToString();
            DailyChallengeInfo info      = await tableService.GetLatestInfo();

            if (guessText.ToLower().Contains("check results"))
            {
                DailyChallenge dailyChallenge = await tableService.GetDailyChallenge();

                await CheckResults(stepContext, cancellationToken, dailyChallenge, info);

                return(await stepContext.EndDialogAsync(cancellationToken));
            }
            else
            {
                TelemetryClient.TrackTrace("Checking for guess: " + guessText, Severity.Information, null);
                try
                {
                    DailyChallengeEntry entry = await mapService.GetLocationDetails(guessText, Logger);

                    if (entry == null)
                    {
                        var locationSplit = stepContext.Result.ToString().Split(' ');
                        if (locationSplit.Length > 1)
                        {
                            var searchText = guessText.Substring(guessText.IndexOf(' '));
                            entry = await mapService.GetLocationDetails(searchText, Logger);
                        }
                    }

                    if (entry == null)
                    {
                        await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Sorry, bing maps couldn't identify the location '{stepContext.Result.ToString()}'. Please try again."), cancellationToken);

                        return(await stepContext.EndDialogAsync());
                    }
                    else
                    {
                        DailyChallenge dailyChallenge = await tableService.GetDailyChallenge();

                        if (dailyChallenge.entries != null)
                        {
                            var matchingEntries = dailyChallenge.entries.Where <DailyChallengeEntry>(e => e.imageResponse == entry.imageResponse);
                            if (matchingEntries.Count() > 0)
                            {
                                await stepContext.Context.SendActivityAsync(MessageFactory.Text($"Sorry, someone has beaten you to suggesting '{stepContext.Result.ToString()}'. Please try again."), cancellationToken);

                                // This line caused a bit of a meltdown so changing to end dialogue
                                //return await stepContext.BeginDialogAsync(nameof(ChallengeGuesserDialog), null, cancellationToken);
                                return(await stepContext.EndDialogAsync());
                            }
                        }

                        double distanceFromResult = DistanceMeasureHelper.GetDistanceFromResult(entry.latitude, entry.longitude, dailyChallenge.latitude, dailyChallenge.longitude);

                        entry.distanceFrom = distanceFromResult;
                        entry.userName     = stepContext.Context.Activity.From.Name;
                        entry.userId       = stepContext.Context.Activity.From.Id;
                        dailyChallenge.entries.Add(entry);

                        await tableService.SaveDailyChallenge(dailyChallenge);

                        IMessageActivity            reply         = MessageFactory.Attachment(new List <Attachment>());
                        DailyChallengeEntriesStatus currentStatus = await CheckWhetherAllEntriesReceived(stepContext, cancellationToken, dailyChallenge, info);

                        reply.Attachments.Add(AttachmentHelper.AwaitingGuesses(currentStatus.userCount, dailyChallenge.photoUrl, currentStatus.usersWithEntryCount, entry.userName, entry.imageResponse));

                        await stepContext.Context.SendActivityAsync((Activity)reply);

                        return(await stepContext.EndDialogAsync(null, cancellationToken));
                    }
                }
                catch (Exception exp)
                {
                    TelemetryClient.TrackTrace("Error loading results: " + exp.Message + exp.StackTrace, Severity.Error, null);
                    throw exp;
                }
            }
        }