def ask(): data = request.get_json() if not data: return jsonify({"message": "No payload received"}), 400 if ("message" not in data): return jsonify({"message": "please specify a message"}), 400 if ("current_context" not in data): current_context = None else: current_context = data["current_context"] chatbot = Chatbot(sensitivity=0.4) chatbot.load() ''' chatbot.add_action("NutrizionistaCittaIntent", search_nutritionists) chatbot.add_action("DietistaCittaIntent", search_dietisti) chatbot.add_action("DietologoCittaIntent", search_dietologi) ''' answer = chatbot.ask(data["message"], current_context=current_context, return_proba=True) return jsonify({ "answer": str(answer[0]), "new_context": str(answer[1]), "probability": str(answer[2]) }), 200
def bot_ask(update: Update, context: CallbackContext): #print('Messaggio ricevuto:', update.effective_message.text) #print('Context attuale:', context.chat_data.get('context')) chatbot = Chatbot(sensitivity=0.4) chatbot.load() chatbot.add_action( "SiCodice", siCodice(update.effective_message.text, update.effective_user.id)) chatbot.add_error_string("NominativoCliente", [ "Il codice inserito è errato o non è nel database, ne provi un altro", "Il codice non è registrato, potrebbe essere errato" ]) chatbot.add_action( "NoCodEscalation", noCodEscalation(update.effective_message.text, update.effective_user.id)) chatbot.add_error_string("codCliente", [ "La mail inserita è errata o non è nel nostro database, ne provi un'altra", "La mail non è registrata, potrebbe essere errata" ]) response, new_context, _, intent = chatbot.ask( update.effective_message.text, current_context=context.chat_data.get('context'), return_proba=True) query_db(update.effective_user.id, intent, update.effective_message.text, response) if intent == 'Sconosciuto': if context.chat_data.get('intent_sconosciuto', 0) == 1: update.effective_message.reply_text( 'Attenda un secondo le passo l\'operatore...') context.bot.send_message( 1002946854, f'L\'utente {update.effective_user.mention_html()} ha bisogno di aiuto:\n\nQuesta è la sua conversazione: \n\n' f'{ultimeConvUtente(update.effective_user.id)}', parse_mode=ParseMode.HTML) else: context.chat_data['intent_sconosciuto'] = context.chat_data.get( 'intent_sconosciuto', 0) + 1 context.chat_data['context'] = new_context update.effective_message.reply_text(response) else: context.chat_data['context'] = new_context update.effective_message.reply_text(response) context.chat_data['intent_sconosciuto'] = 0
def ask(): data = request.get_json() if ("message" not in data): return jsonify({"message": "please specify a message"}), 400 chatbot = Chatbot() chatbot.load() answer = chatbot.ask(data["message"], return_proba=True) return jsonify({ "answer": str(answer[0]), "probability": str(answer[1]) }), 200