From 248d750c97c94ef043a30e34dfe640a704ad142d Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 7 Nov 2020 12:16:52 +0100 Subject: [PATCH] Setup reasons selection --- callbackQuery/create.py | 9 +++++++++ callbackQuery/edit.py | 2 +- callbackQuery/main.py | 6 ++++++ callbackQuery/new.py | 30 +++++++++++++++++++++++++++--- callbackQuery/reason.py | 16 ++++++++++++++++ main.py | 1 + 6 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 callbackQuery/create.py create mode 100644 callbackQuery/reason.py diff --git a/callbackQuery/create.py b/callbackQuery/create.py new file mode 100644 index 0000000..0471f2d --- /dev/null +++ b/callbackQuery/create.py @@ -0,0 +1,9 @@ +from telegram import Update +from telegram.ext import CallbackContext + +from main import reasons + + +def create(update: Update, context: CallbackContext): + del reasons[update.effective_chat.id][update["_effective_user"]["id"]] + diff --git a/callbackQuery/edit.py b/callbackQuery/edit.py index f7da337..3f2edb6 100644 --- a/callbackQuery/edit.py +++ b/callbackQuery/edit.py @@ -11,5 +11,5 @@ def edit(update: Update, context: CallbackContext): if update.effective_chat.id not in messages: messages[update.effective_chat.id] = {} - messages[update.effective_chat.id] = {update["_effective_user"]["id"]: update.callback_query.data} + messages[update.effective_chat.id][update["_effective_user"]["id"]] = update.callback_query.data diff --git a/callbackQuery/main.py b/callbackQuery/main.py index fc3dd17..098ac05 100644 --- a/callbackQuery/main.py +++ b/callbackQuery/main.py @@ -1,9 +1,11 @@ from telegram import Update from telegram.ext import CallbackContext, CallbackQueryHandler +from callbackQuery.create import create from callbackQuery.data import data from callbackQuery.edit import edit from callbackQuery.new import new +from callbackQuery.reason import reason from commands.start import start @@ -14,8 +16,12 @@ def callback_query(update: Update, context: CallbackContext): new(update, context) elif update.callback_query.data == "data": data(update, context) + elif update.callback_query.data == "create": + create(update, context) elif update.callback_query.data.startswith("edit_"): edit(update, context) + elif update.callback_query.data.startswith("reason_"): + reason(update, context) callback_query_handler = CallbackQueryHandler(callback_query) diff --git a/callbackQuery/new.py b/callbackQuery/new.py index 57c2315..345f76e 100644 --- a/callbackQuery/new.py +++ b/callbackQuery/new.py @@ -1,7 +1,7 @@ -from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, ParseMode from telegram.ext import CallbackContext -from main import database +from main import database, reasons def new(update: Update, context: CallbackContext): @@ -12,5 +12,29 @@ def new(update: Update, context: CallbackContext): [InlineKeyboardButton("Home", callback_data="home")] ])) else: - context.bot.send_message(chat_id=update.effective_chat.id, text="Select your reasons") + if update.effective_chat.id not in reasons: + reasons[update.effective_chat.id] = {} + if update["_effective_user"]["id"] not in reasons[update.effective_chat.id]: + reasons[update.effective_chat.id][update["_effective_user"]["id"]] = [] + + last_line = [InlineKeyboardButton("Home", callback_data="home")] + if len(reasons[update.effective_chat.id][update["_effective_user"]["id"]]) != 0: + last_line.append(InlineKeyboardButton("Send", callback_data="create")) + + context.bot.send_message(chat_id=update.effective_chat.id, + text=f"Select your reasons\n`" + + "`, `".join(reasons[update.effective_chat.id][update["_effective_user"]["id"]]) + + "`", + parse_mode=ParseMode.MARKDOWN_V2, + reply_markup=InlineKeyboardMarkup([ + [InlineKeyboardButton("Work", callback_data="reason_work"), + InlineKeyboardButton("Shopping", callback_data="reason_shopping")], + [InlineKeyboardButton("Health", callback_data="reason_health"), + InlineKeyboardButton("Family", callback_data="reason_family")], + [InlineKeyboardButton("Handicap", callback_data="reason_handicap"), + InlineKeyboardButton("Sport/animal", callback_data="reason_sport_animal")], + [InlineKeyboardButton("Injunction", callback_data="reason_injunction"), + InlineKeyboardButton("Missions", callback_data="reason_missions")], + [InlineKeyboardButton("Children", callback_data="reason_children")], + last_line])) diff --git a/callbackQuery/reason.py b/callbackQuery/reason.py new file mode 100644 index 0000000..33881f9 --- /dev/null +++ b/callbackQuery/reason.py @@ -0,0 +1,16 @@ +from telegram import Update +from telegram.ext import CallbackContext + +from callbackQuery.new import new +from main import reasons + + +def reason(update: Update, context: CallbackContext): + data = update.callback_query.data.replace("reason_", "") + if data in reasons[update.effective_chat.id][update["_effective_user"]["id"]]: + del reasons[update.effective_chat.id][update["_effective_user"]["id"]]\ + [reasons[update.effective_chat.id][update["_effective_user"]["id"]].index(data)] + else: + reasons[update.effective_chat.id][update["_effective_user"]["id"]].append(data) + new(update, context) + diff --git a/main.py b/main.py index 48dc6b4..6760a7d 100644 --- a/main.py +++ b/main.py @@ -10,6 +10,7 @@ local = {"first_name": "Firstname", "address": "Address"} database = {} messages = {} +reasons = {} logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)