private async Task <string> CreateWeatherCardAsync()
        {
            var http = new HttpClient
            {
                BaseAddress = new Uri("https://free-api.heweather.net/s6/weather/")
            };
            var client  = new HeWeatherClient(http, _configuration);
            var weather = await client.NowAsync("西湖区");

            var weather6 = weather?.HeWeather6?.FirstOrDefault();

            if (weather6 != null)
            {
                return($"{{\"@context\": \"https://schema.org/extensions\",\"@type\": \"MessageCard\",\"themeColor\": \"0072C6\",\"title\": \"{weather6.basic.location},{weather6.basic.parent_city}\",\"text\": \"{weather6.now.cond_txt} {weather6.now.tmp}℃\"}}");
            }
            return(string.Empty);
        }
Exemplo n.º 2
0
        protected override async Task <MessagingExtensionResponse> OnTeamsMessagingExtensionQueryAsync(ITurnContext <IInvokeActivity> turnContext, MessagingExtensionQuery query, CancellationToken cancellationToken)
        {
            List <MessagingExtensionAttachment> attachments = null;
            var cityName = query?.Parameters?.FirstOrDefault(p => p.Name == "CityName")?.Value as string ?? string.Empty;

            if (string.IsNullOrWhiteSpace(cityName))
            {
                attachments = new List <MessagingExtensionAttachment>();
            }
            else
            {
                var weather = await _weatherClient.NowAsync(cityName);

                attachments = weather?.HeWeather6?.Where(w => w.status == "ok" && w.now != null)
                              .Select(item => new MessagingExtensionAttachment
                {
                    ContentType = AdaptiveCard.ContentType,
                    Content     = CreateWeatherCard(item),
                    Preview     = new ThumbnailCard
                    {
                        Title  = $"{item.basic.parent_city}-{item.basic.location}",
                        Text   = $"{item.now.cond_txt} {item.now.tmp}",
                        Images = new List <CardImage>()
                        {
                            new CardImage($"https://cdn.heweather.com/cond_icon/{item.now.cond_code}.png", "Icon")
                        }
                    }.ToAttachment()
                }).ToList();
            }



            return(new MessagingExtensionResponse
            {
                ComposeExtension = new MessagingExtensionResult
                {
                    Type = "result",
                    AttachmentLayout = "list",
                    Attachments = attachments
                }
            });
        }