Skip to content

Commit

Permalink
Added command !wikirandom with test
Browse files Browse the repository at this point in the history
  • Loading branch information
Defkon1 committed Oct 26, 2021
1 parent 8159061 commit de2ba42
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
14 changes: 14 additions & 0 deletions SonequaBot.Discord/SonequaDiscord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,20 @@ private async Task CommandHandler(DiscordClient client, MessageCreateEventArgs e
await e.Channel.SendMessageAsync("", false, embed);
}

if (command is IResponseImageCard messageCard)
{
var cardData = messageCard.GetImageCardEvent(source);

var embed = new DiscordEmbedBuilder
{
Title = cardData.Title,
Description = cardData.Description,
ImageUrl = cardData.ImageUrl,
Url = cardData.Url
};
await e.Channel.SendMessageAsync("", false, embed);
}

return;
}
}
Expand Down
72 changes: 72 additions & 0 deletions SonequaBot.Shared/Commands/CommandWikiRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Newtonsoft.Json.Linq;
using SonequaBot.Shared.Commands.DTO;
using SonequaBot.Shared.Commands.Interfaces.Responses;
using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace SonequaBot.Shared.Commands
{
public class CommandWikiRandom: CommandBase, IResponseImageCard
{
private const string ENDPOINT = "https://it.wikipedia.org/api/rest_v1/page/random/summary";

protected override string ActivationCommand => "!wikirandom";

public ImageCardData GetImageCardEvent(CommandSource source)
{
ImageCardData result = new ImageCardData()
{
Title = "Rick Astley",
Description = "Richard Paul Astley (Newton-le-Willows, 6 febbraio 1966) è un cantautore, musicista e conduttore radiofonico britannico.",
ImageUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6d/Rick_Astley_-_Pepsifest_2009.jpg/200px-Rick_Astley_-_Pepsifest_2009.jpg"
};

try
{
string json = this.GetPage().Result;

if (!string.IsNullOrEmpty(json))
{
JObject wikiPage = JObject.Parse(json);
result.Title = wikiPage["displaytitle"]?.ToString() ?? string.Empty;
result.Description = wikiPage["extract"]?.ToString() ?? string.Empty;
result.ImageUrl = wikiPage["thumbnail"]?["source"]?.ToString() ?? string.Empty;
result.Url = wikiPage["content_urls"]?["desktop"]?["page"]?.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;
}
}
}
23 changes: 23 additions & 0 deletions SonequaBot.Shared/Commands/DTO/ImageCardData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;

namespace SonequaBot.Shared.Commands.DTO
{
public class ImageCardData
{
public ImageCardData()
{
this.Title = string.Empty;
this.Description = string.Empty;
this.ImageUrl = string.Empty;
this.Url = string.Empty;
}

public string Title { get; set; }

public string Description { get; set; }

public string ImageUrl { get; set; }

public string Url { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SonequaBot.Shared.Commands.DTO;

namespace SonequaBot.Shared.Commands.Interfaces.Responses
{
public interface IResponseImageCard
{
ImageCardData GetImageCardEvent(CommandSource source);
}
}
1 change: 1 addition & 0 deletions SonequaBot.Tests/SonequaBot.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<ProjectReference Include="..\SonequaBot.Sentiment\SonequaBot.Sentiment.csproj" />
<ProjectReference Include="..\SonequaBot.Shared\SonequaBot.Shared.csproj" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions SonequaBot.Tests/WikiRandomCommand/WikiRandomCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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.WikiRandomCommand
{
public class WikiRandomCommandTest
{
[Fact]
public void Command_Should_Run()
{
var source = new CommandSource
{
Channel = "Test channel",
Message = "!wikirandom",
User = "Defkon1"
};

var commandProvider = new CommandWikiRandom();
Assert.NotNull(commandProvider);

var cardData = commandProvider.GetImageCardEvent(source);
Assert.NotNull(cardData);

Assert.NotEqual(string.Empty, cardData.Title);
Assert.NotEqual(string.Empty, cardData.Description);
//// Assert.NotEqual(string.Empty, cardData.ImageUrl); // image can be empty if no thumbnail is provided from Wikipedia
Assert.NotEqual(string.Empty, cardData.Url);
}
}
}

0 comments on commit de2ba42

Please sign in to comment.