From 6fe39b3fb3f97fd17e58b93ef6fccba680a9f675 Mon Sep 17 00:00:00 2001 From: Defkon1 Date: Wed, 27 Oct 2021 09:34:54 +0200 Subject: [PATCH] Added !chucknorris facts command --- .../Commands/CommandChuckNorris.cs | 77 +++++++++++++++++++ .../ChuckNorrisCommandTest.cs | 36 +++++++++ 2 files changed, 113 insertions(+) create mode 100644 SonequaBot.Shared/Commands/CommandChuckNorris.cs create mode 100644 SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs diff --git a/SonequaBot.Shared/Commands/CommandChuckNorris.cs b/SonequaBot.Shared/Commands/CommandChuckNorris.cs new file mode 100644 index 0000000..5403d21 --- /dev/null +++ b/SonequaBot.Shared/Commands/CommandChuckNorris.cs @@ -0,0 +1,77 @@ +namespace SonequaBot.Shared.Commands +{ + using System; + using System.IO; + using System.Net.Http; + using System.Threading.Tasks; + + using Newtonsoft.Json.Linq; + using SonequaBot.Shared.Commands.DTO; + using SonequaBot.Shared.Commands.Interfaces; + using SonequaBot.Shared.Commands.Interfaces.Responses; + + public class CommandChuckNorris : CommandBase, IResponseImageCard + { + private const string ENDPOINT = "https://api.chucknorris.io/jokes/random"; + + protected override CommandActivationComparison ActivationComparison => CommandActivationComparison.Contains; + + protected override string ActivationCommand => "!chucknorris"; + + public ImageCardData GetImageCardEvent(CommandSource source) + { + ImageCardData result = new ImageCardData() + { + Title = $"Hey, {source.User}!", + Description = "If any one of Chuck Norris' sextapes was ever released publicly, it would win the Best Picture Oscar", + ImageUrl = "https://assets.chucknorris.host/img/avatar/chuck-norris.png", + Url = "https://api.chucknorris.io/jokes/AONc06lISwSZVjqMW2gO3Q" + }; + + try + { + string json = this.GetPage().Result; + + if (!string.IsNullOrEmpty(json)) + { + JObject fact = JObject.Parse(json); + result.Title = string.IsNullOrEmpty(source?.User) ? "Hey, you!" : $"Hey, {source.User}!"; + result.Description = fact["value"]?.ToString() ?? string.Empty; + result.ImageUrl = fact["icon_url"]?.ToString() ?? string.Empty; + result.Url = fact["url"]?.ToString() ?? string.Empty; + } + } + catch (Exception ex) + { + // external CommandHandler is catching exceptions + throw; + } + + return result; + } + + private async Task GetPage() + { + string json = string.Empty; + + try + { + using (var client = new HttpClient()) + { + HttpResponseMessage response = await client.GetAsync(ENDPOINT); + HttpContent responseContent = response.Content; + using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync())) + { + json = await reader.ReadToEndAsync(); + } + } + } + catch (Exception) + { + throw; + } + + return json; + } + } +} \ No newline at end of file diff --git a/SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs b/SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs new file mode 100644 index 0000000..7e91d24 --- /dev/null +++ b/SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using SonequaBot.Shared.Commands; +using Xunit; + +namespace SonequaBot.Sentiment.Tests.ChuckNorrisCommand +{ + public class ChuckNorrisCommandTest + { + [Fact] + public void Command_Should_Run() + { + var source = new CommandSource + { + Channel = "Test channel", + Message = "!chucknorris", + User = "Defkon1" + }; + + var commandProvider = new CommandChuckNorris(); + Assert.NotNull(commandProvider); + + var cardData = commandProvider.GetImageCardEvent(source); + Assert.NotNull(cardData); + + Assert.NotEqual(string.Empty, cardData.Title); + Assert.Equal("Hey, Defkon1!", cardData.Title); + Assert.NotEqual(string.Empty, cardData.Description); + Assert.NotEqual(string.Empty, cardData.ImageUrl); + Assert.NotEqual(string.Empty, cardData.Url); + } + } +}