public async Task MessageReceivedAsync(IDialogContext context, IAwaitable <IMessageActivity> argument) { // Get the message out var message = await argument; var activityMessage = message as Activity; if (activityMessage.MentionsRecipient() || message.Conversation.IsGroup == false) { var geo = new GeoLocatorService.GeoService(); var matches = await geo.FindCoordinates(activityMessage.RemoveRecipientMention()); if (matches.Count > 1) { await context.Forward(new LocationDialog(), LocationPicked, matches, new System.Threading.CancellationToken()); } else if (matches.Count == 1) { await DisplayWeather(context, matches[0]); context.Wait(MessageReceivedAsync); } else { await context.PostAsync($"Could not find the weather for {activityMessage.RemoveRecipientMention()}"); context.Wait(MessageReceivedAsync); } } else { context.Wait(MessageReceivedAsync); } }
// Callback for when a location has been picked public async Task LocationPicked(IDialogContext context, IAwaitable <string> argument) { var location = await argument; var geo = new GeoLocatorService.GeoService(); var city = (await geo.FindCoordinates(location)).First(); await DisplayWeather(context, city); context.Wait(MessageReceivedAsync); }
/// <summary> /// POST: api/Messages /// Receive a message from a user and reply to it /// </summary> public async Task <HttpResponseMessage> Post([FromBody] Activity activity) { if (activity.Type == ActivityTypes.Message) { if (activity.MentionsRecipient()) { ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl)); // Show typing while performing lookups var typingActivity = activity.CreateReply(); typingActivity.Type = ActivityTypes.Typing; await connector.Conversations.ReplyToActivityAsync(typingActivity); // Remove the mention from the activity text var cityName = activity.RemoveRecipientMention(); var locatorService = new GeoLocatorService.GeoService(); var matches = await locatorService.FindCoordinates(cityName); if (matches.Count == 0) { await connector.Conversations.ReplyToActivityAsync(activity.CreateReply("Couldn't find the weather!")); } else if (matches.Count == 1) { var weather = new WeatherService.WeatherService(); var current = await weather.GetCurrentConditions(matches[0].Latitude, matches[0].Longitude); var reply = activity.CreateReply($"The current conditions for {matches[0].CityState} are {current.Summary} and {current.CurrentTemp}."); await connector.Conversations.ReplyToActivityAsync(reply); } else { var cityHero = new HeroCard(); cityHero.Title = "Which city?"; cityHero.Subtitle = $"Multiple cities found for {cityName}"; cityHero.Text = "Select which city you want the weather for:"; cityHero.Buttons = new List <CardAction>(); foreach (var city in matches) { var cityAction = new CardAction(); cityAction.Type = ActionTypes.PostBack; cityAction.Value = city.CityState; cityAction.Title = city.CityState; cityHero.Buttons.Add(cityAction); } var cityHeroReply = activity.CreateReply(); cityHeroReply.Attachments = new List <Attachment>(); cityHeroReply.Attachments.Add(cityHero.ToAttachment()); await connector.Conversations.ReplyToActivityAsync(cityHeroReply); } } } else { HandleSystemMessage(activity); } var response = Request.CreateResponse(HttpStatusCode.OK); return(response); }