コード例 #1
0
def select_study_level(message):
    from flask_app import bot, start_handler

    answer = ""

    sql_con = sqlite3.connect("Bot_db")
    cursor = sql_con.cursor()
    cursor.execute(
        """SELECT study_programs_json 
                      FROM user_choice 
                      WHERE user_id = ?""", (message.chat.id, ))
    data = cursor.fetchone()[0]
    cursor.close()
    sql_con.close()

    study_programs = json.loads(data)

    study_level_names = []
    for study_program in study_programs:
        study_level_names.append(study_program["StudyLevelName"])
    if message.text in study_level_names:
        answer += "Укажи программу:"
        study_program_combinations_keyboard = telebot.types.ReplyKeyboardMarkup(
            True, False)
        index = study_level_names.index(message.text)
        study_program_combinations = study_programs[index][
            "StudyProgramCombinations"]
        for study_program_combination in study_program_combinations:
            study_program_combinations_keyboard.row(
                study_program_combination["Name"])
        study_program_combinations_keyboard.row("Другая ступень")

        sql_con = sqlite3.connect("Bot_db")
        cursor = sql_con.cursor()
        cursor.execute(
            """UPDATE user_choice 
                          SET study_level_name = ?
                          WHERE user_id = ?""",
            (message.text, message.chat.id))
        sql_con.commit()
        cursor.close()
        sql_con.close()

        bot.send_message(message.chat.id,
                         answer,
                         reply_markup=study_program_combinations_keyboard)
        set_next_step(message.chat.id, "select_study_program_combination")
    elif message.text == "Другое направление":
        start_handler(message)
        return
    else:
        answer += "Пожалуйста, укажи ступень:"
        bot.send_message(message.chat.id, answer)
        set_next_step(message.chat.id, "select_study_level")
コード例 #2
0
def select_division(message):
    from flask_app import bot, start_handler

    answer = ''

    sql_con = sl3.connect(const.path + 'Bot.db')
    cursor = sql_con.cursor()
    cursor.execute('''SELECT divisions_json
                        FROM user_choice
                       WHERE user_id = ?''', (message.chat.id,))
    data = cursor.fetchone()
    cursor.close()
    sql_con.close()

    divisions = json.loads(data[0])
    aliases = [division['Alias'] for division in divisions]
    if message.text in const.existing_divisions:
        answer += 'Выбери курс:'
        study_programs_keyboard = types.ReplyKeyboardMarkup(
            True, False)
        index = const.existing_divisions.index(message.text)
        alias = aliases[index]

        for study_program in const.existing_courses:
            study_programs_keyboard.row(study_program)
        study_programs_keyboard.row('Другое направление')

        data = json.dumps(const.courses, ensure_ascii=False)
        sql_con = sl3.connect(const.path + 'Bot.db')
        cursor = sql_con.cursor()
        cursor.execute('''UPDATE user_choice
                             SET div_alias = ?,
                                 division_name = ?,
                                 study_programs_json = ?
                           WHERE user_id = ?''',
                       (alias, message.text, data, message.chat.id,))
        sql_con.commit()
        cursor.close()
        sql_con.close()

        bot.send_message(message.chat.id, answer,
                         reply_markup=study_programs_keyboard)
        set_next_step(message.chat.id, 'select_admission_year')
    elif message.text == 'Другой способ регистрации':
        start_handler(message)
        return
    else:
        answer += 'Пожалуйста, укажи направление:'
        bot.send_message(message.chat.id, answer)
        set_next_step(message.chat.id, 'select_division')
コード例 #3
0
def confirm_choice(message):
    from flask_app import bot, start_handler, main_keyboard
    from constants import emoji

    if message.text == "Все верно":
        sql_con = sqlite3.connect("Bot_db")
        cursor = sql_con.cursor()
        cursor.execute(
            """SELECT alias, student_group_id
                          FROM user_choice 
                          WHERE user_id = ?""", (message.chat.id, ))
        data = cursor.fetchone()
        alias = data[0]
        group_id = data[1]
        try:
            cursor.execute(
                """INSERT INTO user_data (id, alias, group_id)
                              VALUES (?, ?, ?)""",
                (message.chat.id, alias, group_id))
        except sqlite3.IntegrityError:
            sql_con.rollback()
            cursor.execute(
                """UPDATE user_data 
                              SET alias = ?, group_id = ?
                              WHERE id = ?""",
                (alias, group_id, message.chat.id))
        finally:
            sql_con.commit()
            cursor.execute("""DELETE FROM user_choice WHERE user_id = ?""",
                           (message.chat.id, ))
            sql_con.commit()
        url = "https://timetable.spbu.ru/api/v1/groups/{0}/events".format(
            group_id)
        week_data = requests.get(url).json()
        data = json.dumps(week_data)
        try:
            cursor.execute(
                """INSERT INTO groups_data 
                              (id, alias, json_week_data)
                              VALUES (?, ?, ?)""", (group_id, alias, data))
        except sqlite3.IntegrityError:
            cursor.execute(
                """UPDATE groups_data
                              SET json_week_data = ?
                              WHERE id = ? AND alias = ?""",
                (data, group_id, alias))
        finally:
            sql_con.commit()
            cursor.close()
            sql_con.close()
        answer = "Главное меню\n\n" \
                 "{0} - информация о боте\n" \
                 "{1} - оценить бота\n" \
                 "{2} - настройки\n" \
                 "{3} - электрички\n" \
                 "{4} - редактор расписания\n" \
                 "@Spbu4u_news - новости бота".format(emoji["info"],
                                                      emoji["star"],
                                                      emoji["settings"],
                                                      emoji["suburban"],
                                                      emoji["editor"])
        bot.send_message(message.chat.id,
                         answer,
                         reply_markup=main_keyboard,
                         parse_mode="HTML")
    elif message.text == "Другая группа":
        sql_con = sqlite3.connect("Bot_db")
        cursor = sql_con.cursor()
        cursor.execute(
            """SELECT admission_year_name
                          FROM user_choice 
                          WHERE user_id = ?""", (message.chat.id, ))
        data = cursor.fetchone()
        cursor.close()
        sql_con.close()

        message.text = data[0]
        select_admission_year(message)
        return
    elif message.text == "Другой год":
        select_student_group(message)
        return
    elif message.text == "Другая программа":
        select_admission_year(message)
        return
    elif message.text == "Другая ступень":
        select_study_program_combination(message)
        return
    elif message.text == "Другое направление":
        start_handler(message)
        return
コード例 #4
0
def confirm_choice(message):
    from flask_app import bot, start_handler, main_keyboard_btn

    answer = ''

    if message.text == 'Все верно':
        sql_con = sl3.connect(const.path + 'Bot.db')
        cursor = sql_con.cursor()
        cursor.execute('''SELECT alias, student_group_name
                            FROM user_choice
                           WHERE user_id = ?''', (message.chat.id,))
        data = cursor.fetchone()
        alias = data[0]
        group_name = data[1]
        cursor.execute('''DELETE FROM banned_users
                                WHERE id_not_banned = ?''', (message.chat.id,))
        sql_con.commit()
        cursor.execute('''INSERT INTO banned_users (id_not_banned)
                               VALUES (?)''', (message.chat.id,))
        sql_con.commit()

        try:
            cursor.execute('''INSERT INTO user_data (id,
                                                     first_name,
                                                     last_name,
                                                     username,
                                                     alias,
                                                     group_name,
                                                     date_of_registrations)
                                   VALUES (?, ?, ?, ?, ?, ?, ?)''',
                           (message.chat.id,
                            message.chat.first_name,
                            message.chat.last_name,
                            message.chat.username,
                            alias, group_name,
                            str(datetime.now())[:-7],))
        except sl3.IntegrityError:
            sql_con.rollback()
            cursor.execute('''UPDATE user_data
                                 SET alias = ?, group_name = ?
                               WHERE id = ?''',
                           (alias, group_name, message.chat.id,))
        finally:
            sql_con.commit()
            cursor.execute('''DELETE FROM user_choice
                                    WHERE user_id = ?''',
                           (message.chat.id,))
            sql_con.commit()
            cursor.close()
            sql_con.close()

        answer = ('Главное меню\n\n'
                  '{0} – информация о боте\n'
                  '{1} – оценить бота\n'
                  '{2} – настройки\n'
                  '{3} – параметры уведомлений\n'
                  '{4} – расписание звонков'.format(const.emoji['info'],
                                                    const.emoji['star'],
                                                    const.emoji['settings'],
                                                    const.emoji['alarm_clock'],
                                                    const.emoji['bell']))

        bot.send_message(message.chat.id, answer,
                         reply_markup=main_keyboard_btn,
                         parse_mode='HTML')
    elif message.text == 'Другая группа':
        sql_con = sl3.connect(const.path + 'Bot.db')
        cursor = sql_con.cursor()
        cursor.execute('''SELECT admission_year_name
                            FROM user_choice
                           WHERE user_id = ?''', (message.chat.id,))
        data = cursor.fetchone()
        cursor.close()
        sql_con.close()

        message.text = data[0]
        select_admission_year(message)
        return
    elif message.text == 'Другой курс':
        select_student_group(message)
        return
    elif message.text == 'Другое направление':
        select_admission_year(message)
        return
    elif message.text == 'Другой способ регистрации':
        start_handler(message)
        return
    else:
        answer += ('Пожалуйста, проверь правильно ли ты всё указал и '
                   'подтверди свой выбор:')
        bot.send_message(message.chat.id, answer)
        set_next_step(message.chat.id, 'confirm_choice')