@@ -40,9 +40,23 @@ public class Dinobot
40
40
private IBotPoster _botPoster ;
41
41
private TraceWriter _log ;
42
42
private string _botId ;
43
- private MessageItem _message ;
44
43
45
- internal Dinobot ( TraceWriter log , string botId , IBotPoster botPoster )
44
+ /// <summary>
45
+ /// Initializes a new instance of the <see cref="Dinobot"/> class
46
+ /// </summary>
47
+ /// <param name="botId">ID of the bot to use to post messages</param>
48
+ /// <param name="botPoster">IBotPoster to use to post bot messages</param>
49
+ public Dinobot ( string botId , IBotPoster botPoster ) : this ( botId , botPoster , null )
50
+ {
51
+ }
52
+
53
+ /// <summary>
54
+ /// Initializes a new instance of the <see cref="Dinobot"/> class
55
+ /// </summary>
56
+ /// <param name="botId">ID of the bot to use to post messages</param>
57
+ /// <param name="botPoster">IBotPoster to use to post bot messages</param>
58
+ /// <param name="log">Logger to use. Optional.</param>
59
+ public Dinobot ( string botId , IBotPoster botPoster , TraceWriter log )
46
60
{
47
61
if ( botPoster == null )
48
62
{
@@ -58,6 +72,11 @@ internal Dinobot(TraceWriter log, string botId, IBotPoster botPoster)
58
72
_botPoster = botPoster ;
59
73
}
60
74
75
+ /// <summary>
76
+ /// Gets or sets the message to parse
77
+ /// </summary>
78
+ public MessageItem Message { get ; set ; }
79
+
61
80
/// <summary>
62
81
/// Message called when the Azure function is invoked
63
82
///
@@ -86,7 +105,7 @@ public static async Task<HttpResponseMessage> Run([HttpTrigger(Route = "DinoCall
86
105
}
87
106
88
107
var botPoster = new BotPoster ( BotPostUrl ) ;
89
- var bot = new Dinobot ( log , botId , botPoster ) ;
108
+ var bot = new Dinobot ( botId , botPoster , log ) ;
90
109
bool parsedMessage = await bot . ParseIncomingRequestAsync ( req ) ;
91
110
if ( parsedMessage )
92
111
{
@@ -106,39 +125,39 @@ public static async Task<HttpResponseMessage> Run([HttpTrigger(Route = "DinoCall
106
125
/// </summary>
107
126
/// <param name="request">Incoming request</param>
108
127
/// <returns>True if a message was properly parsed from the request</returns>
109
- internal async Task < bool > ParseIncomingRequestAsync ( HttpRequestMessage request )
128
+ public async Task < bool > ParseIncomingRequestAsync ( HttpRequestMessage request )
110
129
{
111
130
if ( request == null )
112
131
{
113
132
return false ;
114
133
}
115
134
116
135
string content = await request . Content . ReadAsStringAsync ( ) ;
117
- _message = JsonSerializer . DeserializeJson < MessageItem > ( content ) ;
118
- return _message ? . Text != null ;
136
+ Message = JsonSerializer . DeserializeJson < MessageItem > ( content ) ;
137
+ return Message ? . Text != null ;
119
138
}
120
139
121
140
/// <summary>
122
141
/// Processes a message and sends dinos if necessary
123
142
/// </summary>
124
143
/// <returns>True if a bot message was sent, false if message was processed with no action</returns>
125
- internal async Task < bool > ProcessMessageAsync ( )
144
+ public async Task < bool > ProcessMessageAsync ( )
126
145
{
127
- if ( _message == null )
146
+ if ( Message == null )
128
147
{
129
148
return false ;
130
149
}
131
150
132
- _log ? . Info ( "Parsed message for " + _message . GroupId ) ;
133
- if ( string . IsNullOrEmpty ( _message . GroupId ) )
151
+ _log ? . Info ( "Parsed message for " + Message . GroupId ) ;
152
+ if ( string . IsNullOrEmpty ( Message . GroupId ) )
134
153
{
135
154
_log ? . Info ( "Not a group message, ignoring" ) ;
136
155
return false ;
137
156
}
138
157
else
139
158
{
140
159
UpdateEnvironmentVariables ( ) ;
141
- return await HandleIncomingMessageAsync ( _log , _message , _botId ) ;
160
+ return await HandleIncomingMessageAsync ( Message , _botId ) ;
142
161
}
143
162
}
144
163
@@ -177,38 +196,37 @@ private void UpdateEnvironmentVariableForContainer(string key, ICollection<strin
177
196
/// <summary>
178
197
/// Handles an incoming message and sends a bot message if appropriate
179
198
/// </summary>
180
- /// <param name="log">Logger for the operation</param>
181
199
/// <param name="message">Message to process</param>
182
200
/// <param name="botId">ID of the bot to use to send messages</param>
183
201
/// <returns>True if a bot message was sent</returns>
184
- private async Task < bool > HandleIncomingMessageAsync ( TraceWriter log , MessageItem message , string botId )
202
+ private async Task < bool > HandleIncomingMessageAsync ( MessageItem message , string botId )
185
203
{
186
204
// Checks for the type of messages Dinobot will respond to, in decreasing priority order
187
205
// TODO: It would be cool if these were some sort of class-based trigger system you added to a collection rather than this if/else block
188
206
string text = message . Text ? . ToLower ( ) ;
189
- if ( await CheckDinoRequest ( log , text , message , botId ) )
207
+ if ( await CheckDinoRequest ( text , message , botId ) )
190
208
{
191
- log ? . Info ( "Posted Dino" ) ;
209
+ _log ? . Info ( "Posted Dino" ) ;
192
210
return true ;
193
211
}
194
- else if ( await CheckDinoAddressed ( log , text , message , botId ) )
212
+ else if ( await CheckDinoAddressed ( text , message , botId ) )
195
213
{
196
- log ? . Info ( "Dino responded" ) ;
214
+ _log ? . Info ( "Dino responded" ) ;
197
215
return true ;
198
216
}
199
- else if ( await CheckRandy ( log , text , botId ) )
217
+ else if ( await CheckRandy ( text , botId ) )
200
218
{
201
- log ? . Info ( "Posted Randy" ) ;
219
+ _log ? . Info ( "Posted Randy" ) ;
202
220
return true ;
203
221
}
204
- else if ( await CheckDinoQuestion ( log , text , message , botId ) )
222
+ else if ( await CheckDinoQuestion ( text , message , botId ) )
205
223
{
206
- log ? . Info ( "Posted DinoQ" ) ;
224
+ _log ? . Info ( "Posted DinoQ" ) ;
207
225
return true ;
208
226
}
209
227
else
210
228
{
211
- log ? . Info ( "No dino message" ) ;
229
+ _log ? . Info ( "No dino message" ) ;
212
230
}
213
231
214
232
return false ;
@@ -219,20 +237,19 @@ private async Task<bool> HandleIncomingMessageAsync(TraceWriter log, MessageItem
219
237
///
220
238
/// For example, a message contains the text "3 dinos" will result in 3 dino emojis sent by the bot.
221
239
/// </summary>
222
- /// <param name="log">Logger for the operation</param>
223
240
/// <param name="messageText">Pre-processed text for the message</param>
224
241
/// <param name="message">Message being processed</param>
225
242
/// <param name="botId">ID of the bot to send the message</param>
226
243
/// <returns>True if a message was sent by the bot, otherwise false</returns>
227
- private async Task < bool > CheckDinoRequest ( TraceWriter log , string messageText , MessageItem message , string botId )
244
+ private async Task < bool > CheckDinoRequest ( string messageText , MessageItem message , string botId )
228
245
{
229
246
Match match = DinoRegex . Match ( messageText ) ;
230
247
if ( match . Success && match . Groups . Count == 4 )
231
248
{
232
- log ? . Info ( "Found dino" ) ;
249
+ _log ? . Info ( "Found dino" ) ;
233
250
string num = match . Groups [ 2 ] . Value ;
234
251
int dinos = Math . Min ( int . Parse ( num ) , MaxDinos ) ;
235
- log ? . Info ( "Dinos:" + dinos . ToString ( ) ) ;
252
+ _log ? . Info ( "Dinos:" + dinos . ToString ( ) ) ;
236
253
237
254
if ( dinos > 0 )
238
255
{
@@ -250,16 +267,15 @@ private async Task<bool> CheckDinoRequest(TraceWriter log, string messageText, M
250
267
/// For example, a message containing the test "Hey DinoBot" will result in a dinosaur emoji being sent.
251
268
/// The trigger text and users who can use this trigger are configurable via AppSettings
252
269
/// </summary>
253
- /// <param name="log">Logger for the operation</param>
254
270
/// <param name="messageText">Pre-processed text for the message</param>
255
271
/// <param name="message">Message being processed</param>
256
272
/// <param name="botId">ID of the bot to send the message</param>
257
273
/// <returns>True if a message was sent by the bot, otherwise false</returns>
258
- private async Task < bool > CheckDinoAddressed ( TraceWriter log , string messageText , MessageItem message , string botId )
274
+ private async Task < bool > CheckDinoAddressed ( string messageText , MessageItem message , string botId )
259
275
{
260
276
if ( ( _canAddressDino . Count == 0 || _canAddressDino . Contains ( message . UserId ) ) && _dinoAddressTrigger . Any ( t => messageText . Contains ( t ) ) )
261
277
{
262
- log ? . Info ( "Dino addressed" ) ;
278
+ _log ? . Info ( "Dino addressed" ) ;
263
279
Attachment existingReply = message . GetExistingReply ( ) ;
264
280
HttpStatusCode response = await _botPoster . PostEmojiAsync ( DinoPack , DinoEmoji , 1 , botId , EmojiPostDelayMs , existingReply ? . ReplyId ?? null , existingReply ? . BaseReplyId ?? null ) ;
265
281
return response == HttpStatusCode . Accepted ;
@@ -270,18 +286,17 @@ private async Task<bool> CheckDinoAddressed(TraceWriter log, string messageText,
270
286
/// <summary>
271
287
/// Checks if the user requests the Randy emoji
272
288
/// </summary>
273
- /// <param name="log">Logger for the operation</param>
274
289
/// <param name="messageText">Pre-processed text for the message</param>
275
290
/// <param name="botId">ID of the bot to send the message</param>
276
291
/// <returns>True if a message was sent by the bot, otherwise false</returns>
277
- private async Task < bool > CheckRandy ( TraceWriter log , string messageText , string botId )
292
+ private async Task < bool > CheckRandy ( string messageText , string botId )
278
293
{
279
294
if ( messageText . Contains ( "(randy pooping)" ) )
280
295
{
281
296
HttpStatusCode response = await _botPoster . PostEmojiAsync ( RandyPack , RandyEmoji , 1 , botId , EmojiPostDelayMs ) ;
282
297
if ( response == HttpStatusCode . Accepted )
283
298
{
284
- log ? . Info ( "Posted Randy:" + response . ToString ( ) ) ;
299
+ _log ? . Info ( "Posted Randy:" + response . ToString ( ) ) ;
285
300
return true ;
286
301
}
287
302
}
@@ -291,20 +306,19 @@ private async Task<bool> CheckRandy(TraceWriter log, string messageText, string
291
306
/// <summary>
292
307
/// Checks if the user asked a question and uses the most sophisticated AI to determine if Dinobot should respond
293
308
/// </summary>
294
- /// <param name="log">Logger for the operation</param>
295
309
/// <param name="messageText">Pre-processed text for the message</param>
296
310
/// <param name="message">The message being processed</param>
297
311
/// <param name="botId">ID of the bot to send the message</param>
298
312
/// <returns>True if a message was sent by the bot, otherwise false</returns>
299
- private async Task < bool > CheckDinoQuestion ( TraceWriter log , string messageText , MessageItem message , string botId )
313
+ private async Task < bool > CheckDinoQuestion ( string messageText , MessageItem message , string botId )
300
314
{
301
315
if ( messageText . Contains ( "?" ) )
302
316
{
303
317
Random rand = new Random ( ) ;
304
318
double val = rand . NextDouble ( ) ;
305
319
if ( val <= ResponseWeight )
306
320
{
307
- log ? . Info ( "Posting response" ) ;
321
+ _log ? . Info ( "Posting response" ) ;
308
322
Attachment existingReply = message . GetExistingReply ( ) ;
309
323
HttpStatusCode response = await _botPoster . PostEmojiAsync ( DinoPack , DinoEmoji , 1 , botId , EmojiPostDelayMs , message . MessageId , existingReply ? . BaseReplyId ?? null ) ;
310
324
return response == HttpStatusCode . Accepted ;
0 commit comments