Repository navigation
Expand file tree
/
Copy pathTextCommands.py
More file actions
803 lines (614 loc) · 31.9 KB
/
Copy pathTextCommands.py
File metadata and controls
803 lines (614 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
import os
import pickle
import re
from typing import Dict, Union
from zebra import Zebra
from telegram import Update, Message
from telegram.ext import ContextTypes
from constants import *
import responses
import ConfigHandler
from user import User
async def super_user_command(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, users: Dict[int, User],
printer: Zebra, slap_detector, printer_cf, users_cf, state_cf,
config: Dict, sticker_history: set) -> bool:
""" Takes a command sent by a user, determines if it's a superuser.
If it is a superuser, check whether the messages match a command. If so, run that command.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user
:param users: A dict containing the users, found by user ID.
:param printer: The Zebra printer object.
:param slap_detector: SlapDetection class object or None whether the hardware is available.
:param printer_cf: Dict of configuration settings relating to the printer.
:param users_cf: Dict of configuration settings relating to users.
:param state_cf: Dict of configuration settings relating to the state of the script.
:param config: The Dict containing the rest of the _cf configuration dictionaries.
:param sticker_history: A set of Telegram Sticker or PhotoSize objects.
:returns: A bool indicating whether the message matches a command.
"""
# First check set_queue, since it is case-sensitive
# Sets the print queue to your specification
if await set_queue(update, context, command, printer, printer_cf):
return True
command = command.lower()
# ==== Reply Commands ==== #
if update.message.reply_to_message is not None:
reply_message = update.message.reply_to_message
# This one does not rely on having an associated user.
if await remove_from_history(update, context, command, reply_message, sticker_history):
return True
# Get the sender ID
if reply_message.forward_origin != "hidden_user" and reply_message.forward_origin is not None:
current_user = users.get(reply_message.forward_origin.sender_user.id)
elif (user_id := get_user_from_text(reply_message.text_html)) is not None: # If the reply is to an HTML user mention (Used in the /random feature)
current_user = users.get(user_id)
else: # if the user has their chat hidden
for key, value in users.items():
if value.check_log(reply_message.id):
current_user = users.get(value.user_id)
break
else:
current_user = None
if current_user is None:
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.CANT_FIND_USER_ERROR)
return True
# Check for Adding or subtracting sticker limit command (Ex: +5 or -2 or =999 to change limit)
if await user_limit_adjust(update, context, command, current_user):
return True
# Check for ban or reset command
if await ban_reset(update, context, command, current_user):
return True
# ==== General Commands ==== #
# List commands
if await list_commands(update, context, command):
return True
# Resets all users to the default
if await reset_all(update, context, command, users):
return True
# Wipes all users
if await wipe(update, context, command, users):
return True
# Clears sticker history for the /random feature
if await clear_history(update, context, command, sticker_history):
return True
# Set all limits to X
# Command has 3 parts: The command, a space, and a digit
if await set_limit(update, context, command, users, users_cf):
return True
# Get sticker limit
if await get_limit(update, context, command, users_cf):
return True
# Turn bot on or off
if await toggle_bot(update, context, command, printer, state_cf, printer_cf):
return True
if await toggle_monitoring(update, context, command, state_cf):
return True
if await toggle_random_printing(update, context, command, state_cf):
return True
# Display print offset and list commands
if await get_print_offset(update, context, command, printer_cf):
return True
# Set the x (Horizontal) print offset
if await set_print_offset(update, context, command, printer_cf):
return True
# Display the current queue and checks if it exists on the system
if await check_queue(update, context, command, printer, printer_cf):
return True
# Lists the print queues on the device
if await list_queues(update, context, command, printer):
return True
# Enables/disables the random event
if await toggle_random_event(update, context, command, state_cf):
return True
# Enables/disables slap detection
if await toggle_slap_detection(update, context, command, slap_detector, state_cf):
return True
# Saves config
if await save_config(update, context, command, config):
return True
return False
async def no_super_users(update: Update, context: ContextTypes.DEFAULT_TYPE,
setup_cf: Dict[str, Union[str, list, bool]]) -> bool:
""" Checks whether there is a superuser. If not, tells the user what their user_id is.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param setup_cf: Dict of api token and superuser IDs.
:returns: A bool indicating whether the message triggers this function.
"""
if len(setup_cf['super_user_id']) == 0 and update.message.text.lower() == COMMANDS:
text = responses.SUPERUSER_NOT_SET.format(id=update.message.from_user.id)
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
# ========================= #
# ======= Functions ======= #
# ========================= #
def get_command_int(command: str, user_response: str) -> Union[int, None]:
""" Gets the number part of a command and check if it's in a valid format.
Used with commands that require a command and a number.
Ex: 'setlimit 10' returns 10
Ex: '+5' returns 5
Ex: 'setlimit 10 asdfs' returns None
:param command: The text part of the command from the module
:param user_response: The full message from the user
:returns: An int representing the amount if it was successful. None if the format was incorrect.
"""
modifier = False
if len(command) == len(user_response):
return None
# Check if there is a + or - before the number
if user_response[len(command) + 1] == "+" or user_response[len(command) + 1] == "-":
modifier = True
# If there is a space between
if not user_response[len(command)] == " ":
return None
# if command ends with a number
if not user_response[len(command) + modifier + 1:].isdigit(): # Modifier adds +1 if true
return None
# At this point the command is valid
number = int(user_response[len(command) + modifier + 1:])
# If there is a negative sign before the number
if modifier and user_response[len(command) + 1] == "-":
number *= -1
return number
async def user_limit_adjust(update: Update, context: ContextTypes.DEFAULT_TYPE,
command: str, current_user: User) -> bool:
""" Reply command. Adjusts a specific user's sticker limit
Ex: When replying to a monitored sticker use '+5' or '-2' or '=999' to change limit
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param current_user: A User object containing the user the operation is being performed on.
:returns: A bool indicating whether the message triggers this function.
"""
# Add or subtract sticker limit (Ex: +5 or -2 or =999 to change limit)
if not command[1:].isdigit():
return False
if command.startswith("+"):
current_user.sticker_max += int(command[1:])
text = responses.ADD_USER_LIMIT.format(amount=int(command[1:]),
total=current_user.sticker_max)
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
if command.startswith("-"):
current_user.sticker_max -= int(command[1:])
text = responses.SUBTRACT_USER_LIMIT.format(amount=int(command[1:]),
total=current_user.sticker_max)
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
if command.startswith("="):
current_user.sticker_max = int(command[1:])
text = responses.SET_USER_LIMIT.format(total=current_user.sticker_max)
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def ban_reset(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, current_user: User) -> bool:
""" Reply command. Ban (set user's sticker limit to 0) and reset (set user's sticker count to 0) commands.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param current_user: A User object containing the user the operation is being performed on.
:returns: A bool indicating whether the message triggers this function.
"""
# ban
if command == BAN:
current_user.sticker_max = 0
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.USER_BANNED)
return True
# reset
if command == RESET:
current_user.sticker_count = 0
text = responses.USER_LIMIT_RESET.format(amount=str(current_user.sticker_max))
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def remove_from_history(update: Update, context: ContextTypes.DEFAULT_TYPE,
command: str, reply_message: Message, sticker_history: set) -> bool:
""" Reply command. Remove the selected sticker from the pool of stickers the /random feature chooses from to print.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param reply_message: The message that the superuser is replying to.
:param sticker_history: A set of Telegram Sticker or PhotoSize objects.
:returns: A bool indicating whether the message triggers this function.
"""
if command == REMOVE:
if reply_message.sticker is not None:
sticker_history.remove(reply_message.sticker)
elif len(reply_message.photo) != 0 :
sticker_history.remove(reply_message.photo[-1]) # TODO: Either check all sizes or find the right size
else:
text = responses.CANT_REMOVE
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
text = responses.STICKER_REMOVED_FROM_HISTORY
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
pickle.dump(sticker_history, open("sticker_history.p", "wb"))
return True
return False
async def list_commands(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str):
""" Displays the list of commands
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:returns: A bool indicating whether the message triggers this function.
"""
if command == COMMANDS:
text = responses.COMMANDS
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def reset_all(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, users: Dict[int, User]) -> bool:
""" Displays the list of commands
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param users: A dict containing the users, found by user ID.
:returns: A bool indicating whether the message triggers this function.
"""
if command == RESET_ALL_COUNT:
for key, value in users.items():
value.sticker_count = 0
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.COUNTS_RESET)
return True
return False
async def wipe(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, users: Dict[int, User]) -> bool:
""" Wipes the data on all users, starts over fresh.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param users: A dict containing the users, found by user ID.
:returns: A bool indicating whether the message triggers this function.
"""
if command != WIPE:
return False
users.clear()
try:
os.remove("limit_tracker.p")
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.USERS_WIPED)
except FileNotFoundError:
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.NO_FILE)
except OSError:
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.CANT_DELETE_FILE)
return True
async def clear_history(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, sticker_history: set) -> bool:
""" Clears sticker history for the /random feature
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param sticker_history: A set of Telegram Sticker or PhotoSize objects.
:returns: A bool indicating whether the message triggers this function.
"""
if command != CLEAR_HISTORY:
return False
sticker_history.clear()
pickle.dump(sticker_history, open("sticker_history.p", "wb"))
await context.bot.send_message(chat_id=update.effective_chat.id, text=responses.STICKER_HISTORY_CLEARED)
return True
async def set_limit(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, users: Dict[int, User],
users_cf: Dict[str, Union[int, bool]]) -> bool:
""" Sets all user's sticker limits to X
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param users: A dict containing the users, found by user ID.
:param users_cf: Dict of configuration settings relating to users.
:returns: A bool indicating whether the message triggers this function.
"""
# Command has 3 parts: The command, a space, and a digit
if not command.startswith(SET_ALL_LIMIT): # Command starts with the limit command
return False
# Get the number from the command
command_int = get_command_int(SET_ALL_LIMIT, command)
if command_int is None:
return False
# Change the limit on each user
for key, value in users.items():
value.sticker_max = command_int
users_cf['sticker_limit'] = command_int
text = responses.SET_NEW_LIMIT.format(amount=str(command_int))
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def get_limit(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
users_cf: Dict[str, Union[int, bool]]) -> bool:
""" Gets the current sticker limit
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param users_cf: Dict of configuration settings relating to users.
:returns: A bool indicating whether the message triggers this function.
"""
if command != GET_LIMIT:
return False
text = responses.GET_LIMIT.format(amount=str(users_cf['sticker_limit']))
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def toggle_bot(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, printer: Zebra,
state_cf: Dict[str, bool], printer_cf: Dict[str, Union[str, int]]) -> bool:
""" Turns the bot on or off. Superuser commands can still be sent, but the bot will not respond anything else.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer: The Zebra printer object.
:param state_cf: Dict of configuration settings relating to the state of the script.
:param printer_cf: Dict of configuration settings relating to the printer.
:returns: A bool indicating whether the message triggers this function.
"""
# Turn off bot
if command == BOT_DISABLE:
if state_cf['bot_enabled']:
text = responses.BOT_DISABLED
else:
text = responses.BOT_ALREADY_DISABLED
state_cf['bot_enabled'] = False
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
# Turn on bot
if command == BOT_ENABLE:
if state_cf['bot_enabled']:
text = responses.BOT_ALREADY_ENABLED
else:
# Checks to make sure there is a valid print queue before enabling
if printer_cf['printer_queue'].lower() in (queue.lower() for queue in printer.getqueues()):
state_cf['bot_enabled'] = True
text = responses.BOT_ENABLED
else:
text = responses.BOT_ENABLE_ERROR_NO_QUEUE
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def toggle_monitoring(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
state_cf: Dict[str, bool]) -> bool:
""" Turns sticker monitoring on or off. Determines whether stickers that are printed are forwarded to superusers
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param state_cf: Dict of configuration settings relating to the state of the script.
:returns: A bool indicating whether the message triggers this function.
"""
# Turn off sticker monitoring
if command == STICKER_MONITORING_OFF:
if state_cf['sticker_monitoring']:
text = responses.MONITORING_DISABLED
else:
text = responses.MONITORING_ALREADY_DISABLED
state_cf['sticker_monitoring'] = False
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
# Turn on sticker monitoring
if command == STICKER_MONITORING_ON:
if state_cf['sticker_monitoring']:
text = responses.MONITORING_ALREADY_ENABLED
else:
text = responses.MONITORING_ENABLED
state_cf['sticker_monitoring'] = True
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def toggle_random_printing(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
state_cf: Dict[str, bool]) -> bool:
""" Turns random sticker printing feature on or off. The user command that allows the printer to print a random sticker
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param state_cf: Dict of configuration settings relating to the state of the script.
:returns: A bool indicating whether the message triggers this function.
"""
if command == RANDOM_OFF:
if state_cf['random_sticker_printing']:
text = responses.RANDOM_DISABLED
else:
text = responses.RANDOM_ALREADY_DISABLED
state_cf['random_sticker_printing'] = False
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
if command == RANDOM_ON:
if state_cf['random_sticker_printing']:
text = responses.RANDOM_ALREADY_ENABLED
else:
text = responses.RANDOM_ENABLED
state_cf['random_sticker_printing'] = True
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def get_print_offset(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
printer_cf: Dict[str, Union[str, int]]) -> bool:
""" Displays print offset and lists commands
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer_cf: Dict of configuration settings relating to the printer.
:returns: A bool indicating whether the message triggers this function.
"""
if command == PRINT_OFFSET:
text = responses.PRINT_OFFSET.format(offset_x=printer_cf["image_offset_x"],
offset_y=printer_cf["image_offset_y"])
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def set_print_offset(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
printer_cf: Dict[str, Union[str, int]]) -> bool:
""" Sets a new x or y print offset
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer_cf: Dict of configuration settings relating to the printer.
:returns: A bool indicating whether the message triggers this function.
"""
# Set the x (Horizontal) print offset
if command.startswith(PRINT_OFFSET_X):
command_int = get_command_int(PRINT_OFFSET_X, command)
if command_int is not None:
printer_cf["image_offset_x"] = command_int
text = responses.SET_PRINT_OFFSET_X.format(offset_x=printer_cf["image_offset_x"])
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
# Set the y (vertical) print offset
if command.startswith(PRINT_OFFSET_Y):
command_int = get_command_int(PRINT_OFFSET_Y, command)
if command_int is not None:
printer_cf["image_offset_y"] = command_int
text = responses.SET_PRINT_OFFSET_Y.format(offset_y=printer_cf["image_offset_y"])
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def check_queue(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
printer: Zebra, printer_cf: Dict[str, Union[str, int]]) -> bool:
""" Display the current queue and checks if it exists on the system
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer: The Zebra printer object.
:param printer_cf: Dict of configuration settings relating to the printer.
:returns: A bool indicating whether the message triggers this function.
"""
if command != CHECK_QUEUE:
return False
if printer_cf['printer_queue'] == '':
text = responses.CHECK_QUEUE_NO_QUEUE
elif printer_cf['printer_queue'].lower() in (queue.lower() for queue in printer.getqueues()):
text = responses.CHECK_QUEUE_SUCCESS.format(print_queue=printer_cf['printer_queue'])
else:
text = responses.CHECK_QUEUE_FAIL
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def list_queues(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, printer: Zebra) -> bool:
""" Lists the print queues on the device
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer: The Zebra printer object.
:returns: A bool indicating whether the message triggers this function.
"""
if command != LIST_QUEUES:
return False
if len(printer.getqueues()) > 0:
text = responses.LIST_QUEUES + "\n"
for i in printer.getqueues():
text += i + "\n"
else:
text = responses.LIST_QUEUES_NO_QUEUES
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def set_queue(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, printer: Zebra,
printer_cf: Dict[str, Union[str, int]]) -> bool:
""" Changes the printer queue to the one specified
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param printer: The Zebra printer object.
:param printer_cf: Dict of configuration settings relating to the printer.
:returns: A bool indicating whether the message triggers this function.
"""
if not command.lower().startswith(SET_QUEUE):
return False
# If the space is in the correct position and the name of the printer is longer than 1 character
if command[len(SET_QUEUE)] == " " and len(command[len(SET_QUEUE) + 1:]) > 1:
# If the given printer queue name is in the list of the computer's queues
if command[len(SET_QUEUE) + 1:] in printer.getqueues():
printer_cf['printer_queue'] = command[len(SET_QUEUE) + 1:]
text = responses.SET_QUEUE_SUCCESS
else:
text = responses.SET_QUEUE_BAD_QUEUE
else:
text = responses.SET_QUEUE_SYNTAX_ERROR
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def toggle_random_event(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
state_cf: Dict[str, bool]) -> bool:
""" Enables/disables random events
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param state_cf: Dict of configuration settings relating to the state of the script.
:returns: A bool indicating whether the message triggers this function.
"""
# Turn on random events
if command == EVENT_ON:
if state_cf['event']:
text = responses.EVENT_ALREADY_ENABLED
else:
text = responses.EVENT_ENABLED
state_cf['event'] = True
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
# Turn off random events
if command == EVENT_OFF:
if state_cf['event']:
text = responses.EVENT_DISABLED
state_cf['event'] = False
else:
text = responses.EVENT_ALREADY_DISABLED
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def toggle_slap_detection(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str,
slap_detector, state_cf: Dict[str, bool]) -> bool:
""" Enables/disables slap detection
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param slap_detector: A SlapDetection object.
:param state_cf: Dict of configuration settings relating to the state of the script.
:return: A bool indicating whether the message triggers this function.
"""
# Turn on slap detection
if command == SLAP_ON:
if not state_cf['slap_hardware']:
text = responses.SLAP_HARDWARE_MISSING
elif state_cf['slap_detection']:
text = responses.SLAP_ALREADY_ENABLED
else:
text = responses.SLAP_ENABLED
slap_detector.change_state(True)
state_cf['slap_detection'] = True
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
# Turn off slap detection
if command == SLAP_OFF:
if not state_cf['slap_hardware']:
text = responses.SLAP_HARDWARE_MISSING
elif state_cf['slap_detection']:
text = responses.SLAP_DISABLED
slap_detector.change_state(False)
state_cf['slap_detection'] = False
else:
text = responses.SLAP_ALREADY_DISABLED
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
return False
async def save_config(update: Update, context: ContextTypes.DEFAULT_TYPE, command: str, config: Dict) -> bool:
""" Enables/disables random events
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
:param command: The text command sent by the user.
:param config: The Dict containing the rest of the _cf configuration dictionaries.
:returns: A bool indicating whether the message triggers this function.
"""
if command != SAVE:
return False
ConfigHandler.save_config(config)
text = responses.CONFIG_SAVED
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return True
async def command_not_recognized(update: Update, context: ContextTypes.DEFAULT_TYPE):
""" Displays a command not recognized error. Triggers when a user's message doesn't match any other commands.
:param update: Update object containing the sent message.
:param context: Object containing the bot interface for the current chat.
"""
text = responses.COMMAND_NOT_RECOGNIZED
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
return
def get_user_from_text(message_text: str) -> Union[int, None]:
""" Gets the user_id from an HTML text mention.
Used to get the user ID of a user utilizing the /random function since it can't forward it from the user.
:param message_text: Message text to parse.
:returns: User ID as int or None
"""
if message_text is None:
return None
match = re.search(r'id=(\d+)', message_text)
if match:
return int(match.group(1))
return None