def send_message(): request_data = request.get_json() user = request_data.get('user', '') message = request_data.get('message', '') chat = request_data.get('chat') '''create new message''' new_message = Message(message=message, chat=chat) new_message.user = user new_message.from_bot = 0 db_session.add(new_message) db_session.commit() """ TODO : get the response of the message from the model and create a new message """ responses = get_response_from_bot(message, model, bag, labels, question_dict) new_message = Message(message=responses, chat=chat) new_message.user = user #the responses are from chatbot new_message.from_bot = 1 db_session.add(new_message) db_session.commit() return jsonify({ "status": "success", "message": "message sent successfuly" })
def get_response(): request_data = request.get_json() user = request_data.get('user', '') message = request_data.get('message', '') chat = request_data.get('chat') lang, message_eng = lang_to_eng(message) print(lang, message_eng) '''create new message''' new_message = Message(message=message_eng, chat=chat) new_message.user = user new_message.from_bot = 0 db_session.add(new_message) db_session.commit() """ TODO : get the response of the message from the model and create a new message """ responses, similar, context = get_response_from_bot( message_eng, model, bag, labels, question_dict, 2, questions) #summarized = summarize(responses) if lang in SUPPORTED_LANGUAGES: responses = eng_to_lang(responses, lang) new_message = Message(message=responses, chat=chat) new_message.user = user #the responses are from chatbot new_message.from_bot = 1 db_session.add(new_message) db_session.commit() try: return jsonify({ "message": responses, "similar": similar, "context": context, "status": "success" }) except: return jsonify({ "status": "faliure", "message": "could not get response" })
def activate_chat(): request_data = request.get_json() user = request_data.get('user', '') #check if there is a chathead with this user flag = False chat = Chathead.query.filter(Chathead.user_id == user).first() if not chat: flag = True try: new_chat_head = Chathead() new_chat_head.user_id = user db_session.add(new_chat_head) db_session.commit() except: return jsonify({ "status": "error", "message": "chat creation failed" }) chat = Chathead.query.filter(Chathead.user_id == user).first() if flag: """ if new chat is formed enter a welcome message """ new_message = Message(message="Hey! I am a chatbot", chat=chat.id) new_message.user = user new_message.from_bot = 1 #the message is from bot db_session.add(new_message) db_session.commit() return jsonify({ "status": "success", "message": "chat creation successful", "chat_id": chat.id })