Skip to content

Commit

Permalink
Merge pull request #74 from Defkon1/feature/chucknorris
Browse files Browse the repository at this point in the history
Added !chucknorris facts command
  • Loading branch information
kasuken authored Oct 27, 2021
2 parents 5b638ec + 6fe39b3 commit bbc7b74
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
77 changes: 77 additions & 0 deletions SonequaBot.Shared/Commands/CommandChuckNorris.cs
Original file line number Diff line number Diff line change
@@ -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<string> 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;
}
}
}
36 changes: 36 additions & 0 deletions SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit bbc7b74

Please sign in to comment.