Example #1
0
def choose_dow(bot: Bot, update: Update) -> (int, str, None):
    """ Send schedule for given Day Of Week (dow) """
    uid = update.message.from_user.id
    chat_id = update.message.chat.id
    message = update.message.text

    if is_cancelled(message):
        send_cancel(bot, uid)
        return ConversationHandler.END
    elif is_back(message):
        bot.send_message(chat_id,
                         MESSAGES['choose_dow:back'],
                         ParseMode.HTML,
                         reply_markup=ReplyKeyboardMarkup(
                             SCHEDULE_KEYBOARD, True))
        return SCHEDULE

    lessons = get_lessons(uid)
    if not lessons:
        bot.send_message(chat_id, MESSAGES['choose_dow:empty'], ParseMode.HTML)
        return
    schedule = dict(
        zip(DAY_MAPPING, [
            lessons.monday, lessons.tuesday, lessons.wednesday,
            lessons.thursday, lessons.friday, lessons.saturday
        ]))
    if message not in schedule:
        bot.send_message(chat_id, MESSAGES['choose_dow:spam'](message),
                         ParseMode.HTML)
        return
    bot.send_message(chat_id, schedule[message], ParseMode.HTML)
Example #2
0
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
Example #3
0
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
Example #4
0
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
Example #5
0
def ask_email(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

    bot.send_message(chat_id,
                     MESSAGES['ask_email:ask'],
                     ParseMode.HTML,
                     reply_markup=ReplyKeyboardRemove())
    return ASK_EMAIL
Example #6
0
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
Example #7
0
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
Example #8
0
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)
Example #9
0
def recipients(bot: Bot, update: Update, user_data: dict) -> (int, str):
    uid = update.message.from_user.id
    message = update.message.text
    if is_cancelled(message):
        bot.send_message(**user_data['whom_to_send_sp'])
        return ConversationHandler.END
    if message == MAILING_WHOM_KEYBOARD[0][0]:
        user_data['recipients'] = 'all'
    elif message == MAILING_WHOM_KEYBOARD[1][-1]:
        user_data['recipients'] = 'lecturers'
    elif message == MAILING_WHOM_KEYBOARD[1][0]:
        user_data['recipients'] = 'students'
    elif message == BACK_KEY[0]:
        bot.send_message(
            uid,
            MESSAGES['recipients:back'],
            ParseMode.HTML
        )
        start(bot, update)
        return ConversationHandler.END
    else:
        bot.send_message(
            uid,
            MESSAGES['whom_to_send:spam'](message),
            ParseMode.HTML
        )
        bot.send_message(**user_data['whom_to_send_sp'])
        return WHOM_TO_SEND

    send_params = {
        'text': MESSAGES['recipients:ask'],
        'chat_id': uid,
        'reply_markup': ReplyKeyboardMarkup([BACK_KEY], resize_keyboard=True),
        'parse_mode': ParseMode.HTML
    }

    user_data['recipients_sp'] = send_params

    bot.send_message(**send_params)
    return PREPARE_MAILING
Example #10
0
def on_day(bot: Bot, update: Update, next_day: bool = False) -> (int, None):
    uid = update.message.from_user.id
    chat_id = update.message.chat.id
    message = update.message.text

    if is_cancelled(message):
        send_cancel(bot, uid)
        return ConversationHandler.END

    lessons = get_lessons(uid)
    if not lessons:
        bot.send_message(chat_id, MESSAGES['on_day:empty'], ParseMode.HTML)
        return
    schedule = dict(
        zip(DAY_MAPPING, [
            lessons.monday, lessons.tuesday, lessons.wednesday,
            lessons.thursday, lessons.friday, lessons.saturday
        ]))
    day = (datetime.now().weekday() + int(next_day)) % 7
    try:
        text = schedule[DAY_MAPPING[day]]
    except IndexError:
        text = MESSAGES['on_day:sunday']
    bot.send_message(chat_id, text, ParseMode.HTML)
Example #11
0
def prepare_mailing(bot: Bot, update: Update, user_data: dict) -> (int, str):
    uid = update.message.from_user.id
    message = update.message.text
    if is_cancelled(message):
        bot.send_message(**user_data['recipients_sp'])
        return WHOM_TO_SEND

    recipients = Users.select()
    if user_data['recipients'] == 'students':
        recipients = recipients.where(Users.is_student == 1)
    elif user_data['recipients'] == 'lecturers':
        recipients = recipients.where(Users.is_student == 0)

    for key, val in user_data.items():
        del key, val

    if recipients.exists():
        thread = Thread(
            name=f"mailing::{uid}",
            target=do_mailing,
            args=(bot, recipients, message, uid)
        )
        thread.start()
        bot.send_message(
            uid,
            MESSAGES['prepare_mailing:start'],
            ParseMode.HTML
        )
        start(bot, update)
    else:
        bot.send_message(
            uid,
            MESSAGES['prepare_mailing:empty'],
            ParseMode.HTML
        )
    return ConversationHandler.END
Example #12
0
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)