Пример #1
0
def title_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["cancel"],
                                 language)

    # If pushed button is "cancel" exit user from conversation
    if pushed_button == "cancel":
        return exit_from_conversation(update)

    # Check if user has task with such title
    if update.message.text in get_user_tasks(user_id, only_titles=True):
        update.message.reply_text(get_message("task_exist", language))
        return "title_handler"

    # Clear context.user_data for new task and load title there
    context.user_data["new_task"] = {}
    context.user_data["new_task"]["title"] = update.message.text

    # Send message to write days of the week of the task
    update.message.reply_text(get_message("write_task_days_of_the_week",
                                          language),
                              reply_markup=get_menu_keyboard(
                                  "cancel_back", language))

    return "days_of_the_week_handler"
Пример #2
0
def start_handler(update, context):
    language = get_user_language(update=update, short=True)

    update.message.reply_text(get_message("welcome", language))
    send_main_menu(update)

    return 'main_menu'
Пример #3
0
def send_main_menu(update):
    # Get user language
    language = get_user_language(update=update, short=True)

    # Send menu
    update.message.reply_text(get_signboard("main_menu", language),
                              reply_markup=get_menu_keyboard(
                                  "main_menu", language))
Пример #4
0
def send_edit_mode_menu(update):
    # Get user language
    language = get_user_language(update=update, short=True)

    # Send menu
    update.message.reply_text(get_message("choice_action", language),
                              reply_markup=get_menu_keyboard(
                                  "edit_task_menu", language))
def language_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    user_language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, [["back"]], user_language)

    # If pushed button is "back", return to settings menu
    if pushed_button == "back":
        send_settings_menu(update)
        return "menu_handler"

    # Get user message
    text = update.message.text

    selected_language = {}

    # Check if user message is language
    for language in LANGUAGES:
        if language["title"].lower() in text.lower():
            selected_language = language
            break

    # If user message is language
    if selected_language:
        # Create database session
        session = db_session.create_session()

        # Edit user's language in database
        user = session.query(User).filter(User.telegram_id == user_id).first()
        user.language_id = selected_language["id"]

        # Commit and close database session
        session.commit()
        session.close()

        # Say user that language changed
        update.message.reply_text(
            get_message("language_changed", selected_language["short"],
                        selected_language["emoji"]))

        # Return to settings menu
        send_settings_menu(update)
        return "menu_handler"

    # If user message isn't button, ask user click on the buttons
    else:
        update.message.reply_text(get_message("click_buttons", user_language))
        return "language_handler"
Пример #6
0
def days_of_the_week_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["cancel_back"],
                                 language)

    # If pushed button is "cancel" exit user from conversation
    if pushed_button == "cancel":
        return exit_from_conversation(update)

    # If pushed button is "back" return user to previous
    # stage of task adding - write task title
    if pushed_button == "back":
        update.message.reply_text(get_message("write_task_title", language),
                                  reply_markup=get_menu_keyboard(
                                      "cancel", language))

        return "title_handler"

    # Get days of the week from user message
    days_of_the_week_str = get_days_of_the_week_from_string(
        update.message.text, language)

    # Check if message is incorrect
    if not days_of_the_week_str:
        update.message.reply_text(get_message("invalid_input", language))
        return "days_of_the_week_handler"

    # Load days of the week to context.user_data for new task
    context.user_data["new_task"]["days_of_the_week"] = days_of_the_week_str

    # Get data from context.user_data
    new_task_data = context.user_data["new_task"]

    # Add new task to database
    add_task(user_id, new_task_data["title"],
             new_task_data["days_of_the_week"])

    # Clear context.user_data for new task
    context.user_data["new_task"] = {}

    # Send message to user about successful adding
    update.message.reply_text(get_message("task_added", language))

    # Exit from conversation
    return exit_from_conversation(update)
Пример #7
0
def handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["main_menu"],
                                 language)

    # If pushed button is "today_tasks"
    if pushed_button == "today_tasks":
        # Get user's today tasks
        today_tasks = get_user_tasks(user_id,
                                     only_titles=True,
                                     today_tasks=True)

        # If user has today tasks, make list of tasks
        if today_tasks:
            text = get_message("today_tasks", language) + '\n\n'
            for task in today_tasks:
                text += task + "\n"

        # If user hasn't today tasks, say user about it
        else:
            text = get_message("not_tasks_today", language)

        update.message.reply_text(text)

        return "main_menu"

    # If pushed button is "editor", proceed to editor menu
    elif pushed_button == "editor":
        send_editor_menu(update)
        return "editor"

    # If pushed button is "settings", proceed to settings menu
    elif pushed_button == "settings":
        send_settings_menu(update)
        return "settings"

    # If user message isn't button, ask user click on the buttons
    else:
        update.message.reply_text(get_message("click_buttons", language))
        send_main_menu(update)
        return "main_menu"
Пример #8
0
def send_choice_tasks_menu(update, page=0):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Get tasks keyboard
    keyboard = get_tasks_keyboard(user_id, language, page=page)

    # If keyboard doesn't exist - return False
    if not keyboard:
        return False

    # Send menu
    update.message.reply_text(get_message("choice_task", language),
                              reply_markup=keyboard)

    # Return True, if message sent
    return True
def menu_handler(update, context):
    # Get user language
    language = get_user_language(update=update, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["settings_menu"],
                                 language)

    # If pushed button is "back", exit from conversation
    if pushed_button == "back":
        return exit_from_conversation(update)

    # If pushed button is "choice_language", proceed to choice language menu
    elif pushed_button == "choice_language":
        update.message.reply_text(get_message("choice_language", language),
                                  reply_markup=get_languages_menu(language))

        return "language_handler"
Пример #10
0
def handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["editor_menu"],
                                 language)

    # If pushed button is "add_task", proceed to task adding
    if pushed_button == "add_task":
        update.message.reply_text(get_message("write_task_title", language),
                                  reply_markup=get_menu_keyboard(
                                      "cancel", language))

        return "add_task"

    # If pushed button is "add_task"
    elif pushed_button == "edit_task":
        # If user has tasks, proceed to task editing
        if get_user_tasks(user_id):
            send_choice_tasks_menu(update)
            return "edit_task"

        # If user hasn't tasks, stay in editor menu
        else:
            update.message.reply_text(get_message("not_tasks", language))
            send_editor_menu(update)

            return "editor_menu"

    # If pushed button is "back", exit from conversation
    elif pushed_button == "back":
        return exit_from_conversation(update)

    # If user message isn't button, ask user click on the buttons
    else:
        update.message.reply_text(get_message("click_buttons", language))
        send_editor_menu(update)

        return "editor_menu"
def new_title_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["cancel"],
                                 language)

    # If pushed button is "cancel", send edit mode menu
    if pushed_button == "cancel":
        send_edit_mode_menu(update)
        return "edit_mode_handler"

    new_title = update.message.text

    task_titles_list = get_user_tasks(user_id, only_titles=True)

    # Check if title with such title exist
    if new_title in task_titles_list:
        update.message.reply_text(get_message("task_exist", language))
        return "new_title_handler"

    # Edit task title
    edit_result = edit_task(context.user_data["selected_task_id"],
                            title=new_title)

    # If editing is successful, return to edit mode handler
    if edit_result:
        update.message.reply_text(get_message("task_title_edited", language))
        send_edit_mode_menu(update)
        return "edit_mode_handler"

    # Else send message to user about invalid input
    else:
        update.message.reply_text(get_message("invalid_input", language))
        return "new_title_handler"
def new_days_of_the_week_handler(update, context):
    # Get user language
    language = get_user_language(update=update, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["cancel"],
                                 language)

    # If pushed button is "cancel", send edit mode menu
    if pushed_button == "cancel":
        send_edit_mode_menu(update)
        return "edit_mode_handler"

    # Get days of the week from user message
    days_of_the_week_str = get_days_of_the_week_from_string(
        update.message.text, language)

    # Check if message is incorrect
    if not days_of_the_week_str:
        update.message.reply_text(get_message("invalid_input", language))
        return "new_days_of_the_week_handler"

    # Edit task title
    edit_result = edit_task(context.user_data["selected_task_id"],
                            days_of_the_week=days_of_the_week_str)

    # If editing is successful, return to edit mode handler
    if edit_result:
        update.message.reply_text(
            get_message("task_days_of_the_week_edited", language))
        send_edit_mode_menu(update)
        return "edit_mode_handler"

    # Else send message to user about invalid input
    else:
        update.message.reply_text(get_message("invalid_input", language))
        return "new_days_of_the_week_handler"
def choice_task_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update,
                                 KEYBOARDS["static"]["cancel_back_next"],
                                 language)

    # If user message in user tasks, proceed to editing/deleting
    if update.message.text in get_user_tasks(user_id, only_titles=True):
        # Save to context.user_data selected task id
        context.user_data["selected_task_id"] = \
            get_task_from_title(update.message.text, user_id, only_id=True)

        # Send to user menu with modes for editing
        send_edit_mode_menu(update)

        return "edit_mode_handler"

    # If pushed button is "cancel", exit user from conversation
    if pushed_button == "cancel":
        context.user_data["page"] = 0
        return exit_from_conversation(update)

    # If pushed button is "next"
    elif pushed_button == "next":
        # Increase page in context.user_data
        if "page" in context.user_data:
            context.user_data["page"] += 1
        else:
            context.user_data["page"] = 1

        # Send menu to user for choice task
        sending_result = send_choice_tasks_menu(update,
                                                context.user_data["page"])

        # If menu sending is unsuccessful, send previous page of menu
        if not sending_result:
            update.message.reply_text(get_message("click_buttons", language))
            context.user_data["page"] -= 1
            send_choice_tasks_menu(update, context.user_data["page"])

            return "choice_task_handler"

    # If pushed button is "back"
    elif pushed_button == "back":
        # If menu page more than 0, send previous page of menu
        if "page" in context.user_data:
            if context.user_data["page"] > 0:
                context.user_data["page"] -= 1
                send_choice_tasks_menu(update, context.user_data["page"])

                return "choice_task_handler"

        # If menu page is 0, send present page of menu
        update.message.reply_text(get_message("click_buttons", language))
        send_choice_tasks_menu(update)
        return "choice_task_handler"

    # Else send present page of menu
    else:
        update.message.reply_text(get_message("click_buttons", language))
        send_choice_tasks_menu(update)
        return "choice_task_handler"
def edit_mode_handler(update, context):
    # Get user id and user language
    user_id = update.message.from_user.id
    language = get_user_language(user_id=user_id, short=True)

    # Check pushed button of keyboard
    pushed_button = check_button(update, KEYBOARDS["static"]["edit_task_menu"],
                                 language)

    # If pushed button is "back" return user to previous
    # stage of task editing - choice task menu
    if pushed_button == "back":
        if "page" not in context.user_data:
            context.user_data["page"] = 0

        send_choice_tasks_menu(update, context.user_data["page"])

        return "choice_task_handler"

    # If pushed button is "title", proceed to title editing
    elif pushed_button == "title":
        update.message.reply_text(get_message("write_task_title", language),
                                  reply_markup=get_menu_keyboard(
                                      "cancel", language))

        return "new_title_handler"

    # If pushed button is "days_of_the_week",
    # proceed to days of the week editing
    elif pushed_button == "days_of_the_week":
        update.message.reply_text(get_message("write_task_days_of_the_week",
                                              language),
                                  reply_markup=get_menu_keyboard(
                                      "cancel", language))

        return "new_days_of_the_week_handler"

    # If pushed button is "delete", delete task
    elif pushed_button == "delete":
        # Delete task
        delete_task(context.user_data["selected_task_id"])

        # Send message to user about successful deleting
        update.message.reply_text(get_message("task_deleted", language))

        # If user has tasks, send choice task menu
        if get_user_tasks(user_id):
            send_choice_tasks_menu(update)
            context.user_data["page"] = 0

            return "choice_task_handler"

        # Else send editor menu
        else:
            send_editor_menu(update)

            return ConversationHandler.END

    # If more than one condition is incorrect,
    # send message to user push to buttons
    else:
        update.message.reply_text(get_message("click_buttons", language))

        return "edit_mode_handler"