public static async Task <GoldRate> GetRates() { if (DateTime.Now.Subtract(lastUpdated).Days <= 1 && goldRate != null) { return(goldRate); } Stream stream = await(await new HttpClient().GetAsync("http://www.goldrate24.com/")).Content.ReadAsStreamAsync(); HtmlDocument htmlDocument = new HtmlDocument(); htmlDocument.Load(stream); try { var nodes = htmlDocument.DocumentNode.SelectNodes("//table[contains(@class, 'now table table-striped table-hover table-bordered table-light')]"); goldRate = new GoldRate(); goldRate.Carat24 = nodes[0].SelectNodes("//th[contains(text(), ' Gold Gram 24K')]")[0].NextSibling.InnerHtml; goldRate.Carat22 = nodes[0].SelectNodes("//th[contains(text(), ' Gold Gram 22K')]")[0].NextSibling.InnerHtml; lastUpdated = DateTime.Now; } catch (Exception ex) { throw ex; } return(goldRate); }
// Send Activity to channels private async Task SendActivity(ITurnContext turnContext, string message) { Activity activity = turnContext.Activity.CreateReply(); // Fetching the gold rates GoldRate result = GoldRatesParser.GetRates().Result; // Messages based upon the entity's value if (string.IsNullOrEmpty(message)) { activity.Text = string.Format(Messages.GetRateMessages(), result.Carat24, result.Carat22); } else if (message == "22") { activity.Text = string.Format(Messages.GetRateCaratMessages(), message, result.Carat22); } else { activity.Text = string.Format(Messages.GetRateCaratMessages(), message, result.Carat24); } // Platform specific cards (Google) if (turnContext.Activity.ChannelId == "google") { var card = new GoogleBasicCard() { Content = new GoogleBasicCardContent() { Title = "Today's gold rate", Subtitle = "Keeping you up to date about the gold rates on daily basis.", FormattedText = activity.Text, Display = ImageDisplayOptions.DEFAULT, Image = new Image() { Url = "https://images-na.ssl-images-amazon.com/images/I/71lpB+tqfgL._SL210_QL95_BG0,0,0,0_FMpng_.png" }, }, }; turnContext.GoogleSetCard(card); } // Platform specific cards (Alexa) if (turnContext.Activity.ChannelId == "alexa") { turnContext.AlexaSetCard(new AlexaCard() { Type = AlexaCardType.Simple, Title = "Today's gold rate", Content = activity.Text }); } // Adaptive Cards can also be written here to support other OOTB channels await turnContext.SendActivityAsync(activity); }