Archived
1
0
Fork 0

Setup reasons selection

This commit is contained in:
Ethanell 2020-11-07 12:16:52 +01:00
parent 41a063e6d9
commit 248d750c97
6 changed files with 60 additions and 4 deletions

9
callbackQuery/create.py Normal file
View file

@ -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"]]

View file

@ -11,5 +11,5 @@ def edit(update: Update, context: CallbackContext):
if update.effective_chat.id not in messages: if update.effective_chat.id not in messages:
messages[update.effective_chat.id] = {} 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

View file

@ -1,9 +1,11 @@
from telegram import Update from telegram import Update
from telegram.ext import CallbackContext, CallbackQueryHandler from telegram.ext import CallbackContext, CallbackQueryHandler
from callbackQuery.create import create
from callbackQuery.data import data from callbackQuery.data import data
from callbackQuery.edit import edit from callbackQuery.edit import edit
from callbackQuery.new import new from callbackQuery.new import new
from callbackQuery.reason import reason
from commands.start import start from commands.start import start
@ -14,8 +16,12 @@ def callback_query(update: Update, context: CallbackContext):
new(update, context) new(update, context)
elif update.callback_query.data == "data": elif update.callback_query.data == "data":
data(update, context) data(update, context)
elif update.callback_query.data == "create":
create(update, context)
elif update.callback_query.data.startswith("edit_"): elif update.callback_query.data.startswith("edit_"):
edit(update, context) edit(update, context)
elif update.callback_query.data.startswith("reason_"):
reason(update, context)
callback_query_handler = CallbackQueryHandler(callback_query) callback_query_handler = CallbackQueryHandler(callback_query)

View file

@ -1,7 +1,7 @@
from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton, ParseMode
from telegram.ext import CallbackContext from telegram.ext import CallbackContext
from main import database from main import database, reasons
def new(update: Update, context: CallbackContext): def new(update: Update, context: CallbackContext):
@ -12,5 +12,29 @@ def new(update: Update, context: CallbackContext):
[InlineKeyboardButton("Home", callback_data="home")] [InlineKeyboardButton("Home", callback_data="home")]
])) ]))
else: 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]))

16
callbackQuery/reason.py Normal file
View file

@ -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)

View file

@ -10,6 +10,7 @@ local = {"first_name": "Firstname",
"address": "Address"} "address": "Address"}
database = {} database = {}
messages = {} messages = {}
reasons = {}
logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO) logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)