コード例 #1
0
        protected async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> result)
        {
            GeoLocation geoLocation = null;
            var         activity    = await result;

            if (string.IsNullOrEmpty(activity.Text) && activity.ChannelId == FacebookChannelId)
            {
                var    facebookLocation = (FacebookLocation)activity.ChannelData.ToObject <FacebookLocation>();
                double?latitude         = facebookLocation?.message?.attachments?[0]?.payload?.coordinates?.lat;
                double?longitude        = facebookLocation?.message?.attachments?[0]?.payload?.coordinates?.@long;

                if (latitude.HasValue && longitude.HasValue)
                {
                    geoLocation = new GeoLocation()
                    {
                        Latitude  = latitude.Value,
                        Longitude = longitude.Value
                    };
                }
            }
            else
            {
                var googleLocationService = new GoogleLocationService();
                var geoCodingRequest      = new GeocodingRequest
                {
                    Address  = activity.Text ?? string.Empty,
                    ApiKey   = _apiKey,
                    Language = _language
                };
                geoLocation = await googleLocationService.GetLocationAsync(geoCodingRequest);
            }

            if (geoLocation != null)
            {
                WeatherLocation weatherLocation = null;
                try
                {
                    weatherLocation = context.UserData.GetValueOrDefault <WeatherLocation>(WeatherBot.Storage.Location.UserDataLocationKey) ?? new WeatherLocation();
                }
                catch (Exception ex)
                {
                    // Do nothing
                    weatherLocation = new WeatherLocation();
                }

                weatherLocation.GeoLocation = geoLocation;
                context.UserData.SetValue(WeatherBot.Storage.Location.UserDataLocationKey, weatherLocation);
            }

            context.Done <GeoLocation>(geoLocation);
        }