diff --git a/config/template_config.ini b/config/template_config.ini index 6f260be..57bac43 100644 --- a/config/template_config.ini +++ b/config/template_config.ini @@ -3,7 +3,7 @@ # Config file parameters [Config] ; Config file version. DO NOT EDIT THIS! -version = 5 +version = 6 ; Set this to no when you are done editing the file is_template = yes @@ -27,4 +27,10 @@ engine = sqlite:// [Payment Methods] # Cash payment is always enabled # Credit card: get the token at @BotFather -credit_card_token = 123456789:YOUR_TOKEN_HERE_ \ No newline at end of file +credit_card_token = 123456789:YOUR_TOKEN_HERE_ + +[Payments] +# Minimum wallet payment accepted +min_amount = 10 +# Maximum wallet payment accepted +max_amount = 100 \ No newline at end of file diff --git a/strings.py b/strings.py index f566466..619b973 100644 --- a/strings.py +++ b/strings.py @@ -54,6 +54,9 @@ menu_cancel = "🔙 Annulla" payment_cash = "Puoi pagare in contanti alla sede fisica del negozio.\n" \ "Il gestore provvederà ad aggiungere credito al tuo account appena gli avrai consegnato i soldi." +# Payment: invoice amount +payment_cc_amount = "Quanti fondi vuoi aggiungere al tuo portafoglio?" + # Info: informazioni sul bot bot_info = 'Questo bot utilizza greed,' \ ' un framework di @Steffo per i pagamenti su Telegram rilasciato sotto la' \ @@ -66,3 +69,9 @@ error_nonprivate_chat = "⚠️ Questo bot funziona solo in chat private." # Error: a message was sent in a chat, but no worker exists for that chat. Suggest the creation of a new worker with /start error_no_worker_for_chat = "⚠️ La conversazione con il bot è interrotta.\n" \ "Per riavviarla, manda il comando /start al bot." + +# Error: add funds amount over max +error_payment_amount_over_max = "⚠️ Il massimo di fondi che possono essere aggiunti in una singola transazione è {max_amount}." + +# Error: add funds amount under min +error_payment_amount_under_min = "⚠️ Il minimo di fondi che possono essere aggiunti in una singola transazione è {min_amount}." \ No newline at end of file diff --git a/worker.py b/worker.py index 4dace82..9024c22 100644 --- a/worker.py +++ b/worker.py @@ -6,6 +6,8 @@ import configloader import sys import queue as queuem import database as db +import re +from decimal import Decimal class StopSignal: """A data class that should be sent to the worker when the conversation has to be stopped abnormally.""" @@ -92,6 +94,25 @@ class ChatWorker(threading.Thread): # Return the message text return update.message.text + def __wait_for_regex(self, regex:str) -> str: + """Continue getting updates until the regex finds a match in a message, then return the first capture group.""" + while True: + # Get the next update + update = self.__receive_next_update() + # Ensure the update contains a message + if update.message is None: + continue + # Ensure the message contains text + if update.message.text is None: + continue + # Try to match the regex with the received message + match = re.search(regex, update.message.text) + # Ensure there is a match + if match is None: + continue + # Return the first capture group + return match.group(1) + def __user_menu(self): """Function called from the run method when the user is not an administrator. Normal bot actions should be placed here.""" @@ -163,11 +184,33 @@ class ChatWorker(threading.Thread): return def __add_credit_cash(self): - """Tell the user how to pay with cash at this shop""" + """Tell the user how to pay with cash at this shop.""" self.bot.send_message(self.chat.id, strings.payment_cash) def __add_credit_cc(self): - raise NotImplementedError() + """Ask the user how much money he wants to add to his wallet.""" + # Loop used to continue asking if there's an error during the input + while True: + # Create a keyboard to be sent later + keyboard = [[telegram.KeyboardButton(strings.currency_format_string.format(symbol=strings.currency_symbol, value="10"))], + [telegram.KeyboardButton(strings.currency_format_string.format(symbol=strings.currency_symbol, value="25"))], + [telegram.KeyboardButton(strings.currency_format_string.format(symbol=strings.currency_symbol, value="50"))], + [telegram.KeyboardButton(strings.currency_format_string.format(symbol=strings.currency_symbol, value="100"))]] + # Send the message and the keyboard + self.bot.send_message(self.chat.id, strings.payment_cc_amount, + reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)) + # Wait until a valid amount is sent + # TODO: check and debug the regex + selection = Decimal(self.__wait_for_regex(r"([0-9]{1,3}(?:[.,][0-9]{1,2})?)").replace(",", ".")) + # Ensure the amount is within the range + if selection > Decimal(configloader.config["Payments"]["max_amount"]): + self.bot.send_message(self.chat.id, strings.error_payment_amount_over_max.format(max_amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=configloader.config["Payments"]["max_amount"]))) + continue + elif selection < Decimal(configloader.config["Payments"]["min_amount"]): + self.bot.send_message(self.chat.id, strings.error_payment_amount_under_min.format(min_amount=strings.currency_format_string.format(symbol=strings.currency_symbol, value=configloader.config["Payments"]["min_amount"]))) + continue + # The amount is valid, send the invoice + print(selection) def __bot_info(self): """Send information about the bot."""