-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from pulcher/add_cheer_handler
Added payload object and initial handler
- Loading branch information
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |