added range and validate number question type

This commit is contained in:
Pavel 2024-09-05 18:06:39 +03:00
parent 18fab9828e
commit 1423c78af8
2 changed files with 68 additions and 1 deletions

BIN
main.exe Normal file

Binary file not shown.

@ -155,6 +155,46 @@ fn (mut b TgBot) message_handler(update vgram.Update) {
b.render_message(current_state.id,chat_id_str,previous_question)
return
}
if previous_question.question_type == models.question_type_number.str() {
content := json.decode(models.QuizQuestionNumber, previous_question.content) or {
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при обработке вопроса типа number')
return
}
mut message_text := update.message.text
if !message_text.is_int() {
b.bot.send_message(
chat_id: chat_id_str,
text: 'Допустимо вводить только число'
)
return
}
mut min := 0
mut max := 0
number_value := message_text.int()
if content.range.contains('—') {
range_arr := content.range.split('—')
if range_arr.len == 2 {
min = range_arr[0].int()
max = range_arr[1].int()
}
}
if max > 0{
if number_value < min || number_value > max {
b.bot.send_message(
chat_id: chat_id_str,
text: 'Введённое число вне заданного диапазона (${min} - ${max})'
)
return
}
}
// todo отправка в answerer
println(number_value)
b.render_message(current_state.id, chat_id_str, previous_question)
return
}
}
}
@ -676,10 +716,37 @@ fn (mut b TgBot)render_date_question(state_id i64,chat_id_str string, question m
}
fn (mut b TgBot)render_number_question(state_id i64,chat_id_str string, question models.QuizQuestion, content models.QuizQuestionNumber){
fn (mut b TgBot) render_number_question(state_id i64, chat_id_str string, question models.QuizQuestion, content models.QuizQuestionNumber) {
mut range := ''
if content.range.contains('—') {
range_arr := content.range.split('—')
if range_arr.len == 2 {
range = ' (вводить числа от ${range_arr[0]} до ${range_arr[1]})'
}
}
if content.choose_range {
b.bot.send_message(
chat_id: chat_id_str,
text: '*$question.title*\n\n${question.description}${range}',
parse_mode: 'Markdown'
)
} else {
b.bot.send_message(
chat_id: chat_id_str,
text: '*$question.title*\n\n${question.description}',
parse_mode: 'Markdown'
)
}
b.repo.update_state(state_id, question.id) or {
b.bot.send_message(chat_id: chat_id_str, text: 'Ошибка при обновлении состояния респондента')
return
}
}
fn (mut b TgBot)render_file_question(state_id i64,chat_id_str string, question models.QuizQuestion, content models.QuizQuestionFile){
}