Example #1
0
def manage(message_data, ids_data, vk):
    message_text = message_data[0]
    from_id = message_data[1]

    if ids_data[from_id]['first_run']:
        vk.messages.send(
            user_id=from_id,
            message="Добро пожаловать в центр напоминаний!\n"
            "Напоминания помогут вам не забывать вовремя кормить кота!\n\n"
            "Список команд центра напоминаний:\n"
            '1. "Создать" — создать новое напоминание,\n'
            '2. "Удалить" — удаление имеющихся напоминаний,\n'
            '3. "Мои напоминания" — просмотр списка имеющихся напоминаний,\n'
            '4. "Выход" — выйти из Центра напоминаний.',
            random_id=random.randint(0, 2**64),
            keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())
        ids_data[from_id]['first_run'] = False
    else:
        if ids_data[from_id]['creation']:
            create_reminder(from_id, message_text, ids_data, vk)

        elif ids_data[from_id]['deleting_reminders']:
            deleting_reminders(ids_data, from_id, message_text, vk)

        elif 'создать' in message_text:
            create_reminder(from_id, message_text, ids_data, vk)
            ids_data[from_id]['creation'] = True

        elif 'мои' in message_text:
            vk.messages.send(user_id=from_id,
                             message=get_reminders(ids_data, from_id),
                             random_id=random.randint(0, 2**64),
                             keyboard=keyboards.new_keyboard(
                                 from_id, ids_data).get_keyboard())

        elif 'удал' in message_text:
            vk.messages.send(user_id=from_id,
                             message=get_reminders(ids_data, from_id),
                             random_id=random.randint(0, 2**64),
                             keyboard=keyboards.new_keyboard(
                                 from_id, ids_data).get_keyboard())
            if get_reminders(ids_data, from_id) != "У Вас нет напоминаний.":
                deleting_reminders(ids_data, from_id, message_text, vk)

        elif 'выход' in message_text:
            ids_data[from_id]["reminder_manage"] = False
            ids_data[from_id]['first_run'] = True
            vk.messages.send(user_id=from_id,
                             message="Вы вышли из центра напоминаний",
                             random_id=random.randint(0, 2**64),
                             keyboard=keyboards.new_keyboard(
                                 from_id, ids_data).get_keyboard())
Example #2
0
def remind(from_id, ids_data, vk):
    vk.messages.send(user_id=from_id,
                     message="Напоминание!\nВремя покормить кота!",
                     random_id=random.randint(0, 2**64),
                     keyboard=keyboards.new_keyboard(from_id,
                                                     ids_data).get_keyboard())
    print(f"sent to {from_id} at {datetime.datetime.now()}")
Example #3
0
def end(ids_data, from_id, vk):
    ids_data[from_id]['test_active'] = False
    ids_data[from_id]['test_question'] = 0
    vk.messages.send(user_id=from_id,
                     message=f"Тест закончен!\n"
                     f"Вы набрали {ids_data[from_id]['score']} баллов из 3!",
                     random_id=random.randint(0, 2**64),
                     keyboard=keyboards.new_keyboard(from_id,
                                                     ids_data).get_keyboard())
    if ids_data[from_id]['score'] == 3:
        user = vk.users.get(user_id=from_id, fields='sex')[0]
        if 'sex' in user:
            if user['sex'] == 2:
                vk.messages.send(user_id=from_id,
                                 message=f"Вы молодец!",
                                 random_id=random.randint(0, 2**64))
            elif user['sex'] == 1:
                vk.messages.send(user_id=from_id,
                                 message=f"Вы умница!",
                                 random_id=random.randint(0, 2**64))
        else:
            vk.messages.send(user_id=from_id,
                             message=f"Вы молодец!",
                             random_id=random.randint(0, 2**64))
    ids_data[from_id]['score'] = 0
Example #4
0
def ask(ids_data, from_id, vk, question):
    vk.messages.send(user_id=from_id,
                     message=question,
                     random_id=random.randint(0, 2**64),
                     keyboard=keyboards.new_keyboard(from_id,
                                                     ids_data).get_keyboard())
    ids_data[from_id]['test_question'] += 1
Example #5
0
def fallback(ids_data, from_id, vk, drop_items):
    for item in drop_items:
        ids_data[from_id][item] = False
    vk.messages.send(user_id=from_id,
                     message="Операция прервана",
                     random_id=random.randint(0, 2 ** 64),
                     keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())
Example #6
0
def deleting_reminders(ids_data, from_id, message_text, vk):
    try:
        if ids_data[from_id]['deleting_reminders']:
            if "выход" in message_text:
                fallback.fallback(ids_data, from_id, vk,
                                  ('deleting_reminders', ))
                return
            clear_line = ''
            for symbol in message_text:
                if symbol != ' ':
                    clear_line += symbol
            to_delete_list = clear_line.split(',')
            helper = 1
            for item in to_delete_list:
                del ids_data[from_id]['reminders'][int(item) - helper]
                helper += 1
            vk.messages.send(user_id=from_id,
                             message="Выбранныйе напоминания успешно удалены!",
                             random_id=random.randint(0, 2**64),
                             keyboard=keyboards.new_keyboard(
                                 from_id, ids_data).get_keyboard())
            ids_data[from_id]['deleting_reminders'] = False

        else:
            vk.messages.send(
                user_id=from_id,
                message=
                "Пожалуйста, через запятую введите номера напоминаний, которые вы хотите удалить.",
                random_id=random.randint(0, 2**64),
                keyboard=keyboards.new_keyboard(from_id,
                                                ids_data).get_keyboard())
            ids_data[from_id]['deleting_reminders'] = True

    except Exception:
        traceback.print_exc()
        vk.messages.send(
            user_id=from_id,
            message="Пожалуйста, введите корректное значение: "
            "номера напоминаний через запятую, которые вы хотите удалить.",
            random_id=random.randint(0, 2**64),
            keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())
Example #7
0
def translate(ids_data, from_id, vk, message_text):
    if not ids_data[from_id]['first_run']:
        try:
            if 'выход' in message_text:
                fallback.fallback(ids_data, from_id, vk, ('translation_mode',))
                ids_data[from_id]['first_run'] = True
                return None
            line = message_text.split(' ')
            cat_line = 'Перевод на кошачий: '
            for _ in range(len(line)):
                cat_line += random.choice(cat_dict) + ' '
            vk.messages.send(user_id=from_id,
                             message=cat_line,
                             random_id=random.randint(0, 2 ** 64),
                             keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())
        except Exception:
            traceback.print_exc()
            vk.messages.send(user_id=from_id,
                             message="Не удалось перевести сообщение.",
                             random_id=random.randint(0, 2 ** 64),
                             keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())
    else:
        ids_data[from_id]['first_run'] = False
Example #8
0
def create_reminder(from_id, message_text, ids_data, vk):
    if ids_data[from_id]['creation']:
        try:
            if ids_data[from_id]['getting_times']:
                times = get_times(message_text, ids_data, from_id)
                if times:
                    scheduler = BackgroundScheduler()
                    for item in times:
                        scheduler.add_job(func=remind,
                                          trigger="cron",
                                          day_of_week='mon-sun',
                                          hour=item[:2],
                                          minute=item[3:],
                                          args=(from_id, ids_data, vk))
                        ids_data[from_id]['reminders'].append(
                            (item[:2], item[3:]))
                    scheduler.start()
                    vk.messages.send(user_id=from_id,
                                     message="Напоминание успешно создано!",
                                     random_id=random.randint(0, 2**64),
                                     keyboard=keyboards.new_keyboard(
                                         from_id, ids_data).get_keyboard())
                    ids_data[from_id]['creation'] = False
                    ids_data[from_id]['getting_times'] = False

                elif "выход" in message_text:
                    fallback.fallback(ids_data, from_id, vk,
                                      ("creation", "getting_times"))

                else:
                    vk.messages.send(
                        user_id=from_id,
                        message=
                        'Пожалуйста, введите корректное значение для каждого кормления в формате '
                        '"hh-mm" через запятую.',
                        random_id=random.randint(0, 2**64),
                        keyboard=keyboards.new_keyboard(
                            from_id, ids_data).get_keyboard())

            elif "выход" in message_text:
                fallback.fallback(ids_data, from_id, vk, ("creation", ))

            else:
                if 0 < int(message_text) <= 9:
                    ids_data[from_id]['amount_of_feeds'] = int(message_text)
                    ids_data[from_id]['getting_times'] = True
                    vk.messages.send(
                        user_id=from_id,
                        message=
                        'Введите время для каждого кормления в формате "hh-mm" через запятую.',
                        random_id=random.randint(0, 2**64),
                        keyboard=keyboards.new_keyboard(
                            from_id, ids_data).get_keyboard())

                else:
                    vk.messages.send(
                        user_id=from_id,
                        message=
                        "Пожалуйста, введите корректное значение (Рекомендуется вводить "
                        "значение от 1 до 9).",
                        random_id=random.randint(0, 2**64),
                        keyboard=keyboards.new_keyboard(
                            from_id, ids_data).get_keyboard())

        except Exception:
            traceback.print_exc()
            vk.messages.send(
                user_id=from_id,
                message="Пожалуйста, введите корректное значение.",
                random_id=random.randint(0, 2**64),
                keyboard=keyboards.new_keyboard(from_id,
                                                ids_data).get_keyboard())
    else:
        vk.messages.send(
            user_id=from_id,
            message="Сколько раз в день вы кормите своего кота/кошку?\n"
            "\n(Чтобы узнать оптимальное количество раз, посоветуйтесь с ветеринаром. "
            "Также, данная информация указывается на упаковке кормов). ",
            random_id=random.randint(0, 2**64),
            keyboard=keyboards.new_keyboard(from_id, ids_data).get_keyboard())