Skip to content

Commit 20126c2

Browse files
committed
Added a SignalR Hub to play sound in a webpart
If you load the webpart into OBS it will play a sound when the !attention command is raised.
1 parent bc0f82a commit 20126c2

File tree

12 files changed

+194
-19
lines changed

12 files changed

+194
-19
lines changed

Fritz.Chatbot/Commands/AttentionCommand.cs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,34 @@
77

88
namespace Fritz.Chatbot.Commands
99
{
10-
public class AttentionCommand : IBasicCommand
11-
{
12-
private readonly IConfiguration Configuration;
13-
14-
public AttentionCommand(IConfiguration configuration)
10+
public class AttentionCommand : IBasicCommand
1511
{
16-
this.Configuration = configuration;
17-
}
12+
private readonly IConfiguration Configuration;
1813

19-
public string Trigger => "attention";
14+
public AttentionCommand(IAttentionClient client, IConfiguration configuration)
15+
{
16+
this.Configuration = configuration;
17+
this.Client = client;
18+
}
2019

21-
public string Description => "Play audio queue to divert attention to chat";
20+
protected IAttentionClient Client { get; }
2221

23-
public TimeSpan? Cooldown => TimeSpan.FromSeconds(5);
22+
public string Trigger => "attention";
2423

25-
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
26-
{
27-
var player = new NetCoreAudio.Player();
28-
await player.Play("hey_listen.wav");
24+
public string Description => "Play audio queue to divert attention to chat";
25+
26+
public TimeSpan? Cooldown => TimeSpan.FromSeconds(5);
27+
28+
public async Task Execute(IChatService chatService, string userName, ReadOnlyMemory<char> rhs)
29+
{
30+
await this.Client.AlertFritz();
2931

30-
var attentionText = Configuration["FritzBot:AttentionCommand:TemplateText"];
32+
var attentionText = Configuration["FritzBot:AttentionCommand:TemplateText"];
3133

32-
var sb = new StringBuilder();
33-
sb.AppendFormat(attentionText, userName);
34+
var sb = new StringBuilder();
35+
sb.AppendFormat(attentionText, userName);
3436

35-
await chatService.SendMessageAsync(attentionText);
37+
await chatService.SendMessageAsync(attentionText);
38+
}
3639
}
37-
}
3840
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Threading.Tasks;
5+
6+
namespace Fritz.StreamLib.Core
7+
{
8+
public interface IAttentionClient
9+
{
10+
Task AlertFritz();
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Mvc;
6+
7+
namespace Fritz.StreamTools.Controllers
8+
{
9+
public class AttentionController : Controller
10+
{
11+
public IActionResult Index()
12+
{
13+
return View();
14+
}
15+
16+
public IActionResult TestClient()
17+
{
18+
return View();
19+
}
20+
}
21+
}

Fritz.StreamTools/Fritz.StreamTools.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
<EmbeddedResource Remove="ClientApp\**" />
1212
<None Remove="ClientApp\**" />
1313
</ItemGroup>
14+
<ItemGroup>
15+
<Content Remove="wwwroot\hey_listen.wav" />
16+
</ItemGroup>
1417
<ItemGroup>
1518
<PackageReference Include="BuildBundlerMinifier" Version="2.8.391" />
1619
<PackageReference Include="Bundgaard.MixerLib" Version="1.2.65" />
@@ -24,6 +27,9 @@
2427
<PackageReference Include="System.Memory" Version="4.5.1" />
2528
</ItemGroup>
2629
<ItemGroup>
30+
<None Include="wwwroot\contents\hey_listen.wav">
31+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
32+
</None>
2733
<None Include="wwwroot\js\GoalConfiguration\GoalConfiguration.js" />
2834
<None Include="wwwroot\js\GoalConfiguration\GoogleFonts.js" />
2935
<None Include="wwwroot\js\GoalConfiguration\Preview.js" />
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Fritz.StreamLib.Core;
2+
using Microsoft.AspNetCore.SignalR;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
8+
namespace Fritz.StreamTools.Hubs
9+
{
10+
public interface IAttentionHubClient
11+
{
12+
Task AlertFritz();
13+
Task ClientConnected(string connectionId);
14+
}
15+
16+
public class AttentionHub : Hub<IAttentionHubClient>, IAttentionClient
17+
{
18+
public override Task OnConnectedAsync()
19+
{
20+
return this.Clients.Others.ClientConnected(this.Context.ConnectionId);
21+
}
22+
23+
public Task AlertFritz()
24+
{
25+
return this.Clients.Others.AlertFritz();
26+
}
27+
}
28+
}

Fritz.StreamTools/Startup.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public void Configure(IApplicationBuilder app, Microsoft.Extensions.Hosting.IHos
5555
{
5656
configure.MapHub<FollowerHub>("/followerstream");
5757
configure.MapHub<GithubyMcGithubFace>("/github");
58+
configure.MapHub<AttentionHub>("/attentionhub");
5859
});
5960

6061
app.UseMvc(routes =>

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public static void Execute(
3838
services.AddSingleton<IConfigureOptions<SignalrTagHelperOptions>, ConfigureSignalrTagHelperOptions>();
3939
services.AddSingleton<SignalrTagHelperOptions>(cfg => cfg.GetService<IOptions<SignalrTagHelperOptions>>().Value);
4040

41+
services.AddSingleton<IAttentionClient, AttentionHub>();
42+
4143
services.AddSingleton<IHostedService, FritzBot>();
4244
services.AddSingleton(new GitHubClient(new ProductHeaderValue("Fritz.StreamTools")));
4345
FritzBot.RegisterCommands(services);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@{
2+
Layout = null;
3+
}
4+
5+
<!DOCTYPE html>
6+
7+
<html>
8+
<head>
9+
<title>Attention</title>
10+
</head>
11+
<body>
12+
<script src="~/lib/signalr/signalr-client.js"></script>
13+
<script src="~/js/attentionhub.js"></script>
14+
<script>
15+
var audio = new Audio('@Url.Content(@"~/contents/hey_listen.wav")');
16+
17+
var hub = new AttentionHub();
18+
hub.onAlertFritz = () => {
19+
alertFritz();
20+
}
21+
22+
function alertFritz() {
23+
audio.play();
24+
}
25+
26+
(function () {
27+
hub.start();
28+
})();
29+
30+
</script>
31+
</body>
32+
</html>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
@{
3+
ViewData["Title"] = "Test Client";
4+
}
5+
6+
<h2>Test Client</h2>
7+
8+
<a class="btn btn-primary" href="javascript:alertFritz();">Send Alert</a>
9+
10+
@section scripts {
11+
<script src="~/lib/signalr/signalr-client.js"></script>
12+
<script src="~/js/attentionhub.js"></script>
13+
<script type="text/javascript">
14+
var attentionHub = new AttentionHub();
15+
16+
(function () {
17+
attentionHub.start();
18+
})();
19+
20+
function alertFritz() {
21+
attentionHub.sendTest();
22+
}
23+
</script>
24+
}

Fritz.StreamTools/Views/Shared/_Layout.cshtml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
<li class="nav-item">
3434
<a class="nav-link" asp-page="/CurrentViewers">Current Viewers</a>
3535
</li>
36+
<li class="nav-item">
37+
<a class="nav-link" asp-controller="Attention" asp-action="TestClient">Attention Test</a>
38+
</li>
3639
</ul>
3740
</div>
3841
</nav>

0 commit comments

Comments
 (0)