def _TextToText_VNPost_XacNhanGiaoHang(self, client_id, request_iterator): try: ending = False bot_vnpost = Bot("VNPost_XacNhanGiaoHang_Callout.json") status_success = voicebot_pb2.Status( code=VoiceBot.SUCCESS, message="Voicebot thinking ...") text_response = bot_vnpost.next_sentence("alo") resp = "" for token in text_response: resp += token + ". " text_response = resp.replace("..", ".") logging.info("{}\t_TextToText: {}".format(client_id, text_response)) yield status_success, "", True, text_response for status, text, final in request_iterator: if status.code == VoiceBot.ERROR: yield status, "" break if not final: yield status_success, text, False, "" else: if ending: return ## BOT HERE ## # text_bot = "ha ha buồn cười quá" text_ask = text text_response = bot_vnpost.next_sentence(text_ask) resp = "" for token in text_response: resp += token + ". " text_response = resp.replace("..", ".") ############## logging.info("{}\t_TextToText: {}".format( client_id, text_response)) if '_END_' in text_response: text_response = text_response.replace('_END_', '') yield status_success, text, True, text_response ending = True # return else: yield status_success, text, True, text_response except GeneratorExit: return except: status_error = voicebot_pb2.Status( code=VoiceBot.ERROR, message="NLU Service not working!") logging.error(traceback.format_exc()) yield status_error, "", True, ""
def test_run_ok(self): send_mock = Mock() api_mock = Mock() api_mock.messages.send = send_mock events = [] for input_text in self.IPUTUTS: event = deepcopy(self.RAW_EVENT) event['object']['message']['text'] = input_text events.append(VkBotMessageEvent(event)) long_poller_mock = Mock() long_poller_mock.listen = Mock(return_value=events) with patch('chatbot.bot.VkBotLongPoll', return_value=long_poller_mock): bot = Bot('', '') bot.api = api_mock bot.send_image = Mock() bot.run() assert send_mock.call_count == len(self.IPUTUTS) real_outputs = [] for call in send_mock.call_args_list: args, kwargs = call real_outputs.append(kwargs['message']) assert real_outputs == self.EXEPTED_OUTPUTS
def test_run(self): count = 5 obj = {} events = [obj] * count long_poller_mock = Mock(return_value=events) long_poller_listen_mock = Mock() long_poller_listen_mock.listen = long_poller_mock with patch('chatbot.bot.vk_api.VkApi'): with patch('chatbot.bot.VkBotLongPoll', return_value=long_poller_listen_mock): bot = Bot('', '') bot.on_event = Mock() bot.send_image = Mock() bot.run() bot.on_event.assert_called() bot.on_event.assert_any_call(obj) assert bot.on_event.call_count == count
app = Flask(__name__) args = { "files": { "vContext": "../data/twitter_out_model.tsv", "dialogs": "../data/OpenSubtitles.en", "whoosh_index": "../whoosh_index", "schema_index": "schema_index", "context_vec_index": "context_vec_index" }, "dim": 100, "context_limit": 10, "lang": u"en" } bot = Bot(args) bot_context = [] @app.route("/load", methods=['POST']) def load(): bot.preloadDB() return jsonify({"status":200}) @app.route("/talk", methods=['POST']) def talk(): global bot_context question = request.json['q'] result = bot.answer(question.lower()) bot_context.append(question) bot_context.append(result)