Setup basic bot structure and data edit
This commit is contained in:
parent
6bf1795393
commit
41a063e6d9
12 changed files with 186 additions and 12 deletions
10
bot/__init__.py
Normal file
10
bot/__init__.py
Normal file
|
@ -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
|
5
callbackQuery/__init__.py
Normal file
5
callbackQuery/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
from callbackQuery.main import callback_query_handler
|
||||||
|
from main import updater
|
||||||
|
|
||||||
|
|
||||||
|
updater.dispatcher.add_handler(callback_query_handler)
|
33
callbackQuery/data.py
Normal file
33
callbackQuery/data.py
Normal file
|
@ -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")]]))
|
15
callbackQuery/edit.py
Normal file
15
callbackQuery/edit.py
Normal file
|
@ -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}
|
||||||
|
|
21
callbackQuery/main.py
Normal file
21
callbackQuery/main.py
Normal file
|
@ -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)
|
16
callbackQuery/new.py
Normal file
16
callbackQuery/new.py
Normal file
|
@ -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")
|
||||||
|
|
5
commands/__init__.py
Normal file
5
commands/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
from commands.start import start_handler
|
||||||
|
from main import updater
|
||||||
|
|
||||||
|
|
||||||
|
updater.dispatcher.add_handler(start_handler)
|
15
commands/start.py
Normal file
15
commands/start.py
Normal file
|
@ -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)
|
20
main.py
20
main.py
|
@ -1,20 +1,16 @@
|
||||||
import logging
|
import logging
|
||||||
from json import load
|
|
||||||
|
|
||||||
from telegram.ext import CommandHandler
|
from bot import updater
|
||||||
from telegram.ext 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)
|
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()
|
updater.start_polling()
|
||||||
|
|
5
message/__init__.py
Normal file
5
message/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
from main import updater
|
||||||
|
from message.main import message_handler
|
||||||
|
|
||||||
|
|
||||||
|
updater.dispatcher.add_handler(message_handler)
|
37
message/edit.py
Normal file
37
message/edit.py
Normal file
|
@ -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)
|
16
message/main.py
Normal file
16
message/main.py
Normal file
|
@ -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)
|
Reference in a new issue