示例#1
0
        /// <summary>
        /// Execution for the <see cref="GetWeatherForecastDialog"/> starts here.
        /// </summary>
        /// <param name="context">Mandatory. The context for the execution of a dialog's conversational process.</param>
        public async Task StartAsync(IDialogContext context)
        {
            string cityName    = _messageHelpers.ExtractEntityFromMessage("City.Name", _entities);
            string countryCode = _messageHelpers.ExtractEntityFromMessage("City.CountryCode", _entities, TextCaseType.UpperCase);
            string countryName = _messageHelpers.ExtractEntityFromMessage("City.CountryName", _entities);

            City preferredCity = _botDataService.GetPreferredWeatherLocation(context);

            if (string.IsNullOrEmpty(cityName))
            {
                if (preferredCity?.Name == null)
                {
                    PromptDialog.Text(context, ResumeAfterSpecifyCityNamePrompt, "What's the name of the city you want the forecast for?",
                                      "I can't understand you. Tell me the name of the city you want the forecast for");
                }
                else
                {
                    var weatherForecast = await _weatherService.GetWeather(preferredCity);

                    context.Done(weatherForecast != null
                            ? $"Currently the weather in {preferredCity.Name} is {weatherForecast}"
                            : "🤧⛅ - I'm having trouble accessing weather reports. We'll have to try again later!");
                }
            }
            else
            {
                IList <City> citySearchResults = await _weatherService.SearchForCities(cityName, countryCode, countryName);
                await ResumeAfterCitySearch(context, cityName, citySearchResults);
            }
        }
        /// <summary>
        /// Execution for the <see cref="PreferredWeatherLocationDialog"/> starts here.
        /// </summary>
        /// <param name="context">Mandatory. The context for the execution of a dialog's conversational process.</param>
        public Task StartAsync(IDialogContext context)
        {
            City savedPreferredCity = _botDataService.GetPreferredWeatherLocation(context);

            if (_entities != null)
            {
                _extractedCityFromMessage = _messageHelpers.ExtractEntityFromMessage("City.Name", _entities);
            }

            if (!string.IsNullOrWhiteSpace(_extractedCityFromMessage))
            {
                PromptDialog.Confirm(context, ResumeAfterPreferredCityConfirmation, $"So you'd like to change your preferred weather location to {_extractedCityFromMessage}?", $"Sorry I don't understand - try again! So you'd like to change your preferred weather location to {_extractedCityFromMessage}");
                return(Task.CompletedTask);
            }

            if (savedPreferredCity != null)
            {
                PromptDialog.Confirm(context, ResumeAfterConfirmation, $"Your saved weather location is {savedPreferredCity.Name}, {savedPreferredCity.Country}, would you like to change it?", $"Sorry I don't understand - try again! Would you like to change your preferred weather location?");
                return(Task.CompletedTask);
            }

            PromptDialog.Text(context, ResumeAfterPromptForPreferredLocation, "What's the name of your preferred weather location?", "Sorry I didn't get that - try again! What's the name of your preferred weather location?");
            return(Task.CompletedTask);
        }