Skip to content

Commit 8e96238

Browse files
committed
Fixed the shoutout command to use the Twitch Helix endpoint
1 parent 87ad9ac commit 8e96238

File tree

6 files changed

+108
-7
lines changed

6 files changed

+108
-7
lines changed

Fritz.Chatbot/Commands/ShoutoutCommand.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
using System.Net.Http;
44
using System.Threading.Tasks;
55
using Fritz.StreamLib.Core;
6+
using Microsoft.Extensions.Logging;
67

78
namespace Fritz.Chatbot.Commands
89
{
910
public class ShoutoutCommand : IBasicCommand2
1011
{
1112
private readonly HttpClient _HttpClient;
13+
private readonly ILogger _Logger;
1214

13-
public ShoutoutCommand(IHttpClientFactory httpClientFactory)
15+
public ShoutoutCommand(IHttpClientFactory httpClientFactory, ILogger logger)
1416
{
1517

1618
_HttpClient = httpClientFactory.CreateClient("ShoutoutCommand");
19+
_HttpClient.BaseAddress = new Uri("https://api.twitch.tv/helix/users");
20+
21+
_Logger = logger;
1722

1823
}
1924

@@ -35,8 +40,12 @@ public async Task Execute(IChatService chatService, string userName, bool isMode
3540
if (rhsTest.Contains(" ")) return;
3641

3742
rhsTest = WebUtility.UrlEncode(rhsTest);
38-
var result = await _HttpClient.GetAsync(rhsTest);
39-
if (result.StatusCode != HttpStatusCode.OK) return;
43+
var result = await _HttpClient.GetAsync($"?login={rhsTest}");
44+
if (result.StatusCode != HttpStatusCode.OK)
45+
{
46+
_Logger.LogWarning($"Unable to verify Shoutout for {rhsTest}");
47+
return;
48+
}
4049

4150
await chatService.SendMessageAsync($"Please follow @{rhsTest} at: https://twitch.tv/{rhsTest}");
4251

Fritz.StreamTools.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFi
1919
docker-compose.ci.build.yml = docker-compose.ci.build.yml
2020
docker-compose.override.yml = docker-compose.override.yml
2121
docker-compose.yml = docker-compose.yml
22+
global.json = global.json
2223
LICENSE = LICENSE
2324
README.md = README.md
2425
EndProjectSection

Fritz.StreamTools/StartupServices/ConfigureServices.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@ private static void RegisterGitHubServices(IServiceCollection services, IConfigu
9191

9292
services.AddHttpClient("ShoutoutCommand", c =>
9393
{
94-
c.BaseAddress = new Uri("https://api.twitch.tv/kraken/channels/");
9594
c.DefaultRequestHeaders.Add("client-id", configuration["StreamServices:Twitch:ClientId"]);
9695
});
9796

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Fritz.Chatbot.Commands;
2+
using Fritz.StreamLib.Core;
3+
using Microsoft.Extensions.Logging;
4+
using Moq;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Net.Http;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace Test.Chatbot
14+
{
15+
16+
public class ShoutoutCommandFixture
17+
{
18+
private readonly MockRepository _Mockery;
19+
private readonly Mock<IHttpClientFactory> _ClientFactory;
20+
private readonly Mock<IChatService> _ChatService;
21+
private readonly ILogger _Logger;
22+
23+
public ShoutoutCommandFixture(ITestOutputHelper output)
24+
{
25+
_Mockery = new MockRepository(MockBehavior.Default);
26+
_ClientFactory = new Mock<IHttpClientFactory>();
27+
_ChatService = new Mock<IChatService>();
28+
_Logger = new XunitLogger<ShoutoutCommand>(output);
29+
30+
}
31+
32+
33+
[Fact]
34+
public async Task ShouldShoutoutCsharpfritz() {
35+
36+
// arrange
37+
var client = new HttpClient();
38+
client.DefaultRequestHeaders.Add("client-id", "t7y5txan5q662t7zj7p3l4wlth8zhv");
39+
_ClientFactory.Setup(f => f.CreateClient(It.IsAny<string>()))
40+
.Returns(client);
41+
var sut = new ShoutoutCommand(_ClientFactory.Object, _Logger);
42+
_ChatService.Setup(c => c.SendMessageAsync(It.Is<string>(s => s.StartsWith("Please follow @csharpfritz")))).Verifiable();
43+
44+
// act
45+
await sut.Execute(_ChatService.Object, "csharpfritz", false, false, true, "csharpfritz".ToCharArray());
46+
47+
// assert
48+
_ChatService.Verify();
49+
50+
51+
}
52+
53+
}
54+
55+
56+
}

Test/XunitLogger.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Text;
5+
using Xunit.Abstractions;
6+
7+
namespace Test
8+
{
9+
public class XunitLogger<T> : ILogger<T>, IDisposable
10+
{
11+
private ITestOutputHelper _output;
12+
13+
public XunitLogger(ITestOutputHelper output)
14+
{
15+
_output = output;
16+
}
17+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
18+
{
19+
_output.WriteLine(state.ToString());
20+
}
21+
22+
public bool IsEnabled(LogLevel logLevel)
23+
{
24+
return true;
25+
}
26+
27+
public IDisposable BeginScope<TState>(TState state)
28+
{
29+
return this;
30+
}
31+
32+
public void Dispose()
33+
{
34+
}
35+
}
36+
}

global.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"sdk": {
3-
"version": "2.2.300"
4-
}
2+
"sdk": {
3+
"version": "2.2.401"
4+
}
55
}

0 commit comments

Comments
 (0)