-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from Defkon1/feature/chucknorris
Added !chucknorris facts command
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
SonequaBot.Tests/ChuckNorrisCommand/ChuckNorrisCommandTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |