From 2720d96c576b148491a9041c890a8fe97870fb57 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Tue, 26 Dec 2017 18:15:30 +0100 Subject: [PATCH] Add "Add Credit" menu --- config/template_config.ini | 11 ++++++++-- core.py | 2 +- database.py | 6 ++++++ strings.py | 24 ++++++++++++++++++---- worker.py | 42 ++++++++++++++++++++++++++++++++++---- 5 files changed, 74 insertions(+), 11 deletions(-) diff --git a/config/template_config.ini b/config/template_config.ini index be187ea..6f260be 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 = 4 +version = 5 ; Set this to no when you are done editing the file is_template = yes @@ -20,4 +20,11 @@ long_polling_timeout = 30 # Database parameters [Database] ; The database engine you want to use. Refer to http://docs.sqlalchemy.org/en/latest/core/engines.html for the possible settings. -engine = sqlite:// \ No newline at end of file +engine = sqlite:// + +# Enabled payment methods +# To disable, leave the row empty +[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 diff --git a/core.py b/core.py index 26b86df..d092f50 100644 --- a/core.py +++ b/core.py @@ -94,7 +94,7 @@ def main(): # Ensure a worker exists for the chat and is alive if receiving_worker is None or not receiving_worker.is_alive(): # Suggest that the user restarts the chat with /start - bot.send_message(update.message.chat.id, strings.error_no_worker_for_chat, telegram.ReplyKeyboardRemove()) + bot.send_message(update.message.chat.id, strings.error_no_worker_for_chat, reply_markup=telegram.ReplyKeyboardRemove()) # Skip the update continue # Forward the update to the worker diff --git a/database.py b/database.py index 8c43211..661acbc 100644 --- a/database.py +++ b/database.py @@ -128,6 +128,12 @@ class Transaction(TableDeclarativeBase): return f"" +# TODO +# class Order(TableDeclarativeBase): +# """A product order.""" +# pass + + class Admin(TableDeclarativeBase): """A greed administrator with his permissions.""" diff --git a/strings.py b/strings.py index 11045bc..f566466 100644 --- a/strings.py +++ b/strings.py @@ -11,16 +11,19 @@ currency_format_string = "{symbol} {value}" # Quantity of a product in stock in_stock_format_string = "{quantity} disponibili" -# Answer: the start command was sent and the bot should welcome the user +# Conversation: the start command was sent and the bot should welcome the user conversation_after_start = "Ciao!\n" \ "Benvenuto su greed!" -# Answer: to send an inline keyboard you need to send a message with it +# Conversation: to send an inline keyboard you need to send a message with it conversation_open_user_menu = "Allora, {username}, cosa vorresti fare?" -# Answer: the same message as above but when the first has already been sent +# Conversation: the same message as above but when the first has already been sent conversation_open_user_menu_multiple = "Hai bisogno di qualcos'altro?" +# Conversation: select a payment method +conversation_payment_method = "Come vuoi aggiungere fondi al tuo portafoglio?" + # Notification: the conversation has expired conversation_expired = "๐Ÿ• Il bot non ha ricevuto messaggi per un po' di tempo, quindi ha chiuso la conversazione.\n" \ "Per riavviarne una nuova, invia il comando /start." @@ -33,11 +36,24 @@ menu_order = "๐Ÿ› Ordina" menu_order_status = "โ“ Stato ordini" # User menu: add credit -menu_add_credit = "๐Ÿ’ต Ricarica" +menu_add_credit = "๐Ÿ’ต Aggiungi fondi" # User menu: bot info menu_bot_info = "โ„น๏ธ Informazioni sul bot" +# User menu: cash +menu_cash = "๐Ÿ’ต In contanti" + +# User menu: credit card +menu_credit_card = "๐Ÿ’ณ Con una carta di credito" + +# User menu: cancel +menu_cancel = "๐Ÿ”™ Annulla" + +# Payment: cash payment info +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." + # Info: informazioni sul bot bot_info = 'Questo bot utilizza greed,' \ ' un framework di @Steffo per i pagamenti su Telegram rilasciato sotto la' \ diff --git a/worker.py b/worker.py index eb551ef..4dace82 100644 --- a/worker.py +++ b/worker.py @@ -106,7 +106,6 @@ class ChatWorker(threading.Thread): self.bot.send_message(self.chat.id, strings.conversation_open_user_menu.format(username=str(self.user)), reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)) # Wait for a reply from the user - # TODO: change this selection = self.__wait_for_specific_message([strings.menu_order, strings.menu_order_status, strings.menu_add_credit, strings.menu_bot_info]) # If the user has selected the Order option... @@ -133,6 +132,41 @@ class ChatWorker(threading.Thread): raise NotImplementedError() def __add_credit_menu(self): + """Add more credit to the account.""" + # TODO: a loop might be needed here + # Create a payment methods keyboard + keyboard = list() + # Add the supported payment methods to the keyboard + # Cash + keyboard.append([telegram.KeyboardButton(strings.menu_cash)]) + # Telegram Payments + if configloader.config["Payment Methods"]["credit_card_token"] != "": + keyboard.append([telegram.KeyboardButton(strings.menu_credit_card)]) + # Keyboard: go back to the previous menu + keyboard.append([telegram.KeyboardButton(strings.menu_cancel)]) + # Send the keyboard to the user + self.bot.send_message(self.chat.id, strings.conversation_payment_method, + reply_markup=telegram.ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)) + # Wait for a reply from the user + selection = self.__wait_for_specific_message([strings.menu_cash, strings.menu_credit_card, strings.menu_cancel]) + # If the user has selected the Cash option... + if selection == strings.menu_cash: + # Go to the pay with cash function + self.__add_credit_cash() + # If the user has selected the Credit Card option... + elif selection == strings.menu_credit_card: + # Go to the pay with credit card function + self.__add_credit_cc() + # If the user has selected the Cancel option... + elif selection == strings.menu_add_credit: + # Send him back to the previous menu + return + + def __add_credit_cash(self): + """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() def __bot_info(self): @@ -142,12 +176,12 @@ class ChatWorker(threading.Thread): def __admin_menu(self): """Function called from the run method when the user is an administrator. Administrative bot actions should be placed here.""" - self.bot.send_message(self.chat.id, "Sei un Amministralol") + raise NotImplementedError() def __graceful_stop(self): """Handle the graceful stop of the thread.""" - # Notify the user that the session has expired - self.bot.send_message(self.chat.id, strings.conversation_expired) + # Notify the user that the session has expired and remove the keyboard + self.bot.send_message(self.chat.id, strings.conversation_expired, reply_markup=telegram.ReplyKeyboardRemove()) # Close the database session # End the process sys.exit(0)