def start(self, bot, update, user_data): if not update.message.chat.type == 'private': update.message.reply_text( 'This command can only be used in a private conversation') return ConversationHandler.END user_data['stack'] = [] user_data['acc'] = None user_data['user_msg'] = False user = update.message.from_user message = update.message Logger.log_debug("User %s started config conversation." % user.first_name) chats = list(Cache.get_admin_chats(message.from_user.id)) chat_names = [(chat_id, Cache.get_chat_title(chat_id)) for chat_id in chats] global_button = [[ InlineKeyboardButton(text='Global config', callback_data=HandlerGroup.GLOBAL) ]] if str(self.bot.admin_chat) in chats else [] buttons = [[InlineKeyboardButton(text=name, callback_data=str(id))] for (id, name) in chat_names] buttons.append( [InlineKeyboardButton(text='Exit', callback_data=str(self.EXIT))]) reply_markup = InlineKeyboardMarkup(global_button + buttons) message.reply_text('Select a chat to configure', reply_markup=reply_markup) return self.SELECT_CHAT
def add_handler_select_cache_key_callback(self, bot, update, user_data): stack = user_data['stack'] current_idx = stack[-1][2] user_id = update.callback_query.from_user.id # Get the chats where the user is admin. # If it includes the admin chat, we also add the GLOBAL key chats_where_admin = list(Cache.get_admin_chats(user_id)) if (str(self.bot.admin_chat) in chats_where_admin): chats_where_admin.append('GLOBAL') keys = [ key for chat_id in chats_where_admin for key in Cache.get_chat_keys(chat_id) ] if len(keys) == 0: message = 'You do not have access to any existing keys, please send me a valid key' buttons = None else: message = 'Select a key' buttons = [[ InlineKeyboardButton(text=key, callback_data='0_' + str(key)) ] for key in keys] self.send_or_edit(bot, user_data, update.callback_query.message, message, buttons) return self.ADD_HANDLER_CACHE_KEY
def copy_start(self, bot, update, user_data): msg = update.callback_query.message chats = Cache.get_admin_chats(update._effective_user.id) chat_names = [(chat_id, Cache.get_chat_title(chat_id)) for chat_id in chats] if chat_names: global_button = [[ InlineKeyboardButton(text='Global config', callback_data=HandlerGroup.GLOBAL) ]] if str(self.bot.admin_chat) in chats else [] buttons = [[ InlineKeyboardButton(text=name, callback_data=str(id)) ] for (id, name) in chat_names] message = 'Select a chat to copy a handler from' self.send_or_edit(bot, user_data, msg, message, global_button + buttons) return self.COPY_HANDLER else: self.send_or_edit(bot, user_data, msg, 'No chats to copy from') return ConversationHandler.END
def create(cls, stage, data, arg): buttons = [[ InlineKeyboardButton(text='User', callback_data='user'), InlineKeyboardButton(text='Chat', callback_data='chat') ]] if stage == 0: return (1, data['user_id'], Send('Do you want to filter on user or chat inactivity?', buttons=buttons)) elif stage == 1: if isinstance(arg, str): check_user = arg == 'user' if check_user: return ( 2, 'user', Send( 'Now forward me a message from the user to filter on' )) else: chats = [[ InlineKeyboardButton( text=Cache.get_chat_title(chat_id), callback_data='c_%s' % chat_id) ] for chat_id in Cache.get_admin_chats(data)] return (2, 'chat', Send('Which chat should be filtered on?', buttons=chats)) else: return (1, None, Send('Please use the buttons', buttons=buttons)) elif stage == 2: default = {'chat': {}, 'user': {}} if isinstance(arg, str): return (3, (data, arg[2:]), AskCacheKey(default)) elif arg.forward_from: id = arg.forward_from.id return (3, (data, id), AskCacheKey(default)) else: return ( 2, data, Send( 'I asked you to forward a message, it shouldn\'t be too hard ' )) elif stage == 3 and arg: return (4, (data[0], data[1], arg), Send('Send me the amout of minutes of inactivity')) elif stage == 4: if arg.text and re.match(r'[\d]+$', arg.text): type, id, key = data minutes = int(arg.text) return (5, (type, id, key, minutes, []), AskChild()) else: return (4, arg, Send('Invalid input, try again')) elif stage == 5: if isinstance(arg, MessageHandler): data[4].append(arg) return (5, data, AskChild()) else: type, id, key, minutes, children = data return (-1, None, Done( cls._from_dict( { 'type': type, 'id': id, 'key': key, 'timedelta': (0, minutes * 60) }, children))) else: print(stage, data, arg) raise CreateException('Invalid create state for activity')