Skip to content

Commit 6c86368

Browse files
committed
Now querying Fauna for information about the hat discovered
1 parent 784db42 commit 6c86368

File tree

7 files changed

+79
-5
lines changed

7 files changed

+79
-5
lines changed

Fritz.Chatbot/Commands/PredictHatCommand.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@ public class PredictHatCommand : IBasicCommand
2626

2727
internal static string IterationName = "";
2828
private ScreenshotTrainingService _TrainHat;
29+
private readonly HatDescriptionRepository _Repository;
2930

30-
public PredictHatCommand(IConfiguration configuration, ScreenshotTrainingService service)
31+
public PredictHatCommand(IConfiguration configuration, ScreenshotTrainingService service, HatDescriptionRepository repository)
3132
{
3233
_CustomVisionKey = configuration["AzureServices:HatDetection:Key"];
3334
_AzureEndpoint = configuration["AzureServices:HatDetection:CustomVisionEndpoint"];
3435
_TwitchChannel = configuration["StreamServices:Twitch:Channel"];
3536
_AzureProjectId = Guid.Parse(configuration["AzureServices:HatDetection:ProjectId"]);
3637
_TrainHat = service;
38+
_Repository = repository;
3739
}
3840

3941
public string TwitchScreenshotUrl => $"https://static-cdn.jtvnw.net/previews-ttv/live_user_{_TwitchChannel}-1280x720.jpg?_=";
@@ -82,6 +84,10 @@ public async Task Execute(IChatService chatService, string userName, ReadOnlyMem
8284
}
8385

8486
await chatService.SendMessageAsync($"csharpClip I think (with {bestMatch.Probability.ToString("0.0%")} certainty) Jeff is currently wearing his {bestMatch.TagName} hat csharpClip");
87+
if (bestMatch.Probability >= 0.6D) {
88+
var desc = await _Repository.GetDescription(bestMatch.TagName);
89+
if (!string.IsNullOrEmpty(desc)) await chatService.SendMessageAsync(desc);
90+
}
8591

8692
}
8793

Fritz.Chatbot/Fritz.Chatbot.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</ItemGroup>
1010

1111
<ItemGroup>
12+
<PackageReference Include="FaunaDB.Client" Version="2.12.0" />
1213
<PackageReference Include="Markdig" Version="0.18.3" />
1314
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.0.4" />
1415
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="1.0.4" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using FaunaDB.Client;
2+
using Microsoft.Extensions.Configuration;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
using static FaunaDB.Query.Language;
9+
using static FaunaDB.Types.Option;
10+
using static FaunaDB.Types.Encoder;
11+
using FaunaDB.Types;
12+
13+
namespace Fritz.Chatbot
14+
{
15+
public class HatDescriptionRepository
16+
{
17+
private readonly FaunaClient _Client;
18+
19+
public HatDescriptionRepository(IConfiguration configuration)
20+
{
21+
22+
var secret = configuration["FaunaDb:Secret"];
23+
24+
_Client = new FaunaClient(secret);
25+
26+
}
27+
28+
/// <summary>
29+
/// Get the description, if any, that goes with the tag for the hat identified
30+
/// </summary>
31+
/// <param name="tag">The unique tag for the hat</param>
32+
/// <returns>Description (if any) for the hat</returns>
33+
public async Task<string> GetDescription(string tag) {
34+
35+
try
36+
{
37+
var singleMatch = await _Client.Query(Get(Match(Index("hats_tag_desc"), tag)));
38+
39+
return singleMatch.Get(Field.At("data")).At("description").To<string>().Value;
40+
} catch {
41+
// No result found
42+
return "";
43+
}
44+
}
45+
46+
}
47+
}

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ private static void RegisterGitHubServices(IServiceCollection services, IConfigu
107107
//var provider = services.BuildServiceProvider();
108108
//var svc = provider.GetRequiredService<ScreenshotTrainingService>();
109109
services.AddHostedService<ScreenshotTrainingService>(s => s.GetRequiredService<ScreenshotTrainingService>());
110+
services.AddTransient<HatDescriptionRepository>();
110111
}
111112

112113
private static void AddStreamingServices(this IServiceCollection services, IConfiguration configuration)

Fritz.StreamTools/appsettings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,5 +245,9 @@
245245
"csharpfritz",
246246
"dependabot[bot]"
247247
]
248+
},
249+
"FaunaDb": {
250+
"Endpoint": "<<endpoint>>",
251+
"Secret": "<<secret>>"
248252
}
249253
}

Test/Startup/ConfigureServicesTests.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Fritz.StreamTools.StartupServices;
1010
using Microsoft.Extensions.Configuration;
1111
using Microsoft.Extensions.DependencyInjection;
12+
using Microsoft.Extensions.FileProviders;
1213
using Microsoft.Extensions.Hosting;
1314
using Microsoft.Extensions.Logging;
1415
using Microsoft.Extensions.Logging.Abstractions;
@@ -25,12 +26,14 @@ public void Execute_ShouldRegitserService_WhenAllRequiredConfigurationDone()
2526
new Dictionary<string, string>()
2627
{
2728
{ "FakeConfiguration:PropertyOne", "RandomValue" },
28-
{ "FakeConfiguration:PropertyTwo", "RandomValue" }
29+
{ "FakeConfiguration:PropertyTwo", "RandomValue" },
30+
{ "AzureServices:HatDetection:ProjectId", Guid.NewGuid().ToString() }
2931
}).Build();
3032

3133
var serviceCollection = new ServiceCollection();
3234
serviceCollection.AddSingleton<IConfiguration>(configuration);
3335
serviceCollection.AddSingleton<ILogger>(NullLogger.Instance);
36+
serviceCollection.AddSingleton<IHostEnvironment>(new FakeHostEnvironment());
3437

3538

3639
var serviceRequriedConfiguration = new Dictionary<Type, string[]>()
@@ -51,11 +54,13 @@ public void Execute_ShouldSkipRegisterServices_IfAnyOfRequiredConfigurationNotPa
5154
new Dictionary<string, string>()
5255
{
5356
{ "FakeConfiguration:PropertyOne", "RandomValue" },
57+
{ "AzureServices:HatDetection:ProjectId", Guid.NewGuid().ToString() }
5458
}).Build();
5559

5660
var serviceCollection = new ServiceCollection();
5761
serviceCollection.AddSingleton<IConfiguration>(configuration);
5862
serviceCollection.AddSingleton<ILogger>(NullLogger.Instance);
63+
serviceCollection.AddSingleton<IHostEnvironment>(new FakeHostEnvironment());
5964

6065

6166
var serviceRequriedConfiguration = new Dictionary<Type, string[]>()
@@ -80,6 +85,7 @@ public void Execute_RegisterStreamServicesWithVariousConfigurations_ReturnExpect
8085
serviceCollection.AddSingleton<ILoggerFactory>(new LoggerFactory());
8186
serviceCollection.AddSingleton<IConfiguration>(configuration);
8287
serviceCollection.AddSingleton<ILogger>(NullLogger.Instance);
88+
serviceCollection.AddSingleton<IHostEnvironment>(new FakeHostEnvironment());
8389

8490
// act
8591
ConfigureServices.Execute(serviceCollection, configuration, new Dictionary<Type, string[]>());
@@ -109,7 +115,8 @@ private static Dictionary<string, string> MakeFakeConfiguration(string twitchCli
109115
{"StreamServices:Twitch:ClientId", twitchClientId},
110116
{"StreamServices:Mixer:Channel", mixerClientId},
111117
{"StreamServices:Fake:Enabled", enableFake.ToString()},
112-
{"FritzBot:ServerUrl", "http://localhost:80" }
118+
{"FritzBot:ServerUrl", "http://localhost:80" },
119+
{"AzureServices:HatDetection:ProjectId", Guid.NewGuid().ToString() }
113120
};
114121
}
115122

@@ -125,5 +132,13 @@ public Task StopAsync(CancellationToken cancellationToken)
125132
return null;
126133
}
127134
}
128-
}
135+
136+
private class FakeHostEnvironment : IHostEnvironment
137+
{
138+
public string ApplicationName { get; set; }
139+
public IFileProvider ContentRootFileProvider { get; set; }
140+
public string ContentRootPath { get; set; }
141+
public string EnvironmentName { get; set; }
142+
}
143+
}
129144
}

Test/Twitch/Proxy/GetFollowerCount.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public GetFollowerCount(ITestOutputHelper output)
4242
public ITestOutputHelper Output { get; }
4343
public XUnitLogger Logger { get; }
4444

45-
[Fact]
45+
[Fact(Skip ="Not used")]
4646
public async Task ShouldReturnNonZeroCount()
4747
{
4848

0 commit comments

Comments
 (0)