5
5
using Microsoft . AspNetCore . SignalR . Client ;
6
6
using Microsoft . Extensions . Configuration ;
7
7
using Microsoft . Extensions . Hosting ;
8
+ using Microsoft . Extensions . Logging ;
8
9
9
10
namespace Fritz . Chatbot . Commands
10
11
{
11
- public class AttentionCommand : IBasicCommand
12
+ public class AttentionCommand : IBasicCommand , IDisposable
12
13
{
13
14
private readonly IConfiguration Configuration ;
14
15
15
- public AttentionCommand ( IConfiguration configuration )
16
+ public ILogger Logger { get ; }
17
+
18
+ public AttentionCommand ( IConfiguration configuration , ILoggerFactory loggerFactory )
16
19
{
17
20
this . Configuration = configuration ;
21
+ this . Logger = loggerFactory . CreateLogger ( "AttentionCommand" ) ;
22
+
18
23
var thisUri = new Uri ( configuration [ "FritzBot:ServerUrl" ] , UriKind . Absolute ) ;
19
- this . Client = new HubConnectionBuilder ( ) . WithUrl ( new Uri ( thisUri , "attentionhub" ) . ToString ( ) ) . Build ( ) ;
20
- this . Client . StartAsync ( ) ;
24
+ var attentionUri = new Uri ( thisUri , "attentionhub" ) ;
25
+
26
+ Logger . LogTrace ( $ "Connecting AttentionCommand to: { attentionUri } ") ;
27
+
28
+ this . Client = new HubConnectionBuilder ( ) . WithUrl ( attentionUri . ToString ( ) ) . Build ( ) ;
29
+
21
30
}
22
31
23
32
protected HubConnection Client { get ; }
@@ -30,11 +39,19 @@ public AttentionCommand(IConfiguration configuration)
30
39
31
40
public async Task Execute ( IChatService chatService , string userName , ReadOnlyMemory < char > rhs )
32
41
{
42
+
43
+ await this . Client . StartAsync ( ) ;
44
+
33
45
await this . Client . InvokeAsync ( "AlertFritz" ) ;
34
46
35
47
var attentionText = Configuration [ "FritzBot:AttentionCommand:TemplateText" ] ;
36
48
37
49
await chatService . SendMessageAsync ( string . Format ( attentionText , userName ) ) ;
38
50
}
51
+
52
+ public void Dispose ( )
53
+ {
54
+ Client . DisposeAsync ( ) ;
55
+ }
39
56
}
40
57
}
0 commit comments