/// <summary> /// /// </summary> /// <param name="turnContext"></param> /// <param name="cancellationToken"></param> /// <returns></returns> protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken) { //异常处理可迭代成全局异常,免得每个机器人都需要try catch,使用中间件 try { string text = turnContext.Activity.Text; string replyText; HttpClient httpClient = _httpClientFactory.CreateClient(nameof(StudyEchoBotConst.Weather)); string response = await httpClient.GetStringAsync($"?city={text}&key={_configuration["WeatherKey"]}"); StudyEchoBotResponse <WeatherQueryResponse> studyEchoBotResponse = Newtonsoft.Json.JsonConvert.DeserializeObject <StudyEchoBotResponse <WeatherQueryResponse> >(response); if (!0.Equals(studyEchoBotResponse.ErrorCode)) { replyText = $"{studyEchoBotResponse.Reason},城市名称如:温州、上海、北京"; } else { WeatherQueryResponse weatherQueryResponse = studyEchoBotResponse.Result; RealtimeWeather realtimeWeather = weatherQueryResponse.RealtimeWeather; replyText = $"{weatherQueryResponse.City}当前天气{realtimeWeather.Info}{realtimeWeather.Temperature}℃ {realtimeWeather.Power}{realtimeWeather.Direct}"; } await turnContext.SendActivityAsync(MessageFactory.Text(replyText), cancellationToken); } catch (Exception e) { //防止暴力异常,让系统宕机 Thread.Sleep(1000); _logger.LogError($"StackTrace:{e.StackTrace}\r\nMessage:{e.Message}"); await turnContext.SendActivityAsync(MessageFactory.Text("系统内部异常,请联系管理员"), cancellationToken); } }
public WeatherViewModel(WeatherQueryResponse weatherData, IViewFacade viewFacade, WeatherCodeService weatherCodeService, WeatherRecommendationService evaluationService) { WeatherData = weatherData; ViewFacade = viewFacade; WeatherCodeService = weatherCodeService; RecommendationService = evaluationService; }
public IEnumerable <(string Question, bool Answer)> Get(WeatherQueryResponse weatherQueryResponse) { var current = weatherQueryResponse.current; yield return("Should I go out?", !current.IsRaining()); yield return("Should I wear sunscreen?", current.uv_index > 3); yield return("Can I fly my kite?", !current.IsRaining() && current.wind_speed > 15); }