def send_location(message): user = USERS[message.from_user.username] user["context"] = context_tree if (message.location == None): if (message.text == "Cancelar"): defect = user["inspection"].pop_defect() print(user["username"], "pop defect: {}".format(defect.get_flow())) show_screen(message) else: markup = types.ReplyKeyboardMarkup(resize_keyboard=1, one_time_keyboard=True) item = types.KeyboardButton("Cancelar") markup.row(item) msg = bot.send_message(message.chat.id, "Por favor, envie sua localização.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, send_location) else: location = message.location user["inspection"].set_latitude(location.latitude) user["inspection"].set_longitude(location.longitude) msg = bot.send_message(message.chat.id, "Você está em: {}".format(location)) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) send_local_photo(msg)
def process_inspec_end(message): username = message.from_user.username user = USERS[username] if message.text == 'Sim': user_inspec = user["inspection"] if user_inspec.get_defects() != []: inspec_json = user_inspec.to_json() utils.save_json("data/inspecs/inspec_{}.json".format( user_inspec.get_code()), content=inspec_json) markup = types.ReplyKeyboardMarkup(resize_keyboard=1) itembtn1 = types.KeyboardButton('OK') markup.row(itembtn1) report = user_inspec.generate_report() msg = bot.reply_to( message, "Inspeção finalizada. Seu código de inspeção é:") bot.send_message(message.chat.id, user_inspec.get_code()) msg = bot.send_message(message.chat.id, report, reply_markup=markup) msg.text = 'inicio' bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, init_service) del USERS[username] else: markup = types.ReplyKeyboardMarkup(resize_keyboard=1) itembtn1 = types.KeyboardButton('Registrar novo defeito') markup.row(itembtn1) msg = bot.reply_to( message, "Por favor, adicione pelo menos um defeito antes de finalizar a inspeção. Caso deseje, pode Cancelar a inspeção também.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, process_inspec_end) elif message.text == 'Não, registrar novo defeito' or message.text == 'Registrar novo defeito': user = USERS[message.from_user.username] user["inspection"].add_defect() show_screen(message) else: markup = types.ReplyKeyboardMarkup(resize_keyboard=1) itembtn1 = types.KeyboardButton('Registrar novo defeito') markup.row(itembtn1) msg = bot.reply_to(message, "Ops! algo deu errado.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, process_inspec_end)
def check_context(message): text = message.text current_context = USERS[message.from_user.username]["context"] if utils.CONDITION_MAP["in_context"](text, current_context): next_context = utils.get_child_by_content(text, current_context) # Se nó de voltar, atualizar o estado do usuário para o contexto anterior if utils.CONDITION_MAP["is_back_node"](next_context): prev_context = current_context.get_parent() # TODO: undo user step user = USERS[message.from_user.username] user_inspec = user["inspection"] step = user_inspec.pop_step() print(user["username"], "pop step: {}".format(step)) user["context"] = prev_context bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) show_screen(message) elif utils.CONDITION_MAP["is_cancel_node"](next_context): msg = bot.send_message(message.chat.id, "Inspeção cancelada") # TODO: cancel inspec USERS[message.from_user.username]["context"] = context_tree bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) send_welcome(message) elif utils.CONDITION_MAP["is_finish_node"](next_context): USERS[message.from_user.username]["context"] = context_tree bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) check_inspec_end(message) elif utils.CONDITION_MAP["is_leaf_node"](next_context): user = USERS[message.from_user.username] user["inspection"].add_step(text) # TODO: save route and checks for a new route or a finishing inspec user["context"] = context_tree bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) send_location(message) else: # Está indo para o próximo nó de contexto user = USERS[message.from_user.username] user_inspec = user["inspection"] user_inspec.add_step(text) user["context"] = next_context bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) show_screen(message) else: msg = bot.send_message(message.chat.id, "Não entendi.") show_screen(message)
def decision_inspec(message): text = message.text chat_id = message.chat.id first_name = message.from_user.first_name last_name = message.from_user.last_name username = message.from_user.username if (text == "Iniciar inspeção"): inspec_code = utils.generate_insp_code() USERS[username] = { "context": context_tree, "inspection": Inspection(code=inspec_code, trafo="Sem placa", latitude=0, longitude=0), "first_name": first_name, "last_name": last_name, "username": username } msg = bot.send_message(chat_id, "ok, iniciando inspeção.\nSeu código é:") msg = bot.send_message(chat_id, "{}".format(inspec_code)) init_inspec(message) elif (text == "Continuar inspeção"): # TODO: Criar o fluxo de continuar inspeção USERS[username] = { "context": context_tree, "inspection": None, "first_name": first_name, "last_name": last_name, "username": username } msg = bot.send_message( chat_id, "ok, continuando inspeção. Digite o código da inspeção que deseja continuar" ) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, continue_inspec) else: msg = bot.send_message(chat_id, "ops, selecione uma opção válida.") bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(chat_id) bot.register_next_step_handler(msg, init_service)
def send_local_photo(message): markup = types.ReplyKeyboardMarkup(resize_keyboard=1, row_width=1) itembtn1 = types.KeyboardButton('Concluir') markup.row(itembtn1) msg = bot.send_message( message.chat.id, "Envie as fotos (uma por vez) e selecione a opção concluir quando terminar.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, confirm_send_local_photo)
def show_screen(message): """ Apresenta estado atual da árvore de contexto """ current_context = USERS[message.from_user.username]["context"] markup = utils.generate_keyboard_markup(current_context) msg = bot.send_message(message.chat.id, "Selecione a opção desejada.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, check_context)
def init_inspec(message): chat_id = message.chat.id markup = types.ReplyKeyboardMarkup(resize_keyboard=1, one_time_keyboard=True) itembtn1 = types.KeyboardButton('Sem placa') markup.row(itembtn1) msg = bot.send_message(chat_id, "Informe a placa do trafo inspecionado.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, confirm_placa)
def init_service(message): markup = types.ReplyKeyboardMarkup(resize_keyboard=1, one_time_keyboard=True) itembtn1 = types.KeyboardButton('Iniciar inspeção') itembtn2 = types.KeyboardButton('Continuar inspeção') markup.row(itembtn1) markup.row(itembtn2) msg = bot.send_message(message.chat.id, "Selecione a opção desejada.", reply_markup=markup) bot.clear_reply_handlers(message) bot.clear_step_handler_by_chat_id(message.chat.id) bot.register_next_step_handler(msg, decision_inspec)