def __init__(self, messages, show_preview, buttons, parse_mode): super(AbstractSendMessage, self).__init__(RandomMessageHandler.get_random_id(), []) self.add_options(messages) self.parse_mode = parse_mode self.show_preview = show_preview self.buttons = buttons Cache.config_entry(self._id, False)
def add_handler_cache_key_msg(self, bot, update, user_data): user_data['user_msg'] = True if update.message.text: key = update.message.text.strip() if Cache.contains(key) or len(key) >= 32 or key.startswith('$'): message = 'Invalid key. Either it is already in use, starts with a $, or it is too long' self.send_or_edit(bot, user_data, update.message, message) return self.ADD_HANDLER_CACHE_KEY else: default = user_data['acc'] user_data['acc'] = key Cache.config_entry(key, True) if not Cache.contains(key): Cache.put(key, default) Cache.add_chat_key(key, user_data['chat_id']) return self._handle_stack(bot, update.message, user_data) else: message = 'Invalid key. It must contain text' self.send_or_edit(bot, user_data, update.message, message) return self.ADD_HANDLER_CACHE_KEY
def add_handler_api_key_msg(self, bot, update, user_data): user_data['user_msg'] = True if update.message.text: val = update.message.text.strip() (stage, data, idx) = user_data['stack'][-1] if stage == 0 and (Cache.contains(val) or len(val) >= 32 or val.startswith('$')): message = 'Invalid API key name. Either it is already in use, it starts with a $, or it is too long' self.send_or_edit(bot, user_data, update.message, message) else: (stage, data, res) = self.HANDLERS[idx].create_api(stage, data, val) user_data['stack'][-1] = (stage, data, idx) if isinstance(res, Send): self.send_or_edit(bot, user_data, update.message, res.msg, res.buttons) return self.ADD_HANDLER_API_KEY elif isinstance(res, Done): (key, value) = res.handler user_data['acc'] = key Cache.config_entry(key, True) Cache.add_api_key(key, user_data['chat_id']) Cache.put(key, value, encrypt=True) user_data['stack'] = user_data['stack'][:-1] return self._handle_stack(bot, update.message, user_data) else: print(stage, data, res) self.send_or_edit( bot, user_data, update.message, 'Unexpected API creation state! Please report your steps to the developer' ) return ConversationHandler.END else: message = 'Invalid reply. It must contain text' self.send_or_edit(bot, user_data, update.message, message) return self.ADD_HANDLER_API_KEY
def __init__(self, id, children, do_cache=False): super(RandomMessageHandler, self).__init__(children) self._id = id Cache.config_entry(id, do_cache)