1
1
// Copyright (c) 2024 Quetzal Rivera.
2
2
// Licensed under the GNU General Public License v3.0, See LICENCE in the project root for license information.
3
3
4
- using Microsoft . Extensions . Caching . Memory ;
5
4
using SauceNAO . Application . Models ;
6
5
using SauceNAO . Application . Services ;
7
6
using SauceNAO . Domain . Entities . SauceAggregate ;
@@ -66,18 +65,15 @@ CancellationToken cancellationToken
66
65
if ( message . Chat . Type == ChatTypes . Private )
67
66
{
68
67
// Send a message indicating that the bot is processing the command.
69
- client . SendChatAction (
70
- message . Chat . Id ,
71
- ChatActions . Typing
72
- ) ;
68
+ client . SendChatAction ( message . Chat . Id , ChatActions . Typing ) ;
73
69
74
70
var action = args . FirstOrDefault ( ) ?? "help" ;
75
71
switch ( action )
76
72
{
77
73
case ADD_VALUE :
78
74
79
75
// Initialize the user state.
80
- var initialData = new Dictionary < string , object ? > ( )
76
+ var initialData = new Dictionary < string , string ? > ( )
81
77
{
82
78
{ ACTION_PARAM_NAME , ADD_VALUE } ,
83
79
{ NAME_PARAM_NAME , args . ElementAtOrDefault ( 1 ) } ,
@@ -88,8 +84,11 @@ CancellationToken cancellationToken
88
84
// If the third argument is provided, change it to a boolean.
89
85
if ( initialData [ IS_PUBLIC_PARAM_NAME ] is string isPublic )
90
86
{
91
- initialData [ IS_PUBLIC_PARAM_NAME ] =
92
- isPublic == "true" || isPublic == "--public" ;
87
+ initialData [ IS_PUBLIC_PARAM_NAME ] = (
88
+ isPublic == "true" || isPublic == "--public"
89
+ )
90
+ . ToString ( )
91
+ . ToLowerInvariant ( ) ;
93
92
}
94
93
95
94
return this . InitializeStateAsync ( initialData , message , cancellationToken ) ;
@@ -105,10 +104,10 @@ CancellationToken cancellationToken
105
104
}
106
105
107
106
return this . InitializeStateAsync (
108
- new Dictionary < string , object ? >
107
+ new Dictionary < string , string ? >
109
108
{
110
109
{ ACTION_PARAM_NAME , DELETE_VALUE } ,
111
- { NAME_PARAM_NAME , args [ 1 ] }
110
+ { NAME_PARAM_NAME , args [ 1 ] } ,
112
111
} ,
113
112
message ,
114
113
cancellationToken
@@ -133,9 +132,9 @@ CancellationToken cancellationToken
133
132
replyParameters : new ReplyParameters
134
133
{
135
134
MessageId = message . MessageId ,
136
- AllowSendingWithoutReply = true
135
+ AllowSendingWithoutReply = true ,
137
136
} ,
138
- linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true , } ,
137
+ linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true } ,
139
138
cancellationToken : cancellationToken
140
139
) ;
141
140
@@ -153,9 +152,9 @@ CancellationToken cancellationToken
153
152
replyParameters : new ReplyParameters
154
153
{
155
154
MessageId = message . MessageId ,
156
- AllowSendingWithoutReply = true
155
+ AllowSendingWithoutReply = true ,
157
156
} ,
158
- linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true , } ,
157
+ linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true } ,
159
158
cancellationToken : cancellationToken
160
159
) ;
161
160
}
@@ -167,9 +166,9 @@ CancellationToken cancellationToken
167
166
replyParameters : new ReplyParameters
168
167
{
169
168
MessageId = message . MessageId ,
170
- AllowSendingWithoutReply = true
169
+ AllowSendingWithoutReply = true ,
171
170
} ,
172
- linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true , } ,
171
+ linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true } ,
173
172
cancellationToken : cancellationToken
174
173
) ;
175
174
default :
@@ -180,9 +179,9 @@ CancellationToken cancellationToken
180
179
replyParameters : new ReplyParameters
181
180
{
182
181
MessageId = message . MessageId ,
183
- AllowSendingWithoutReply = true
182
+ AllowSendingWithoutReply = true ,
184
183
} ,
185
- linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true , } ,
184
+ linkPreviewOptions : new LinkPreviewOptions { IsDisabled = true } ,
186
185
cancellationToken : cancellationToken
187
186
) ;
188
187
}
@@ -192,7 +191,7 @@ CancellationToken cancellationToken
192
191
}
193
192
194
193
protected async Task InitializeStateAsync (
195
- IDictionary < string , object ? > initialData ,
194
+ IDictionary < string , string ? > initialData ,
196
195
Message message ,
197
196
CancellationToken cancellationToken
198
197
)
@@ -222,9 +221,9 @@ CancellationToken cancellationToken
222
221
{
223
222
case ADD_VALUE :
224
223
{
225
- var name = ( string ? ) userState . Data [ NAME_PARAM_NAME ] ;
226
- var apikey = ( string ? ) userState . Data [ API_KEY_PARAM_NAME ] ;
227
- var isPublic = ( bool ? ) userState . Data [ IS_PUBLIC_PARAM_NAME ] ;
224
+ var name = userState . Data [ NAME_PARAM_NAME ] ;
225
+ var apikey = userState . Data [ API_KEY_PARAM_NAME ] ;
226
+ var isPublic = userState . Data [ IS_PUBLIC_PARAM_NAME ] ;
228
227
229
228
// If the name for the apikey is not defined, try to get it from the message.
230
229
if ( string . IsNullOrEmpty ( name ) )
@@ -239,7 +238,7 @@ await client.SendMessageAsync(
239
238
replyParameters : new ReplyParameters
240
239
{
241
240
MessageId = message . MessageId ,
242
- AllowSendingWithoutReply = true
241
+ AllowSendingWithoutReply = true ,
243
242
} ,
244
243
cancellationToken : cancellationToken
245
244
) ;
@@ -263,7 +262,7 @@ await client.SendMessageAsync(
263
262
replyParameters : new ReplyParameters
264
263
{
265
264
MessageId = message . MessageId ,
266
- AllowSendingWithoutReply = true
265
+ AllowSendingWithoutReply = true ,
267
266
} ,
268
267
cancellationToken : cancellationToken
269
268
) ;
@@ -298,7 +297,7 @@ await client.SendMessageAsync(
298
297
replyParameters : new ReplyParameters
299
298
{
300
299
MessageId = message . MessageId ,
301
- AllowSendingWithoutReply = true
300
+ AllowSendingWithoutReply = true ,
302
301
} ,
303
302
cancellationToken : cancellationToken
304
303
) ;
@@ -313,7 +312,7 @@ await client.SendMessageAsync(
313
312
replyParameters : new ReplyParameters
314
313
{
315
314
MessageId = message . MessageId ,
316
- AllowSendingWithoutReply = true
315
+ AllowSendingWithoutReply = true ,
317
316
} ,
318
317
cancellationToken : cancellationToken
319
318
) ;
@@ -336,8 +335,9 @@ await client.SendMessageAsync(
336
335
replyParameters : new ReplyParameters
337
336
{
338
337
MessageId = message . MessageId ,
339
- AllowSendingWithoutReply = true
338
+ AllowSendingWithoutReply = true ,
340
339
} ,
340
+ replyMarkup : new ReplyKeyboardRemove ( ) ,
341
341
cancellationToken : cancellationToken
342
342
) ;
343
343
// Remove the user state.
@@ -354,7 +354,7 @@ await client.SendMessageAsync(
354
354
replyParameters : new ReplyParameters
355
355
{
356
356
MessageId = message . MessageId ,
357
- AllowSendingWithoutReply = true
357
+ AllowSendingWithoutReply = true ,
358
358
} ,
359
359
cancellationToken : cancellationToken
360
360
) ;
@@ -365,6 +365,8 @@ await client.SendMessageAsync(
365
365
366
366
// Back up the apikey in the user state if it was not defined before.
367
367
userState . Data [ API_KEY_PARAM_NAME ] ??= apikey ;
368
+ // Back up the user state.
369
+ stateManager . CreateOrUpdateState ( userState ) ;
368
370
369
371
// If the user has not decided if share the apikey, check it their response or ask again.
370
372
if ( isPublic is null )
@@ -382,11 +384,11 @@ await client.SendMessageAsync(
382
384
replyParameters : new ReplyParameters
383
385
{
384
386
MessageId = message . MessageId ,
385
- AllowSendingWithoutReply = true
387
+ AllowSendingWithoutReply = true ,
386
388
} ,
387
389
replyMarkup : new ReplyKeyboardMarkup ( keyboard )
388
390
{
389
- ResizeKeyboard = true
391
+ ResizeKeyboard = true ,
390
392
} ,
391
393
cancellationToken : cancellationToken
392
394
) ;
@@ -395,8 +397,10 @@ await client.SendMessageAsync(
395
397
// Otherwise, try to parse the user response.
396
398
else
397
399
{
398
- isPublic = userResponse == this . YesLabel ;
399
- if ( isPublic == false && userResponse != this . NoLabel )
400
+ isPublic = ( userResponse == this . YesLabel )
401
+ . ToString ( )
402
+ . ToLowerInvariant ( ) ;
403
+ if ( isPublic == "false" && userResponse != this . NoLabel )
400
404
{
401
405
userResponse = null ;
402
406
goto noResponse ;
@@ -419,21 +423,21 @@ await client.SendMessageAsync(
419
423
replyParameters : new ReplyParameters
420
424
{
421
425
MessageId = message . MessageId ,
422
- AllowSendingWithoutReply = true
426
+ AllowSendingWithoutReply = true ,
423
427
} ,
424
428
replyMarkup : new ReplyKeyboardRemove ( ) ,
425
429
cancellationToken : cancellationToken
426
430
) ;
427
431
// Save the apikey in the database.
428
432
this . User . ApiKeys . Add (
429
- new SauceApiKey ( name , apikey ) { IsPublic = isPublic == true }
433
+ new SauceApiKey ( name , apikey ) { IsPublic = isPublic == " true" }
430
434
) ;
431
435
await userRepository . UpdateAsync ( this . User , cancellationToken ) ;
432
436
}
433
437
break ;
434
438
case DELETE_VALUE :
435
439
{
436
- var name = ( string ) userState . Data [ NAME_PARAM_NAME ] ! ;
440
+ var name = userState . Data [ NAME_PARAM_NAME ] ! ;
437
441
// If the user has not responded, ask.
438
442
if ( string . IsNullOrEmpty ( message . Text ) )
439
443
{
@@ -447,11 +451,11 @@ await client.SendMessageAsync(
447
451
replyParameters : new ReplyParameters
448
452
{
449
453
MessageId = message . MessageId ,
450
- AllowSendingWithoutReply = true
454
+ AllowSendingWithoutReply = true ,
451
455
} ,
452
456
replyMarkup : new ReplyKeyboardMarkup ( keyboard )
453
457
{
454
- ResizeKeyboard = true
458
+ ResizeKeyboard = true ,
455
459
} ,
456
460
cancellationToken : cancellationToken
457
461
) ;
@@ -473,7 +477,7 @@ await client.SendMessageAsync(
473
477
replyParameters : new ReplyParameters
474
478
{
475
479
MessageId = message . MessageId ,
476
- AllowSendingWithoutReply = true
480
+ AllowSendingWithoutReply = true ,
477
481
} ,
478
482
replyMarkup : new ReplyKeyboardRemove ( ) ,
479
483
cancellationToken : cancellationToken
@@ -492,7 +496,7 @@ await client.SendMessageAsync(
492
496
replyParameters : new ReplyParameters
493
497
{
494
498
MessageId = message . MessageId ,
495
- AllowSendingWithoutReply = true
499
+ AllowSendingWithoutReply = true ,
496
500
} ,
497
501
replyMarkup : new ReplyKeyboardRemove ( ) ,
498
502
cancellationToken : cancellationToken
0 commit comments