def get_city(bot: Bot, update: Update) -> (int, str): uid = update.message.from_user.id message = update.message.text if is_cancelled(message): send_cancel(bot, uid, user_data={ 'reply_markup': ReplyKeyboardMarkup(START_KEYBOARD, True) }) return ConversationHandler.END elif is_stopped(message): on_stop(bot, update) return ConversationHandler.END elif is_back(message): bot.send_message( uid, MESSAGES['get_city:back'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SETTINGS_KEYBOARD, True) ) elif message not in CITIES: bot.send_message( uid, MESSAGES['get_city:incorrect'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SETTINGS_KEYBOARD, True) ) else: user = Users.get(Users.telegram_id == uid) user.set_city(message, is_update=True) user.save() bot.send_message(uid, MESSAGES['get_city:correct'], ParseMode.HTML) send_current(bot, uid, user.email, user.city) return SETTINGS
def add_user(bot: Bot, update: Update, user_data: dict) -> (int, str): uid = update.message.from_user.id message = update.message.text username = update.message.from_user.username if is_cancelled(message): user_data['reply_markup'] = ReplyKeyboardMarkup( REGISTER_KEYBOARD, True) send_cancel(bot, uid, user_data) return ConversationHandler.END elif is_stopped(message): on_stop(message), return ConversationHandler.END user = Users.create(telegram_id=uid, username=username) user.set_city(user_data['reg_city']) user.set_email(user_data['reg_email']) user.set_status(user_data['reg_email']) user.save() # TODO: check email is valid for key, val in user_data.items(): del key, val thread = Thread(name=f"get_and_save::{uid}, {user.email}", target=get_and_save, args=((user.email, user.is_student, uid), )) thread.start() bot.send_message(uid, MESSAGES['add_user:msg'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(START_KEYBOARD, True)) return ConversationHandler.END
def choose_menu(bot: Bot, update: Update) -> (int, str): chat_id = update.message.chat.id message = update.message.text if is_cancelled(message): send_cancel(bot, chat_id, user_data={ 'reply_markup': ReplyKeyboardMarkup(START_KEYBOARD, True) }) return ConversationHandler.END elif is_stopped(message): on_stop(bot, update) return ConversationHandler.END elif is_back(message): on_back(bot, update) return ConversationHandler.END if message == SETTINGS_KEYBOARD[0][0]: return show_about(bot, update) elif message == SETTINGS_KEYBOARD[1][0]: bot.send_message( chat_id, MESSAGES['choose_menu:ask_email'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup([BACK_KEY], True) ) return ASK_EMAIL elif message == SETTINGS_KEYBOARD[1][1]: bot.send_message( chat_id, MESSAGES['choose_menu:ask_city'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(CITIES_KEYBOARD_BACK, True) ) return ASK_CITY elif message == SETTINGS_KEYBOARD[2][0]: bot.send_message( chat_id, MESSAGES['choose_menu:feedback'], ParseMode.HTML ) else: bot.send_message( chat_id, MESSAGES['choose_menu:spam'](message), ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SETTINGS_KEYBOARD, True) ) return SETTINGS
def on_spam(bot: Bot, update: Update) -> str: chat_id = update.message.chat.id message = update.message.text if is_cancelled(message): send_cancel(bot, chat_id, user_data={ 'reply_markup': ReplyKeyboardMarkup(START_KEYBOARD, True) }) return ConversationHandler.END elif is_stopped(message): on_stop(bot, update) return ConversationHandler.END bot.send_message(chat_id, MESSAGES['on_spam'](message), ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SCHEDULE_KEYBOARD, True)) return SCHEDULE
def ask_city(bot: Bot, update: Update) -> (int, str): chat_id = update.message.chat.id message = update.message.text if is_cancelled(message): send_cancel(bot, chat_id, user_data={ 'reply_markup': ReplyKeyboardMarkup(REGISTER_KEYBOARD, True) }) return ConversationHandler.END elif is_stopped(message): on_stop(bot, update) return ConversationHandler.END bot.send_message(chat_id, MESSAGES['ask_city:ask'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(CITIES_KEYBOARD, True)) return ASK_CITY
def get_email(bot: Bot, update: Update) -> (int, str): uid = update.message.from_user.id message = update.message.text if is_cancelled(message): send_cancel(bot, uid, user_data={ 'reply_markup': ReplyKeyboardMarkup(START_KEYBOARD, True) }) return ConversationHandler.END elif is_stopped(message): on_stop(bot, update) return ConversationHandler.END elif is_back(message): bot.send_message( uid, MESSAGES['get_email:back'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SETTINGS_KEYBOARD, True) ) elif not Users.check_email(message): bot.send_message( uid, MESSAGES['get_email:incorrect'], ParseMode.HTML, reply_markup=ReplyKeyboardMarkup(SETTINGS_KEYBOARD, True) ) else: user = Users.get(Users.telegram_id == uid) user.set_email(message) user.set_status(message) user.save() # TODO: check email is correct thread = Thread( name=f"get_and_save::{uid}, {message}", target=get_and_save, args=((user.email, user.is_student, uid), ) ) thread.start() bot.send_message(uid, MESSAGES['get_email:correct'], ParseMode.HTML) send_current(bot, uid, user.email, user.city) return SETTINGS
def get_email(bot: Bot, update: Update, user_data: dict) -> (int, str): chat_id = update.message.chat.id message = update.message.text if is_cancelled(message): user_data['reply_markup'] = ReplyKeyboardMarkup( REGISTER_KEYBOARD, True) send_cancel(bot, chat_id, user_data) return ConversationHandler.END elif is_stopped(message): on_stop(message), return ConversationHandler.END if not Users.check_email(message): bot.send_message(chat_id, MESSAGES['get_email:incorrect'], ParseMode.HTML) return user_data['reg_email'] = message bot.send_message(chat_id, MESSAGES['get_email:correct'], ParseMode.HTML) return ask_city(bot, update)
def get_city(bot: Bot, update: Update, user_data: dict, verbose: bool = False) -> (int, str): chat_id = update.message.chat.id message = update.message.text if is_cancelled(message): user_data['reply_markup'] = ReplyKeyboardMarkup( REGISTER_KEYBOARD, True) send_cancel(bot, chat_id, user_data) return ConversationHandler.END elif is_stopped(message): on_stop(message), return ConversationHandler.END if message not in CITIES: bot.send_message(chat_id, MESSAGES['get_city:incorrect'], ParseMode.HTML) message = "Москва" # default value user_data['reg_city'] = message if verbose: bot.send_message(chat_id, MESSAGES['get_city:msg'], ParseMode.HTML) return add_user(bot, update, user_data)