:D
This commit is contained in:
42
bot.py
42
bot.py
@ -18,8 +18,11 @@ tests = db.tests
|
|||||||
users = db.users
|
users = db.users
|
||||||
|
|
||||||
# Funciones de utilidad
|
# Funciones de utilidad
|
||||||
|
def find_user(cid):
|
||||||
|
return users.find_one(str(cid))
|
||||||
|
|
||||||
def is_user(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):
|
def add_user(cid):
|
||||||
users.insert_one({
|
users.insert_one({
|
||||||
@ -28,7 +31,16 @@ def add_user(cid):
|
|||||||
"current_tests": []
|
"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():
|
def restart_process():
|
||||||
|
global client
|
||||||
|
client.close()
|
||||||
_thread.interrupt_main()
|
_thread.interrupt_main()
|
||||||
|
|
||||||
def is_admin(cid):
|
def is_admin(cid):
|
||||||
@ -44,11 +56,14 @@ def find_question(test, question):
|
|||||||
return questions['preguntas'][0]
|
return questions['preguntas'][0]
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
def check_answer(question, answer):
|
||||||
|
return '✅ Correcto' if question['respuestas'][int(answer)]['correcta'] else '❌ Error'
|
||||||
|
|
||||||
def generate_tests_keyboard():
|
def generate_tests_keyboard():
|
||||||
keyboard = types.InlineKeyboardMarkup(row_width=4)
|
keyboard = types.InlineKeyboardMarkup(row_width=4)
|
||||||
buttons = []
|
buttons = []
|
||||||
for test in tests.find({},{'test':1, '_id':0}):
|
for i in range(1,33):
|
||||||
buttons.append(types.InlineKeyboardButton('Test {}'.format(test['test']-100), callback_data='test {}'.format(test['test'])))
|
buttons.append(types.InlineKeyboardButton('Test {}'.format(i),callback_data='test {}'.format(100+i)))
|
||||||
for chunk in split(buttons, 4):
|
for chunk in split(buttons, 4):
|
||||||
keyboard.add(*chunk)
|
keyboard.add(*chunk)
|
||||||
return keyboard
|
return keyboard
|
||||||
@ -60,7 +75,7 @@ def generate_test_keyboard(question, test):
|
|||||||
next_prev_buttons = []
|
next_prev_buttons = []
|
||||||
if question.get('pregunta') == 1:
|
if question.get('pregunta') == 1:
|
||||||
next_prev_buttons.append(types.InlineKeyboardButton('⏩', callback_data='pregunta {} {} +'.format(test, question['pregunta'])))
|
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'])))
|
next_prev_buttons.append(types.InlineKeyboardButton('⏪', callback_data='pregunta {} {} -'.format(test, question['pregunta'])))
|
||||||
else:
|
else:
|
||||||
next_prev_buttons.append(types.InlineKeyboardButton('⏪', callback_data='pregunta {} {} -'.format(test, question['pregunta'])))
|
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)
|
keyboard = generate_test_keyboard(question, test)
|
||||||
txt = "Test: {}\nPregunta: {}\n\n{}"
|
txt = "Test: {}\nPregunta: {}\n\n{}"
|
||||||
bot.send_photo(cid, question.get('img'), caption=txt.format(test, 1, question['encabezado']), reply_markup=keyboard)
|
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.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
|
cid = call.message.chat.id
|
||||||
mid = call.message.id
|
mid = call.message.id
|
||||||
_, test, q, option = call.data.split()
|
_, test, q, answer = call.data.split()
|
||||||
question = find_question(test, int(q)+1)
|
question = find_question(test, q)
|
||||||
keyboard = generate_test_keyboard(question, test)
|
answer_reply = check_answer(question, answer)
|
||||||
txt = "Test: {}\nPregunta: {}\n\n{}"
|
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('-')))
|
@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):
|
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")
|
bot.answer_callback_query(call.id, "No se pudo cargar la pregunta")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Iniciar bot
|
# Iniciar bot
|
||||||
bot.send_message(admin, "Reiniciado")
|
bot.send_message(admin, "Reiniciado")
|
||||||
bot.polling(skip_pending=True)
|
bot.polling(skip_pending=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user