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 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 edit_chat(self, bot, update, user_data): query = update.callback_query chat_id = query.data user_data['chat_id'] = chat_id chat_name = 'global handlers' if chat_id == HandlerGroup.GLOBAL else Cache.get_chat_title( chat_id) toggle_global_text = '%s global handlers' % ( 'Enable' if Cache.chat_is_standalone(chat_id) else 'Disable') buttons = [[ InlineKeyboardButton(text='Add a handler', callback_data=str(self.ADD)), InlineKeyboardButton(text='Remove handler', callback_data=str(self.REMOVE)) ], [ InlineKeyboardButton(text='Edit a handler', callback_data=str(self.EDIT)), InlineKeyboardButton(text='Copy a handler', callback_data=str(self.COPY)) ]] toggle_button = [[ InlineKeyboardButton(text=toggle_global_text, callback_data=str(self.TOGGLE)) ]] if chat_id != HandlerGroup.GLOBAL else [] exit_button = [[ InlineKeyboardButton(text='Exit', callback_data=str(self.EXIT)) ]] bot.edit_message_text( chat_id=query.message.chat_id, message_id=query.message.message_id, text='Select what to configure for chat \'%s\'' % chat_name, reply_markup=InlineKeyboardMarkup(buttons + toggle_button + exit_button)) return self.SELECT_ACTION
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')