def __init__(self, bot, user_id): super().__init__(bot, "egrul", user_id) self._behaviours.update({ States.INIT: self._handle_init, States.QUESTIONING: self._handle_questioning }) self._cm = ConstantsManager(subcategory="egrul_filler")
class EgrulFiller(FormFiller): def __init__(self, bot, user_id): super().__init__(bot, "egrul", user_id) self._behaviours.update({ States.INIT: self._handle_init, States.QUESTIONING: self._handle_questioning }) self._cm = ConstantsManager(subcategory="egrul_filler") def _handle_init(self, update): self._bot.send_message(self._user_id, self._cm.get_string("init")) form_id, sheet_id, field_id = self._questions[self._current_question] self._bot.send_message( self._user_id, self._cm.get_string("field_request") % (form_id, sheet_id, field_id)) self._current_question += 1 def _handle_questioning(self, update): form_id, sheet_id, field_id = self._questions[self._current_question] self._collected_info[form_id][sheet_id][field_id] = update.message.text form_id, sheet_id, field_id = self._questions[self._current_question] self._bot.send_message( self._user_id, self._cm.get_string("field_request") % (form_id, sheet_id, field_id)) self._current_question += 1
def __init__(self, bot, user_id): super().__init__(bot, user_id) self._behaviours.update({ _EgrulIndirectStates.CHOOSING_CHANGES: self._handle_choice, _EgrulIndirectStates.PROCESSING_CHANGES: self._handle_process_changes }) self._chosen_changes = [None] * len(self.NAMES) self._cm = ConstantsManager(subcategory="egrul_indirect_establisher") self._schema.update({ "P13001": { "стр.1": {}, "стр.21_ЛистМ_с.1": {}, "стр.22_ЛистМ_с.2": {}, "стр.23_ЛистМ_с.3": {} } })
def __init__(self, bot, submission_id, user_id, xl_engine): self._bot = bot self._form_id = submission_id self._user_id = user_id self._xl_engine = xl_engine self._cm = ConstantsManager(subcategory="form_manager") self._collected_info = None self._behaviours = {SubmissionStages.INIT: self._handle_init, SubmissionStages.Q_FILL_OR_ESTABLISH: self._handle_fill_or_establish, # must be overridden SubmissionStages.ESTABLISHMENT: self._handle_establishment, SubmissionStages.FILLING: self._handle_filling, SubmissionStages.CLEAR: self._handle_clear} self._establisher = None self._filler = None self._state = SubmissionStages.INIT
def __init__(self, xl_engine, updater): self._xl_engine = xl_engine self._updater = updater self._logger = LoggingServer.getInstance("luthor") self._cm = ConstantsManager(subcategory="luthor") self._available_submission_classes = { "Изменения в ЕГРЮЛ": EgrulManager, "Заявление на Шенген": EgrulManager } self._submissions_regexp = "^(" + "|".join( self._available_submission_classes.keys()) + ")$" self.add_handlers() self._submission_managers = {} self._active_managers = {} Luthor.USER_FORM_MANAGERS_STUB = { form_id: None for form_id in self._available_submission_classes }
class SubmissionManager(StateMachine): def __init__(self, bot, submission_id, user_id, xl_engine): self._bot = bot self._form_id = submission_id self._user_id = user_id self._xl_engine = xl_engine self._cm = ConstantsManager(subcategory="form_manager") self._collected_info = None self._behaviours = {SubmissionStages.INIT: self._handle_init, SubmissionStages.Q_FILL_OR_ESTABLISH: self._handle_fill_or_establish, # must be overridden SubmissionStages.ESTABLISHMENT: self._handle_establishment, SubmissionStages.FILLING: self._handle_filling, SubmissionStages.CLEAR: self._handle_clear} self._establisher = None self._filler = None self._state = SubmissionStages.INIT def _handle_init(self, update): reply_keyboard = [[self._cm.get_string("help_submit")], [self._cm.get_string("help_fill")]] update.message.reply_text(self._cm.get_string("submission_help"), reply_markup=ReplyKeyboardMarkup( reply_keyboard, one_time_keyboard=True)) self._state = SubmissionStages.Q_FILL_OR_ESTABLISH def _handle_fill_or_establish(self, update): pass # has to be overridden def _handle_establishment(self, update): schema = self._establisher.handle_update(update) if schema is not None: self._schema = schema f = Formatter() self._bot.send_message(self._user_id, 'DEBUG Established schema:\n```%s```' % f(self._schema), parse_mode=ParseMode.MARKDOWN) self._state = SubmissionStages.FILLING self._filler.init_collected_info(self._schema) # TODO: Do we need both schema and collected info? return self.handle_update(update) def _handle_filling(self, update): result = self._filler.handle_update(update) if result: return self._finalize() def _finalize(self): try: return self._xl_engine.generate(self._user_id, self._form_id, self._collected_info) except ValueError as e: return str(e) def _handle_clear(self, update): return ConversationHandler.END
class Luthor: INSTANCE = None def __init__(self, xl_engine, updater): self._xl_engine = xl_engine self._updater = updater self._logger = LoggingServer.getInstance("luthor") self._cm = ConstantsManager(subcategory="luthor") self._available_submission_classes = { "Изменения в ЕГРЮЛ": EgrulManager, "Заявление на Шенген": EgrulManager } self._submissions_regexp = "^(" + "|".join( self._available_submission_classes.keys()) + ")$" self.add_handlers() self._submission_managers = {} self._active_managers = {} Luthor.USER_FORM_MANAGERS_STUB = { form_id: None for form_id in self._available_submission_classes } def add_handlers(self): conv_handler = ConversationHandler( entry_points=[CommandHandler('start', self.on_start)], states={ LuthorStates.CHOOSING_SUBMISSION: [ RegexHandler(self._submissions_regexp, self.on_choosing_submission) ], LuthorStates.BUILDING_SUBMISSION: [ MessageHandler(Filters.text, self.on_building_submission_message), CallbackQueryHandler(self.on_building_submission_callback) ], }, fallbacks=[CommandHandler('cancel', self.on_cancel)]) self._updater.dispatcher.add_handler(conv_handler) self._updater.dispatcher.add_error_handler(self.on_error) def launch(self): self._updater.start_polling() def on_start(self, bot, update): reply_keyboard = [[ key for key in self._available_submission_classes.keys() ]] update.message.reply_text(self._cm.get_string("hi"), reply_markup=ReplyKeyboardMarkup( reply_keyboard, one_time_keyboard=True)) self._submission_managers[update.message.from_user. id] = Luthor.USER_FORM_MANAGERS_STUB.copy() return LuthorStates.CHOOSING_SUBMISSION def on_choosing_submission(self, bot, update): # submission_id = update.message.text user_id = update.message.from_user.id if self._submission_managers[user_id][submission_id] is None: manager = self._available_submission_classes[submission_id]( self._updater.bot, user_id, self._xl_engine) self._submission_managers[user_id][submission_id] = manager else: manager = self._submission_managers[user_id][submission_id] self._active_managers[user_id] = manager manager.handle_update(update) return LuthorStates.BUILDING_SUBMISSION def on_building_submission_message(self, bot, update): user_id = update.message.from_user.id return self._on_building_submission(user_id, update) def on_building_submission_callback(self, bot, update): user_id = update._effective_user.id return self._on_building_submission(user_id, update) def _on_building_submission(self, user_id, update): manager = self._active_managers[user_id] result = manager.handle_update(update) if result is None: return LuthorStates.BUILDING_SUBMISSION elif isinstance(result, str): self._bot.send_message(user_id, result) return ConversationHandler.END else: self._updater.bot.send_message(user_id, "Ваши документы:") self._updater.bot.send_document(user_id, result, filename=manager.get_form_id() + ".xls") return ConversationHandler.END def on_cancel(self, bot, update): update.message.reply_text("Cancelled") return ConversationHandler.END def on_error(self, bot, update, error): print(type(error), error)
class EgrulIndirectEstablisher(FormEstablisher): NAMES = { _Changes.NAME: "Название", _Changes.CHARTER_TO_CIVIL_CODE: "Устав (для соответствия с новой редакцией ГК)", _Changes.OTHER_CHARTER: "Устав (иные изменения)", _Changes.OKVAD: "ОКВЭД", _Changes.LEGAL_ADDRESS: "Юридический адрес", _Changes.SHARE_CAPITAL: "Уставный капитал", _Changes.PARTICIPANTS_ONLY: "Состав участников без изменения уставного капитала", _Changes.DIRECTOR: "Директора организации", _Changes.EGRUL_ERRORS: "Ошибки в ЕГРЮЛ" } ESTABLISHERS = { _Changes.NAME: EgrulNameEstablisher, _Changes.CHARTER_TO_CIVIL_CODE: EgrulCharterToCCEstablisher, _Changes.OTHER_CHARTER: EgrulNameEstablisher, _Changes.OKVAD: EgrulNameEstablisher, _Changes.LEGAL_ADDRESS: EgrulNameEstablisher, _Changes.SHARE_CAPITAL: EgrulNameEstablisher, _Changes.PARTICIPANTS_ONLY: EgrulNameEstablisher, _Changes.DIRECTOR: EgrulNameEstablisher, _Changes.EGRUL_ERRORS: EgrulNameEstablisher } def __init__(self, bot, user_id): super().__init__(bot, user_id) self._behaviours.update({ _EgrulIndirectStates.CHOOSING_CHANGES: self._handle_choice, _EgrulIndirectStates.PROCESSING_CHANGES: self._handle_process_changes }) self._chosen_changes = [None] * len(self.NAMES) self._cm = ConstantsManager(subcategory="egrul_indirect_establisher") self._schema.update({ "P13001": { "стр.1": {}, "стр.21_ЛистМ_с.1": {}, "стр.22_ЛистМ_с.2": {}, "стр.23_ЛистМ_с.3": {} } }) def _handle_init(self, update): self._choice_keyboard = [[ InlineKeyboardButton(self.NAMES[key], callback_data=key.value) ] for key in self.NAMES.keys()] self._choice_keyboard.append( [InlineKeyboardButton("((Закончить))", callback_data=-1)]) update.message.reply_text(self._cm.get_string("init"), reply_markup=InlineKeyboardMarkup( self._choice_keyboard[:-1])) self._state = _EgrulIndirectStates.CHOOSING_CHANGES def _handle_choice(self, update): try: callback_data = int(update.callback_query.data) except AttributeError: self._bot.send_message(self._user_id, self._cm.get_string("use_buttons")) return message = update.callback_query.message if callback_data >= 0: chosen_change = _Changes(callback_data) if chosen_change in self._chosen_changes: self._chosen_changes[chosen_change.value] = None else: self._chosen_changes[chosen_change.value] = chosen_change if len([c for c in self._chosen_changes if c is not None]) > 0: reply_inline_keyboard = InlineKeyboardMarkup( self._choice_keyboard) new_message_text = self._cm.get_string( "init") + "\n\n -- " + "\n -- ".join([ self.NAMES[c] for c in self._chosen_changes if c is not None ]) else: reply_inline_keyboard = InlineKeyboardMarkup( self._choice_keyboard[:-1]) new_message_text = self._cm.get_string("init") update.callback_query.edit_message_text( text=new_message_text, reply_markup=reply_inline_keyboard) else: self._state = _EgrulIndirectStates.PROCESSING_CHANGES self._chosen_changes = [ c for c in self._chosen_changes if c is not None ] update.callback_query.edit_message_text(text=message.text, reply_markup=None) self._current_change_id = 0 self._current_change = self._chosen_changes[ self._current_change_id] self._current_sub_establisher = self.ESTABLISHERS[ self._current_change](self._bot, self._user_id, self._schema) return self.handle_update(update) def _handle_process_changes(self, update): result = self._current_sub_establisher.handle_update(update) if result: if self._current_change_id < len(self._chosen_changes) - 1: self._current_change_id += 1 self._current_change = self._chosen_changes[ self._current_change_id] self._current_sub_establisher = self.ESTABLISHERS[ self._current_change](self._bot, self._user_id, self._schema) return self.handle_update(update) else: self._state = States.END return self.handle_update(update) else: return