:D
This commit is contained in:
42
bot.py
42
bot.py
@ -18,8 +18,11 @@ tests = db.tests
|
||||
users = db.users
|
||||
|
||||
# Funciones de utilidad
|
||||
def find_user(cid):
|
||||
return users.find_one(str(cid))
|
||||
|
||||
def is_user(cid):
|
||||
return users.find_one(str(cid)) is not None
|
||||
return find_user(cid) is not None
|
||||
|
||||
def add_user(cid):
|
||||
users.insert_one({
|
||||
@ -28,7 +31,16 @@ def add_user(cid):
|
||||
"current_tests": []
|
||||
})
|
||||
|
||||
def add_test(cid, test_number):
|
||||
user = find_user(cid)
|
||||
if user and test_number not in [x['test'] for x in user['current_tests']]:
|
||||
test = tests.find_one({'test': int(test_number)})
|
||||
users.update_one({'_id':str(cid)},{'$push': {'current_tests': test}})
|
||||
|
||||
|
||||
def restart_process():
|
||||
global client
|
||||
client.close()
|
||||
_thread.interrupt_main()
|
||||
|
||||
def is_admin(cid):
|
||||
@ -44,11 +56,14 @@ def find_question(test, question):
|
||||
return questions['preguntas'][0]
|
||||
return {}
|
||||
|
||||
def check_answer(question, answer):
|
||||
return '✅ Correcto' if question['respuestas'][int(answer)]['correcta'] else '❌ Error'
|
||||
|
||||
def generate_tests_keyboard():
|
||||
keyboard = types.InlineKeyboardMarkup(row_width=4)
|
||||
buttons = []
|
||||
for test in tests.find({},{'test':1, '_id':0}):
|
||||
buttons.append(types.InlineKeyboardButton('Test {}'.format(test['test']-100), callback_data='test {}'.format(test['test'])))
|
||||
for i in range(1,33):
|
||||
buttons.append(types.InlineKeyboardButton('Test {}'.format(i),callback_data='test {}'.format(100+i)))
|
||||
for chunk in split(buttons, 4):
|
||||
keyboard.add(*chunk)
|
||||
return keyboard
|
||||
@ -60,7 +75,7 @@ def generate_test_keyboard(question, test):
|
||||
next_prev_buttons = []
|
||||
if question.get('pregunta') == 1:
|
||||
next_prev_buttons.append(types.InlineKeyboardButton('⏩', callback_data='pregunta {} {} +'.format(test, question['pregunta'])))
|
||||
elif question.get('pregunta') == 20:
|
||||
elif (question.get('pregunta') == 20 and str(test) != '132') or (question.get('pregunta') == 17 and str(test) == '132'):
|
||||
next_prev_buttons.append(types.InlineKeyboardButton('⏪', callback_data='pregunta {} {} -'.format(test, question['pregunta'])))
|
||||
else:
|
||||
next_prev_buttons.append(types.InlineKeyboardButton('⏪', callback_data='pregunta {} {} -'.format(test, question['pregunta'])))
|
||||
@ -96,19 +111,18 @@ def test_callback_handler(call):
|
||||
keyboard = generate_test_keyboard(question, test)
|
||||
txt = "Test: {}\nPregunta: {}\n\n{}"
|
||||
bot.send_photo(cid, question.get('img'), caption=txt.format(test, 1, question['encabezado']), reply_markup=keyboard)
|
||||
# TODO: Añadir el test a current_tests del usuario
|
||||
add_test(cid, test)
|
||||
bot.answer_callback_query(call.id)
|
||||
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('pregunta') and (call.data.endswith('+') or call.data.endswith('-')))
|
||||
def question_answer_callback_handler(call):
|
||||
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('pregunta') and call.data[-1] in [str(x) for x in range(0,4)])
|
||||
def select_option_callback_handler(call):
|
||||
cid = call.message.chat.id
|
||||
mid = call.message.id
|
||||
_, test, q, option = call.data.split()
|
||||
question = find_question(test, int(q)+1)
|
||||
keyboard = generate_test_keyboard(question, test)
|
||||
txt = "Test: {}\nPregunta: {}\n\n{}"
|
||||
|
||||
|
||||
_, test, q, answer = call.data.split()
|
||||
question = find_question(test, q)
|
||||
answer_reply = check_answer(question, answer)
|
||||
bot.answer_callback_query(call.id, answer_reply)
|
||||
|
||||
@bot.callback_query_handler(func=lambda call: call.data.startswith('pregunta') and (call.data.endswith('+') or call.data.endswith('-')))
|
||||
def question_pager_callback_handler(call):
|
||||
@ -125,8 +139,6 @@ def question_pager_callback_handler(call):
|
||||
bot.answer_callback_query(call.id, "No se pudo cargar la pregunta")
|
||||
|
||||
|
||||
|
||||
|
||||
# Iniciar bot
|
||||
bot.send_message(admin, "Reiniciado")
|
||||
bot.polling(skip_pending=True)
|
||||
|
||||
Reference in New Issue
Block a user