Пример #1
0
        public async Task Unshorten(EduardoContext context, string url)
        {
            if (!url.Contains("goo.gl"))
            {
                await context.Channel.SendMessageAsync($"**{context.User.Username}, This Url is not shortened**");

                return;
            }

            string unshortened = await ShortenHelper.UnshortenUrlAsync(_credentials.GoogleShortenerApiKey, url);

            await context.Channel.SendMessageAsync($"**{context.User.Mention}, your unshortened url is: \"{unshortened}\"**");
        }
Пример #2
0
        public async Task GetNewsHeadlines(EduardoContext context, string source)
        {
            if (!_newsData.NewsSources.Contains(source))
            {
                await context.Channel.SendMessageAsync($"**{source} is not a valid. Type `sources` to view available news sources**");

                return;
            }

            using Stream responseStream = await NetworkHelper.GetStreamAsync($"https://newsapi.org/v1/articles?source={source}&sortBy=top&apiKey={_credentials.NewsApiKey}");

            using StreamReader sr = new StreamReader(responseStream);
            string json = sr.ReadToEnd();

            JObject jResult = JObject.Parse(json);

            JArray jHeadlines = (JArray)jResult["articles"];
            List <EmbedFieldBuilder> headlines = new List <EmbedFieldBuilder>();

            int maxHeadlines = Math.Min(_newsData.MaxHeadlines, jHeadlines.Count - 1);

            for (int i = 0; i < maxHeadlines; i++)
            {
                string shorten = await ShortenHelper.ShortenUrlAsync(_credentials.GoogleShortenerApiKey, jHeadlines[i]["url"].ToString());

                headlines.Add(new EmbedFieldBuilder
                {
                    Name  = jHeadlines[i]["title"].ToString(),
                    Value = $"{jHeadlines[i]["description"]}\n({shorten})"
                });
            }

            await context.Channel.SendMessageAsync(embed : new EmbedBuilder()
                                                   .WithColor(Color.Blue)
                                                   .WithAuthor($"Latest News from {source.Replace('-', ' ').ToUpper()}",
                                                               @"http://shmector.com/_ph/18/412122157.png")
                                                   .WithThumbnailUrl(jResult["articles"][0]["urlToImage"].ToString())
                                                   .WithFields(headlines)
                                                   .WithFooter("News via newsapi.org",
                                                               @"https://pbs.twimg.com/profile_images/815237522641092609/6IeO3WLV.jpg")
                                                   .Build());
        }