diff --git a/bot/__init__.py b/bot/__init__.py new file mode 100644 index 0000000..6efa0c6 --- /dev/null +++ b/bot/__init__.py @@ -0,0 +1,10 @@ +from json import load + +from telegram.ext import Updater + + +updater = Updater(token=load(open("config.json"))["bot_token"], use_context=True) + +import commands +import callbackQuery +import message diff --git a/callbackQuery/__init__.py b/callbackQuery/__init__.py new file mode 100644 index 0000000..f9ddabe --- /dev/null +++ b/callbackQuery/__init__.py @@ -0,0 +1,5 @@ +from callbackQuery.main import callback_query_handler +from main import updater + + +updater.dispatcher.add_handler(callback_query_handler) diff --git a/callbackQuery/data.py b/callbackQuery/data.py new file mode 100644 index 0000000..2d50ea0 --- /dev/null +++ b/callbackQuery/data.py @@ -0,0 +1,33 @@ +from telegram import InlineKeyboardButton, InlineKeyboardMarkup, ParseMode, Update +from telegram.ext import CallbackContext + +from main import database + + +def data(update: Update, context: CallbackContext): + if update["_effective_user"]["id"] not in database: + database[update["_effective_user"]["id"]] = {"first_name": None, + "last_name": None, + "birth_date": None, + "birth_city": None, + "address": None} + context.bot.send_message(chat_id=update.effective_chat.id, + text=f"*Firstname*: `{database[update['_effective_user']['id']]['first_name']}`\n" + f"*Lastname*: `{database[update['_effective_user']['id']]['last_name']}`\n" + f"*Birth date*: `{database[update['_effective_user']['id']]['birth_date']}`\n" + f"*Birth city*: `{database[update['_effective_user']['id']]['birth_city']}`\n" + f"*Address*: `{database[update['_effective_user']['id']]['address']}`\n\n" + f"Choose the data you want to edit", + parse_mode=ParseMode.MARKDOWN_V2, + reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Firstname", + callback_data="edit_first_name"), + InlineKeyboardButton("Lastname", + callback_data="edit_last_name")], + [InlineKeyboardButton("Birth date", + callback_data="edit_birth_date"), + InlineKeyboardButton("Birth city", + callback_data="edit_birth_city")], + [InlineKeyboardButton("Address", + callback_data="edit_address")], + [InlineKeyboardButton("Home", + callback_data="home")]])) diff --git a/callbackQuery/edit.py b/callbackQuery/edit.py new file mode 100644 index 0000000..f7da337 --- /dev/null +++ b/callbackQuery/edit.py @@ -0,0 +1,15 @@ +from telegram import Update, ParseMode +from telegram.ext import CallbackContext + +from main import messages, local + + +def edit(update: Update, context: CallbackContext): + context.bot.send_message(chat_id=update.effective_chat.id, + text=f"Send the new value for `{local[update.callback_query.data.replace('edit_', '')]}`", + parse_mode=ParseMode.MARKDOWN_V2) + 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} + diff --git a/callbackQuery/main.py b/callbackQuery/main.py new file mode 100644 index 0000000..fc3dd17 --- /dev/null +++ b/callbackQuery/main.py @@ -0,0 +1,21 @@ +from telegram import Update +from telegram.ext import CallbackContext, CallbackQueryHandler + +from callbackQuery.data import data +from callbackQuery.edit import edit +from callbackQuery.new import new +from commands.start import start + + +def callback_query(update: Update, context: CallbackContext): + if update.callback_query.data == "home": + start(update, context) + elif update.callback_query.data == "new": + new(update, context) + elif update.callback_query.data == "data": + data(update, context) + elif update.callback_query.data.startswith("edit_"): + edit(update, context) + + +callback_query_handler = CallbackQueryHandler(callback_query) diff --git a/callbackQuery/new.py b/callbackQuery/new.py new file mode 100644 index 0000000..57c2315 --- /dev/null +++ b/callbackQuery/new.py @@ -0,0 +1,16 @@ +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import CallbackContext + +from main import database + + +def new(update: Update, context: CallbackContext): + if update['_effective_user']['id'] not in database or not all(database[update['_effective_user']['id']].values()): + context.bot.send_message(chat_id=update.effective_chat.id, text="You have no data saved !", + reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Set data", + callback_data="data")], + [InlineKeyboardButton("Home", callback_data="home")] + ])) + else: + context.bot.send_message(chat_id=update.effective_chat.id, text="Select your reasons") + diff --git a/commands/__init__.py b/commands/__init__.py new file mode 100644 index 0000000..301f198 --- /dev/null +++ b/commands/__init__.py @@ -0,0 +1,5 @@ +from commands.start import start_handler +from main import updater + + +updater.dispatcher.add_handler(start_handler) diff --git a/commands/start.py b/commands/start.py new file mode 100644 index 0000000..e30c577 --- /dev/null +++ b/commands/start.py @@ -0,0 +1,15 @@ +from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import CallbackContext, CommandHandler + + +def start(update: Update, context: CallbackContext): + context.bot.send_message(chat_id=update.effective_chat.id, + text="Welcome to TeleExit", + reply_markup=InlineKeyboardMarkup([[InlineKeyboardButton("Create a new certificate", + callback_data="new")], + [InlineKeyboardButton("Manage saved data", + callback_data="data")] + ])) + + +start_handler = CommandHandler("start", start) diff --git a/main.py b/main.py index 0d224b9..48dc6b4 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,16 @@ import logging -from json import load -from telegram.ext import CommandHandler -from telegram.ext import Updater +from bot import updater -updater = Updater(token=load(open("config.json"))["bot_token"], use_context=True) +local = {"first_name": "Firstname", + "last_name": "Lastname", + "birth_date": "Birth date", + "birth_city": "Birth city", + "address": "Address"} +database = {} +messages = {} logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO) - -def start(update, context): - context.bot.send_message(chat_id=update.effective_chat.id, text="I'm a bot, please talk to me!") - - -start_handler = CommandHandler("start", start) -updater.dispatcher.add_handler(start_handler) - updater.start_polling() diff --git a/message/__init__.py b/message/__init__.py new file mode 100644 index 0000000..091e4c0 --- /dev/null +++ b/message/__init__.py @@ -0,0 +1,5 @@ +from main import updater +from message.main import message_handler + + +updater.dispatcher.add_handler(message_handler) diff --git a/message/edit.py b/message/edit.py new file mode 100644 index 0000000..67fdc99 --- /dev/null +++ b/message/edit.py @@ -0,0 +1,37 @@ +import re +from datetime import datetime + +from telegram import Update, ParseMode +from telegram.ext import CallbackContext + +from callbackQuery.data import data +from main import local, messages, database + +rex = { + "first_name": re.compile(r"^([a-zA-Z]| )+$"), + "last_name": re.compile(r"^([a-zA-Z]| )+$"), + "birth_date": re.compile(r"[0-9]{1,2}/[0-9]{1,2}/[0-9]{4}"), + "birth_city": re.compile(r"^[a-zA-Z]+(?:[\s-][a-zA-Z]+)*$"), + "address": re.compile(r"^.*$") +} + + +def check_date(date) -> bool: + try: + datetime.strptime(date, "%d/%m/%Y") + except: + return False + else: + return True + + +def edit(update: Update, context: CallbackContext, data_edit: str): + name = data_edit.replace("edit_", "") + if not rex[name].fullmatch(update.message.text) or\ + (name == "birth_date" and not check_date(update.message.text)): + context.bot.send_message(chat_id=update.effective_chat.id, text=f"Invalid value for `{local[name]}` \!", + parse_mode=ParseMode.MARKDOWN_V2) + else: + del messages[update.effective_chat.id][update["_effective_user"]["id"]] + database[update["_effective_user"]["id"]][name] = update.message.text + data(update, context) diff --git a/message/main.py b/message/main.py new file mode 100644 index 0000000..3f01930 --- /dev/null +++ b/message/main.py @@ -0,0 +1,16 @@ +from telegram import Update +from telegram.ext import CallbackContext, MessageHandler, Filters + +from main import messages +from message.edit import edit + + +def message(update: Update, context: CallbackContext): + if update.effective_chat.id in messages: + if update["_effective_user"]["id"] in messages[update.effective_chat.id]: + data = messages[update.effective_chat.id][update["_effective_user"]["id"]] + if data.startswith("edit_"): + edit(update, context, data) + + +message_handler = MessageHandler(Filters.text, message)