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_options(self, options, get_value): """ options == [(k, v)] -> direct key value store options == [v] && get_value == None -> store values indexed options == [x] && get_value == lambda(x) -> store result of lambda indexed """ for idx, option in enumerate(options): idx = '%s_%d' % (self._id, idx) if isinstance(option, tuple): (key, val) = option Cache.put([self._id, key], val) else: if get_value is None: Cache.put([self._id, idx], option) else: # Make sure that we only apply the load action when it is not in the cache yet. # This because the load action might be very expensive if not Cache.contains([self._id, idx]): Cache.put([self._id, idx], get_value(option))
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 on_update(self, bot): if not Cache.contains(self.cache_key): Cache.put(self.cache_key, {})