Пример #1
0
def handle_habit_report(bot, update):
    check = Check.get(update.cmd_args['check_id'])
    check.status = update.cmd_args['status']
    check.save()

    habit = Habit.get(check.habit_id)

    user = User.get(update.user_id)

    if update.cmd_args['status'] == CheckStatus.SUCCESS.name:
        bot.send_sticker(update.user_id, random.choice(success_stickers))
        bot.send_message(
            update.user_id,
            '+1 очко' if user.language_code == 'ru' else '+1 point')
        user.score += 1  # TODO Заменить на последовательность
        user.save()
    else:
        bot.send_sticker(update.user_id, random.choice(fail_stickers))
        bot.send_message(
            update.user_id,
            '-1 очко' if user.language_code == 'ru' else '-1 point')
        user.score -= 1  # TODO Заменить на последовательность
        user.save()

    if not habit.get_remaining_checks():
        suggest_new_habit(bot, user, habit)
Пример #2
0
def handle_check_query(call):
    data = ast.literal_eval(call.data.split('/')[1])

    check = Check.get(data['check_id'])
    check.status = data['status']
    check.save()

    habit = Habit.get(check.habit_id)

    user = User.get(call.message.chat.id)

    called_button_label = call.message.json['reply_markup']['inline_keyboard'][0][0]['text'] if \
        data['status'] == CheckStatus.SUCCESS.name else \
        call.message.json['reply_markup']['inline_keyboard'][0][1]['text']

    try:
        bot.edit_message_text(
            chat_id=call.message.chat.id,
            text=call.message.text,
            message_id=call.message.message_id,
            reply_markup=markups.get_check_result_inline_markup(
                called_button_label),
            parse_mode='HTML')
    except:
        pass

    if data['status'] == CheckStatus.SUCCESS.name:
        user.score += habit.fine
        user.save()

        ru_text = f'{random.choice(ru_success_phrases)}\n\n*+{habit.fine} очков*'
        en_text = f'{random.choice(en_success_phrases)}\n\n*+{habit.fine} points*'
        text = ru_text if user.language_code == 'ru' else en_text

        try:
            bot.send_message(call.message.chat.id, text, parse_mode='Markdown')
            bot.send_sticker(call.message.chat.id,
                             random.choice(success_stickers))
        except:
            pass

        if habit.judge:
            judge = User.get(habit.judge)

            ru_text_judge = f'{get_user_naming(user, "Твой друг")} выполнил обещание *{habit.label}*'
            en_text_judge = f'{get_user_naming(user, "Your friend")} fulfilled the promise *{habit.label}*'
            text_judge = ru_text_judge if judge.language_code == 'ru' else en_text_judge

            try:
                bot.send_message(judge.id, text_judge, parse_mode='Markdown')
            except Exception:
                pass
    else:
        if habit.judge:
            user_violations_with_judge(user.id, habit.judge)
        else:
            try:
                bot.send_sticker(call.message.chat.id,
                                 random.choice(fail_stickers))
            except:
                pass
            user_violations(call.message)

    suggest_new_habit(user, habit)