def ChatToBot_TongDai(self, request_iterator): for request in request_iterator: #### Process Chatbot HEREEEEEE #### text_ask = request.text text_response = "Tổng đài | " + text_ask ################################### yield chatbot_pb2.ChatBotResponse(status=chatbot_pb2.Status( code=ChatBot.SUCCESS, message="success"), text="1 | " + text_response) yield chatbot_pb2.ChatBotResponse(status=chatbot_pb2.Status( code=ChatBot.SUCCESS, message="success"), text="2 | " + text_response)
def ChatToBot(self, request_iterator, context): client_id = datetime.now().strftime("%Y%m%d%H%M%S") + "-" + str( uuid.uuid4())[:8] # Get config from first request request = next(request_iterator) # The first request must contain chatbot_config if request.chatbot_config is None: logging.error("{}\t{}".format( client_id, "First request must contain voicebot_config field!")) yield chatbot_pb2.ChatBotResponse(status=chatbot_pb2.Status( code=ChatBot.ERROR, message="First request must contain chatbot_config field!")) return # Check if bot are available bot_code = None for code, name in ChatBot.BOTS: if code == request.chatbot_config.bot_code: bot_code = code break if bot_code is None: logging.error("{}\t{}".format( client_id, "Unknown bot code {}".format(request.chatbot_config.bot_code))) yield chatbot_pb2.ChatBotResponse(status=chatbot_pb2.Status( code=ChatBot.ERROR, message="Unknown bot code {}".format( request.chatbot_config.bot_code))) return # Push request for coressponding bot logging.info("{}\tChat to {}".format(client_id, bot_code)) if bot_code == 'bot_duoc': chattobot_response_iterator = self.ChatToBot_Duoc(request_iterator) elif bot_code == 'bot_tongdai': chattobot_response_iterator = self.ChatToBot_TongDai( request_iterator) for chattobot_response in chattobot_response_iterator: yield chattobot_response logging.info("{}\tFinish chat!".format(client_id))
def ChatToBot_Duoc(self, request_iterator): bot_duoc = Bot() for request in request_iterator: #### Process Chatbot HEREEEEEE #### text_ask = request.text text_ask = bot_duoc.next_sentence(text_ask) resp = "" for text in text_ask: resp = resp + text + "." resp = resp.replace("..", ".") text_response = "Dược | " + resp ################################### yield chatbot_pb2.ChatBotResponse(status=chatbot_pb2.Status( code=ChatBot.SUCCESS, message="success"), text=text_response)