From 8e3a96753cbdd947d1294fe6306be032754cde6c Mon Sep 17 00:00:00 2001 From: Harold Pulcher Date: Mon, 27 Nov 2023 21:35:55 -0600 Subject: [PATCH] recoded dad jokes to be local --- Magic8HeadService/Commands/DadCommand.cs | 22 +++--------------- Magic8HeadService/Commands/DadJoke.cs | 8 +++++-- Magic8HeadService/DadJokeService.cs | 29 ++++++++---------------- Magic8HeadService/IDadJokeService.cs | 2 +- 4 files changed, 20 insertions(+), 41 deletions(-) diff --git a/Magic8HeadService/Commands/DadCommand.cs b/Magic8HeadService/Commands/DadCommand.cs index 2ad976c..ac850c4 100644 --- a/Magic8HeadService/Commands/DadCommand.cs +++ b/Magic8HeadService/Commands/DadCommand.cs @@ -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}"); } - } } diff --git a/Magic8HeadService/Commands/DadJoke.cs b/Magic8HeadService/Commands/DadJoke.cs index eba5b04..fad1048 100644 --- a/Magic8HeadService/Commands/DadJoke.cs +++ b/Magic8HeadService/Commands/DadJoke.cs @@ -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 diff --git a/Magic8HeadService/DadJokeService.cs b/Magic8HeadService/DadJokeService.cs index 1ca7467..91103b5 100644 --- a/Magic8HeadService/DadJokeService.cs +++ b/Magic8HeadService/DadJokeService.cs @@ -1,4 +1,5 @@ using Microsoft.Extensions.Logging; +using MrBigHead.Shared; using System.Net.Http; using System.Net.Http.Json; using System.Threading.Tasks; @@ -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 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(siteUrl); - - dadJoke = await client.GetFromJsonAsync("https://karljoke.herokuapp.com/jokes/random"); + return GetRandomAnswer(); + } - return dadJoke; + private string GetRandomAnswer() + { + return sayingResponse.PickSaying("dad"); } } } diff --git a/Magic8HeadService/IDadJokeService.cs b/Magic8HeadService/IDadJokeService.cs index 040b2e3..53c1e40 100644 --- a/Magic8HeadService/IDadJokeService.cs +++ b/Magic8HeadService/IDadJokeService.cs @@ -4,6 +4,6 @@ namespace Magic8HeadService { public interface IDadJokeService { - Task GetDadJoke(string url); + string GetDadJoke(); } }