Skip to content

Commit

Permalink
Merge pull request #150 from pulcher/add_local_dad_repo
Browse files Browse the repository at this point in the history
recoded dad jokes to be local
  • Loading branch information
pulcher authored Nov 28, 2023
2 parents 2b38407 + 8e3a967 commit 8ab1ad9
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 41 deletions.
22 changes: 3 additions & 19 deletions Magic8HeadService/Commands/DadCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,10 @@ public DadCommand(ITwitchClient client, ISayingResponse sayingResponse, IDadJoke

public void Handle(OnChatCommandReceivedArgs cmd)
{
try
{
var dadJoke = dadJokeService.GetDadJoke(alternateSite).Result;
var dadJoke = dadJokeService.GetDadJoke();

sayingResponse.SaySomethingNiceAsync(dadJoke.Setup, client, cmd.Command.ChatMessage.Channel, cmd.Command.ChatMessage.Username);
client.SendMessage(cmd.Command.ChatMessage.Channel, $"Q: {dadJoke.Setup}");

Task.Delay(5000).Wait();

sayingResponse.SaySomethingNiceAsync(dadJoke.Punchline, client, cmd.Command.ChatMessage.Channel, cmd.Command.ChatMessage.Username);
client.SendMessage(cmd.Command.ChatMessage.Channel, $"A: {dadJoke.Punchline}");
}
catch (Exception ex)
{
alternateSite = "https://karljoke.herokuapp.com/jokes/random";

sayingResponse.SaySomethingNiceAsync(ex.Message, client, cmd.Command.ChatMessage.Channel, cmd.Command.ChatMessage.Username);
client.SendMessage(cmd.Command.ChatMessage.Channel, $"We've got problem {ex.Message}");
}
sayingResponse.SaySomethingNiceAsync(dadJoke, client, cmd.Command.ChatMessage.Channel, cmd.Command.ChatMessage.Username);
client.SendMessage(cmd.Command.ChatMessage.Channel, $"Q: {dadJoke}");
}

}
}
8 changes: 6 additions & 2 deletions Magic8HeadService/Commands/DadJoke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ public class DadJoke
{
public int Id { get; set; }
public string Type { get; set; }
public string Setup { get; set; }
public string Punchline { get; set; }
public string Phrase { get; set; }
}
}

// Example of new thing
// id:
// mood: Dad
// phrase: What do you call a heard of sheeps falling off a cliff? A lambslide
29 changes: 10 additions & 19 deletions Magic8HeadService/DadJokeService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using MrBigHead.Shared;
using System.Net.Http;
using System.Net.Http.Json;
using System.Threading.Tasks;
Expand All @@ -7,33 +8,23 @@ namespace Magic8HeadService
{
public class DadJokeService : IDadJokeService
{
private readonly ISayingResponse sayingResponse;
private ILogger logger;
private HttpClient client;
private IHttpClientFactory httpClientFactory;

public DadJokeService(IHttpClientFactory httpClientFactory, ILoggerFactory loggerFactory)
public DadJokeService(ISayingResponse sayingResponse, ILoggerFactory loggerFactory)
{
this.logger = loggerFactory.CreateLogger("Generic Logger");

this.client = httpClientFactory.CreateClient();

this.httpClientFactory = httpClientFactory;
this.sayingResponse = sayingResponse;
}

public async Task<DadJoke> GetDadJoke(string url)
public string GetDadJoke()
{
var siteUrl = "https://karljoke.herokuapp.com/jokes/random";

if (!string.IsNullOrEmpty(url))
siteUrl = url;

//https://dadjoke-2021-1018a.jimf99.repl.co/jokes/random

var dadJoke = await client.GetFromJsonAsync<DadJoke>(siteUrl);

dadJoke = await client.GetFromJsonAsync<DadJoke>("https://karljoke.herokuapp.com/jokes/random");
return GetRandomAnswer();
}

return dadJoke;
private string GetRandomAnswer()
{
return sayingResponse.PickSaying("dad");
}
}
}
2 changes: 1 addition & 1 deletion Magic8HeadService/IDadJokeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ namespace Magic8HeadService
{
public interface IDadJokeService
{
Task<DadJoke> GetDadJoke(string url);
string GetDadJoke();
}
}

0 comments on commit 8ab1ad9

Please sign in to comment.