def start(self): while True: try: res = requests.get(os.path.join(self._bot_url, 'getUpdates'), timeout=5) if res.status_code != 200: logger.warn(res.text) if len(res.json()) == 0: sleep(0.1) continue for m in res.json(): logger.info(m) update = convai_api.ConvUpdate(m) if m['message']['text'].startswith('/start '): self._log_user('_start_or_begin_or_test_cmd', update) greet_user(self._bot, update.effective_chat.id) self._text = m['message']['text'][len('/start '):] self._get_qas() self._add_fsm_and_user(update, True) fsm = self._chat_fsm[update.effective_chat.id] fsm.start_convai() elif m['message']['text'] == '/end': self._log_user('_end_cmd', update) fsm = self._chat_fsm[update.effective_chat.id] fsm.return_to_init() elif m['message']['text'] == 'version': self._log_user('version', update) self._add_fsm_and_user(update) fsm = self._chat_fsm[update.effective_chat.id] fsm._send_message("Version is {}".format(version)) elif m['message']['text'].startswith('reset'): self._log_user('reset', update) self._add_fsm_and_user(update, True) fsm = self._chat_fsm[update.effective_chat.id] fsm.return_to_init() fsm.return_to_start() fsm._send_message("Hmm....") else: self._log_user('_echo_cmd', update) fsm = self._chat_fsm[update.effective_chat.id] fsm._last_user_message = update.message.text if not fsm._text: fsm._send_message( 'Text is not given. Please try to type /end and /test to reset the state and get text.' ) continue # Do we need this if else? if fsm.is_asked(): fsm.check_user_answer_on_asked() else: fsm.classify() except Exception as e: logger.exception(str(e)) sleep(0.1)
def start(self): while True: try: res = requests.get(os.path.join(self._bot_url, 'getUpdates'), timeout=5) if res.status_code != 200: logger.warn(res.text) if len(res.json()) == 0: sleep(0.1) continue for m in res.json(): logger.info(m) update = convai_api.ConvUpdate(m) if m['message']['text'].startswith('/start '): self._log_user('_start_or_begin_or_test_cmd', update) greet_user(self._bot, update.effective_chat.id) self._text = m['message']['text'][len('/start '):] self._get_qas() self._add_fsm_and_user(update, True) fsm = self._chat_fsm[update.effective_chat.id] fsm.start_convai() elif m['message']['text'] == '/end': self._log_user('_end_cmd', update) fsm = self._chat_fsm[update.effective_chat.id] fsm.return_to_init() elif m['message']['text'] == 'version': self._log_user('version', update) self._add_fsm_and_user(update) fsm = self._chat_fsm[update.effective_chat.id] fsm._send_message("Version is {}".format(version)) elif m['message']['text'].startswith('reset'): self._log_user('reset', update) self._add_fsm_and_user(update, True) fsm = self._chat_fsm[update.effective_chat.id] fsm.return_to_init() fsm.return_to_start() fsm._send_message("Hmm....") else: self._log_user('_echo_cmd', update) fsm = self._chat_fsm[update.effective_chat.id] fsm._last_user_message = update.message.text if not fsm._text: fsm._send_message('Text is not given. Please try to type /end and /test to reset the state and get text.') continue # Do we need this if else? if fsm.is_asked(): fsm.check_user_answer_on_asked() else: fsm.classify() except Exception as e: logger.exception(str(e)) sleep(0.1)
def _start_cmd(self, bot, update): self._log_user('_start_cmd', update) self._text_ind += 1 logger_bot.info("BOT[_start_cmd] text_id: {}".format(self._text_ind)) update.message.reply_text("\"{}\"".format(self._text())) greet_user(bot, update.effective_chat.id) self._add_fsm_and_user(update, True) fsm = self._users_fsm[update.effective_user.id] fsm.start()