|
| 1 | +import logging |
| 2 | + |
| 3 | +from mongoengine.errors import DoesNotExist |
| 4 | +from telegram import Update |
| 5 | +from telegram.ext import CommandHandler, CallbackContext |
| 6 | + |
| 7 | +from bot.core import BotTelegramCore |
| 8 | +from bot.messages import GROUP_RESTRICTED, TX_ID_ERROR |
| 9 | +from db.observer import UserObserver |
| 10 | +from db.ticket import Ticket |
| 11 | + |
| 12 | + |
| 13 | +logging.basicConfig( |
| 14 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 15 | + level=logging.INFO) |
| 16 | + |
| 17 | +logger = logging.getLogger(__name__) |
| 18 | + |
| 19 | + |
| 20 | +def add_ticket(update: Update, context: CallbackContext): |
| 21 | + chat = update.effective_chat |
| 22 | + message = update.effective_message |
| 23 | + |
| 24 | + if chat.type != "private": |
| 25 | + message.reply_text(GROUP_RESTRICTED) |
| 26 | + return |
| 27 | + |
| 28 | + try: |
| 29 | + observer = UserObserver.objects.get(chat_id=f"{chat.id}") |
| 30 | + except DoesNotExist: |
| 31 | + observer = UserObserver(f"{chat.username}", |
| 32 | + f"{chat.id}").save() |
| 33 | + |
| 34 | + try: |
| 35 | + tx_id = context.args[0] |
| 36 | + except IndexError: |
| 37 | + message.reply_text(TX_ID_ERROR) |
| 38 | + return |
| 39 | + |
| 40 | + try: |
| 41 | + Ticket.objects.get(observer=observer, tx_id=tx_id) |
| 42 | + message.reply_text(f"Ticket with transaction id {tx_id}" |
| 43 | + f" is already registered!", parse_mode='HTML') |
| 44 | + return |
| 45 | + except DoesNotExist: |
| 46 | + ticket = Ticket(observer, tx_id) |
| 47 | + |
| 48 | + if ticket.fetch(): |
| 49 | + message.reply_text(f"Ticket has been saved!" |
| 50 | + f"\n\n{ticket.html}", parse_mode='HTML') |
| 51 | + |
| 52 | + |
| 53 | +def remove_ticket(update: Update, context: CallbackContext): |
| 54 | + chat = update.effective_chat |
| 55 | + message = update.effective_message |
| 56 | + |
| 57 | + if chat.type != "private": |
| 58 | + message.reply_text(GROUP_RESTRICTED) |
| 59 | + return |
| 60 | + |
| 61 | + try: |
| 62 | + observer = UserObserver.objects.get(chat_id=f"{chat.id}") |
| 63 | + except DoesNotExist: |
| 64 | + observer = UserObserver(f"{chat.username}", |
| 65 | + f"{chat.id}").save() |
| 66 | + |
| 67 | + try: |
| 68 | + tx_id = context.args[0] |
| 69 | + except IndexError: |
| 70 | + message.reply_text(TX_ID_ERROR) |
| 71 | + return |
| 72 | + |
| 73 | + try: |
| 74 | + ticket = Ticket.objects.get(observer=observer, tx_id=tx_id) |
| 75 | + except DoesNotExist: |
| 76 | + message.reply_text(f"Ticket with transaction id {tx_id} doesn't exist!") |
| 77 | + return |
| 78 | + |
| 79 | + ticket.delete() |
| 80 | + message.reply_text(f"Ticket {tx_id} has been removed!") |
| 81 | + |
| 82 | + |
| 83 | +def list_tickets(update: Update, context: CallbackContext): |
| 84 | + chat = update.effective_chat |
| 85 | + message = update.effective_message |
| 86 | + |
| 87 | + if chat.type != "private": |
| 88 | + message.reply_text(GROUP_RESTRICTED) |
| 89 | + return |
| 90 | + |
| 91 | + try: |
| 92 | + observer = UserObserver.objects.get(chat_id=f"{chat.id}") |
| 93 | + except DoesNotExist: |
| 94 | + observer = UserObserver(f"{chat.username}", |
| 95 | + f"{chat.id}").save() |
| 96 | + |
| 97 | + tickets = Ticket.objects(observer=observer) |
| 98 | + |
| 99 | + if not tickets: |
| 100 | + message.reply_text("<b>There are no watched tickets!" |
| 101 | + "</b>", parse_mode='HTML') |
| 102 | + return |
| 103 | + |
| 104 | + text = "<b>Watched tickets</b>\n\n" |
| 105 | + for index, ticket in enumerate(tickets): |
| 106 | + text += ticket.html |
| 107 | + text += '\n' if index != len(tickets) else '' |
| 108 | + |
| 109 | + message.reply_text(text, parse_mode='HTML') |
| 110 | + |
| 111 | + |
| 112 | +def config_handlers(instance: BotTelegramCore): |
| 113 | + logger.info("Setting ticket commands...") |
| 114 | + |
| 115 | + instance.add_handler(CommandHandler("add_ticket", add_ticket)) |
| 116 | + instance.add_handler(CommandHandler("remove_ticket", remove_ticket)) |
| 117 | + instance.add_handler(CommandHandler("list_tickets", list_tickets)) |
0 commit comments