Skip to content

Commit

Permalink
Merge pull request #133 from pulcher/add_cheer_handler
Browse files Browse the repository at this point in the history
Added payload object and initial handler
  • Loading branch information
pulcher authored Oct 17, 2023
2 parents 1a11fce + c5b99e9 commit f10a579
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Magic8HeadService/MqttHandlers/Cheer/CheerHandler.cs
Original file line number Diff line number Diff line change
@@ -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<Worker> logger;

public CheerHandler(ITwitchClient client, ISayingResponse sayingResponse, ILogger<Worker> 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<MqttCheerPayload>(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<MqttCheerPayload>(payloadString);

var messageToSay = $"Thanks for the cheer {cheer.UserName}";

sayingResponse.SaySomethingNiceAsync(messageToSay, client,
client.JoinedChannels.FirstOrDefault().ToString(), string.Empty).Wait();
}
}
}
22 changes: 22 additions & 0 deletions Magic8HeadService/MqttHandlers/Cheer/MqttCheerPayload.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}

0 comments on commit f10a579

Please sign in to comment.