From c5b99e9f33f1fe8e00d35a2b41c3892d902532c9 Mon Sep 17 00:00:00 2001 From: Harold Pulcher Date: Mon, 25 Sep 2023 22:21:14 -0500 Subject: [PATCH] Added payload object and initial handler --- .../MqttHandlers/Cheer/CheerHandler.cs | 47 +++++++++++++++++++ .../MqttHandlers/Cheer/MqttCheerPayload.cs | 22 +++++++++ 2 files changed, 69 insertions(+) create mode 100644 Magic8HeadService/MqttHandlers/Cheer/CheerHandler.cs create mode 100644 Magic8HeadService/MqttHandlers/Cheer/MqttCheerPayload.cs diff --git a/Magic8HeadService/MqttHandlers/Cheer/CheerHandler.cs b/Magic8HeadService/MqttHandlers/Cheer/CheerHandler.cs new file mode 100644 index 0000000..7e40fe6 --- /dev/null +++ b/Magic8HeadService/MqttHandlers/Cheer/CheerHandler.cs @@ -0,0 +1,47 @@ +using Microsoft.Extensions.Logging; +using System.Linq; +using TwitchLib.Client.Interfaces; +using System.Text.Json; +using System.Text; + +namespace Magic8HeadService.MqttHandlers.Cheer +{ + public class CheerHandler : IMqttHandler + { + private ITwitchClient client; + private readonly ISayingResponse sayingResponse; + private ILogger logger; + + public CheerHandler(ITwitchClient client, ISayingResponse sayingResponse, ILogger logger) + { + this.client = client; + this.sayingResponse = sayingResponse; + this.logger = logger; + } + + public bool CanHandle(MqttHandlerMessage message) + { + if (message == null) return false; + + var payloadString = Encoding.ASCII.GetString(message.Payload); + + var cheer = JsonSerializer.Deserialize(payloadString); + + if (cheer != null && cheer.Type == "channel.cheer") + return true; + else + return false; + } + + public void Handle(MqttHandlerMessage message) + { + var payloadString = Encoding.ASCII.GetString(message.Payload); + var cheer = JsonSerializer.Deserialize(payloadString); + + var messageToSay = $"Thanks for the cheer {cheer.UserName}"; + + sayingResponse.SaySomethingNiceAsync(messageToSay, client, + client.JoinedChannels.FirstOrDefault().ToString(), string.Empty).Wait(); + } + } +} diff --git a/Magic8HeadService/MqttHandlers/Cheer/MqttCheerPayload.cs b/Magic8HeadService/MqttHandlers/Cheer/MqttCheerPayload.cs new file mode 100644 index 0000000..e953efe --- /dev/null +++ b/Magic8HeadService/MqttHandlers/Cheer/MqttCheerPayload.cs @@ -0,0 +1,22 @@ +using System.Text.Json.Serialization; + +namespace Magic8HeadService.MqttHandlers.Cheer +{ + public class MqttCheerPayload + { + [JsonPropertyName("type")] + public string Type { get; set; } + [JsonPropertyName("message")] + public string Message { get; set; } + [JsonPropertyName("userName")] + public string UserName { get; set; } + [JsonPropertyName("bits")] + public int Bits { get; set; } + //[JsonPropertyName("isSub")] + //public string IsSub { get; set; } + //[JsonPropertyName("isMod")] + //public string IsMod { get; set; } + //[JsonPropertyName("isVip")] + //public string IsVip { get; set; } + } +} \ No newline at end of file