Skip to content

Commit cda93a8

Browse files
AoshiWAoshiW
authored andcommitted
update example from channel.follow to channel.chat.message + add .http file
1 parent 47fa6fd commit cda93a8

File tree

3 files changed

+87
-14
lines changed

3 files changed

+87
-14
lines changed

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ app.Run();
7777
Step 5: Create the HostedService and listen for events
7878

7979
```csharp
80+
using TwitchLib.EventSub.Core.EventArgs.Channel;
8081
using TwitchLib.EventSub.Webhooks.Core;
8182
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
82-
using TwitchLib.EventSub.Webhooks.Core.EventArgs.Channel;
83+
using TwitchLib.EventSub.Webhooks.Core.Models;
8384

8485
namespace TwitchLib.EventSub.Webhooks.Example
8586
{
@@ -97,26 +98,41 @@ namespace TwitchLib.EventSub.Webhooks.Example
9798
public Task StartAsync(CancellationToken cancellationToken)
9899
{
99100
_eventSubWebhooks.OnError += OnError;
100-
_eventSubWebhooks.OnChannelFollow += OnChannelFollow;
101+
_eventSubWebhooks.UnknownEventSubNotification += OnUnknownEventSubNotification;
102+
_eventSubWebhooks.ChannelChatMessage += OnChannelChatMessage;
101103
return Task.CompletedTask;
102104
}
103105

104106
public Task StopAsync(CancellationToken cancellationToken)
105107
{
106108
_eventSubWebhooks.OnError -= OnError;
107-
_eventSubWebhooks.OnChannelFollow -= OnChannelFollow;
109+
_eventSubWebhooks.UnknownEventSubNotification -= OnUnknownEventSubNotification;
110+
_eventSubWebhooks.ChannelChatMessage -= OnChannelChatMessage;
108111
return Task.CompletedTask;
109112
}
110113

111-
private void OnChannelFollow(object? sender, ChannelFollowArgs e)
114+
private async Task OnChannelChatMessage(object? sender, ChannelChatMessageArgs e)
112115
{
113-
_logger.LogInformation($"{e.Notification.Event.UserName} followed {e.Notification.Event.BroadcasterUserName} at {e.Notification.Event.FollowedAt.ToUniversalTime()}");
116+
_logger.LogInformation($"@{e.Payload.Event.ChatterUserName} #{e.Payload.Event.BroadcasterUserName}: {e.Payload.Event.Message.Text}");
114117
}
115118

116-
private void OnError(object? sender, OnErrorArgs e)
119+
private async Task OnError(object? sender, OnErrorArgs e)
117120
{
118121
_logger.LogError($"Reason: {e.Reason} - Message: {e.Message}");
119122
}
123+
124+
// Handling notifications that are not (yet) implemented
125+
private async Task OnUnknownEventSubNotification(object? sender, UnknownEventSubNotificationArgs e)
126+
{
127+
var metadata = (WebhookEventSubMetadata)e.Metadata;
128+
_logger.LogInformation("Received event that has not yet been implemented: type:{type}, version:{version}", metadata.SubscriptionType, metadata.SubscriptionVersion);
129+
130+
switch ((metadata.SubscriptionType, metadata.SubscriptionVersion))
131+
{
132+
case ("channel.chat.message", "1"): /*code to handle the event*/ break;
133+
default: break;
134+
}
135+
}
120136
}
121137
}
122138
```

TwitchLib.EventSub.Webhooks.Example/EventSubHostedService.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using TwitchLib.EventSub.Webhooks.Core;
1+
using TwitchLib.EventSub.Core.EventArgs.Channel;
2+
using TwitchLib.EventSub.Webhooks.Core;
23
using TwitchLib.EventSub.Webhooks.Core.EventArgs;
3-
using TwitchLib.EventSub.Core.EventArgs.Channel;
44
using TwitchLib.EventSub.Webhooks.Core.Models;
55

66
namespace TwitchLib.EventSub.Webhooks.Example
@@ -20,21 +20,21 @@ public Task StartAsync(CancellationToken cancellationToken)
2020
{
2121
_eventSubWebhooks.Error += OnError;
2222
_eventSubWebhooks.UnknownEventSubNotification += OnUnknownEventSubNotification;
23-
_eventSubWebhooks.ChannelFollow += OnChannelFollow;
23+
_eventSubWebhooks.ChannelChatMessage += OnChannelChatMessage;
2424
return Task.CompletedTask;
2525
}
2626

2727
public Task StopAsync(CancellationToken cancellationToken)
2828
{
2929
_eventSubWebhooks.Error -= OnError;
30-
_eventSubWebhooks.UnknownEventSubNotification += OnUnknownEventSubNotification;
31-
_eventSubWebhooks.ChannelFollow -= OnChannelFollow;
30+
_eventSubWebhooks.UnknownEventSubNotification -= OnUnknownEventSubNotification;
31+
_eventSubWebhooks.ChannelChatMessage -= OnChannelChatMessage;
3232
return Task.CompletedTask;
3333
}
3434

35-
private async Task OnChannelFollow(object? sender, ChannelFollowArgs e)
35+
private async Task OnChannelChatMessage(object? sender, ChannelChatMessageArgs e)
3636
{
37-
_logger.LogInformation($"{e.Payload.Event.UserName} followed {e.Payload.Event.BroadcasterUserName} at {e.Payload.Event.FollowedAt.ToUniversalTime()}");
37+
_logger.LogInformation($"@{e.Payload.Event.ChatterUserName} #{e.Payload.Event.BroadcasterUserName}: {e.Payload.Event.Message.Text}");
3838
}
3939

4040
private async Task OnError(object? sender, OnErrorArgs e)
@@ -43,7 +43,7 @@ private async Task OnError(object? sender, OnErrorArgs e)
4343
}
4444

4545
// Handling notifications that are not (yet) implemented
46-
private async Task OnUnknownEventSubNotification(object sender, UnknownEventSubNotificationArgs e)
46+
private async Task OnUnknownEventSubNotification(object? sender, UnknownEventSubNotificationArgs e)
4747
{
4848
var metadata = (WebhookEventSubMetadata)e.Metadata;
4949
_logger.LogInformation("Received event that has not yet been implemented: type:{type}, version:{version}", metadata.SubscriptionType, metadata.SubscriptionVersion);
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# For more info on HTTP files go to https://aka.ms/vs/httpfile
2+
3+
# change values!!
4+
@callbackBase=_example_com_
5+
@clientId=_Client_Id_
6+
@token=_App_Access_Token_
7+
@broadcasterUserId=_Broadcaster_User_Id_
8+
@userId={{broadcasterUserId}}
9+
10+
@secret=supersecuresecret
11+
@callbackPath=/eventsub/
12+
@callbackUrl={{callbackBase}}{{callbackPath}}
13+
14+
# @name getSubscriptions
15+
GET https://api.twitch.tv/helix/eventsub/subscriptions
16+
Authorization: Bearer {{token}}
17+
Client-Id: {{clientId}}
18+
19+
###
20+
21+
DELETE https://api.twitch.tv/helix/eventsub/subscriptions?id={{getSubscriptions.response.body.$.data[0].id}}
22+
Authorization: Bearer {{token}}
23+
Client-Id: {{clientId}}
24+
25+
###
26+
27+
POST https://api.twitch.tv/helix/eventsub/subscriptions
28+
Authorization: Bearer {{token}}
29+
Client-Id: {{clientId}}
30+
Content-Type: application/json
31+
32+
{
33+
"type": "channel.chat.message",
34+
"version": "1",
35+
"condition": {
36+
"broadcaster_user_id": "{{broadcasterUserId}}",
37+
"user_id": "{{userId}}"
38+
},
39+
"transport": {
40+
"method": "webhook",
41+
"callback": "{{callbackUrl}}",
42+
"secret": "{{secret}}"
43+
}
44+
}
45+
46+
###
47+
48+
POST https://api.twitch.tv/helix/chat/messages
49+
Authorization: Bearer {{token}}
50+
Client-Id: {{clientId}}
51+
Content-Type: application/json
52+
53+
{
54+
"broadcaster_id" : "{{broadcasterUserId}}",
55+
"sender_id" : "{{userId}}",
56+
"message" : "Test message"
57+
}

0 commit comments

Comments
 (0)