def create(cls, stage, data, arg): if stage == 0: return (1, None, Send('Please forward the message to forward')) elif stage == 1 and arg: if not arg.forward_from: return (1, None, Send('Please forward a message')) return (-1, None, Done(Forward(arg.chat.id, arg.message_id))) else: print(stage, data, arg) raise CreateException('Invalid create state for sendTextMessage')
def create(cls, stage, data, arg): if stage == 0: msg = 'Please send me the text for on the button' return (1, None, Send(msg)) elif stage == 1: if isinstance(arg, Message): return (1, None, Done(UrlButton(arg.text))) return ( 1, None, Send('I don\'t like that name, please send me a different one') )
def create(cls, stage, data, arg): if stage is 0: return (1, None, Send("Send ma a sticker from the pack")) elif stage is 1 and arg: if arg.sticker: pack = arg.sticker.set_name return (-1, None, Done(SendStickerPack(pack))) else: return (1, None, Send("That is not a sticker, try again")) else: print(stage, data, arg) raise CreateException('Invalid create state for sendStickerPack')
def create(cls, stage, data, arg): if stage == 0: buttons = [[InlineKeyboardButton(text='yes', callback_data='yes')], [InlineKeyboardButton(text='no', callback_data='no')]] msg = 'Should an existing pin be replaced?' return (1, None, Send(msg=msg, buttons=buttons)) elif stage == 1: if isinstance(arg, Message): buttons = [[ InlineKeyboardButton(text='yes', callback_data='yes') ], [InlineKeyboardButton(text='no', callback_data='no')]] msg = 'Please use the buttons' return (1, None, Send(msg=msg, buttons=buttons)) return (-1, None, Done(Pin(arg == 'yes')))
def create(cls, stage, data, arg): if stage is 0: return (1, None, Send('Please send the album ID')) elif stage is 1 and arg: if arg.text and re.match(r'[\d]+$', arg.text): return (2, arg.text, AskAPIKey()) else: return (1, None, Send('Please send a valid ID')) elif stage is 2 and arg: return (-1, None, Done( SendFlickr(RandomMessageHandler.get_random_id(), arg, data))) else: print(stage, data, arg) raise CreateException('Invalid create state for sendFlickr')
def create(cls, stage, data, arg): if stage == 0: buttons = [[ InlineKeyboardButton(text='yes', callback_data='yes'), InlineKeyboardButton(text='no', callback_data='no') ]] return (1, [], Send('Do you want to filter on minutes?', buttons=buttons)) if stage in (1, 2): return cls._create_step(stage, data, arg, 'minutes', 'hour', 0, 60) elif stage in (3, 4): return cls._create_step(stage, data, arg, 'hour', 'day', 0, 24) elif stage in (5, 6): return cls._create_step(stage, data, arg, 'day', 'weekday', 1, 32) elif stage in (7, 8): return cls._create_step(stage, data, arg, 'weekday', 'week', 0, 7, opt_string='Monday is 0 and Sunday is 6') elif stage in (9, 10): return cls._create_step(stage, data, arg, 'week', 'month', 1, 53) elif stage in (11, 12): return cls._create_step(stage, data, arg, 'month', 'week of the month', 1, 13) elif stage in (13, 14): return cls._create_step(stage, data, arg, 'week of the month', 'year', 1, 5) elif stage in (15, 16): res = cls._create_step(stage, data, arg, 'year', None, 2000, 3000) if isinstance(res, tuple): return res else: return (17, ([], res), AskChild()) elif stage == 17 and isinstance(arg, MessageHandler): data[0].append(arg) return (17, data, AskChild()) elif stage == 17 and isinstance(arg, NoChild): children, config = data return (-1, None, Done( TimeFilter(config[0], config[1], config[2], config[3], config[4], config[5], config[6], config[7], children=children))) else: print(stage, data, arg) raise CreateException('Invalid create state for timeFilter')
def create(cls, stage, data, arg): if stage == 0: return (1, [], AskChild()) elif stage == 1 and isinstance(arg, MessageHandler): data.append((None, arg)) return (2, data, Send(msg='Please send a weight')) elif stage == 1 and isinstance(arg, NoChild): return (-1, None, Done(PickWeighted(data))) elif stage == 2 and arg: if not arg.text or not re.match(r'[\d]+$', arg.text): return (2, data, Send(msg='Invalid weight, must be a positive integer')) else: data[-1] = (int(arg.text), arg) return (1, data, AskChild()) else: print(stage, data, arg) raise CreateException('Invalid create state for pickWeighted')
def create_api(cls, stage, data, arg): if stage is 0: return (1, arg, Send('Please send me a Flickr-API key')) elif stage is 1: return (-1, None, Done((data, arg))) else: print(stage, data, arg) raise CreateException( 'Invalid API key create state for sendFlickr')
def create(cls, stage, data, arg): if stage == 0: return (1, None, Send('Please send me the command you want to filter on')) elif stage == 1 and arg: if not arg.text or not re.match(r'/[\w]+$', arg.text): return (1, None, Send('Not a valid command, try again')) command = arg.text[1:] return (2, (command, []), AskChild()) elif stage == 2 and isinstance(arg, MessageHandler): data[1].append(arg) return (2, data, AskChild()) elif stage == 2 and isinstance(arg, NoChild): (command, children) = data return (-1, None, Done(IsCommand('/' + command, children))) else: print(stage, data, arg) raise CreateException('Invalid create state for commandHandler')
def create(cls, stage, data, arg): if stage == 0: return (1, None, Send('Send me the regex to match')) elif stage == 1 and arg: if arg.text: try: pattern = re.compile(arg.text) groups = list(range(pattern.groups + 1)) + list( pattern.groupindex.keys()) if groups: buttons = [[ InlineKeyboardButton(text=str(group), callback_data='r_' + str(group)) ] for group in groups] msg = 'Select a captured group to replace the original message, ' \ 'select group 0 to retain the entire match' return (2, arg.text, Send(msg=msg, buttons=buttons)) else: return (3, (arg.text, None, []), AskChild()) except re.error: return (1, None, Send('Invalid expression, try again')) else: return (1, data, Send('Please send text and nothing else')) elif stage == 2 and arg: if isinstance(arg, Message): raise CreateException() #TODO: resend the buttons arg = arg[2:] try: group = int(arg) if group < 0: group = None except: group = arg return (3, (data, group, []), AskChild()) elif stage == 3 and isinstance(arg, MessageHandler): data[2].append(arg) return (3, data, AskChild()) elif stage == 3 and isinstance(arg, NoChild): regex, group, children = data return (-1, None, Done(cls._create(regex, children, group))) else: print(stage, data, arg) raise CreateException('Invalid create state for regexFilter')
def create(cls, stage, data, arg): if stage == 0: default = {'chat':{}, 'user':{}} return (1, None, AskCacheKey(default)) elif stage == 1 and arg: return (2, arg, Send('Should the chat activity be monitored?', buttons=Send.YES_NO)) elif stage == 2: if isinstance(arg, str): return (3, (data, arg), Send('Should user activity be monitored?', buttons=Send.YES_NO)) else: return (2, arg, Send('There are just two options, and those are provided by these buttons:', buttons=Send.YES_NO)) elif stage == 3: if isinstance(arg, str): return (-1, None, Done(ActivityMonitor(data[1], arg, data[0]))) else: return (3, arg, Send('That is not equivalent to pressing the buttons', buttons=Send.YES_NO)) else: print(stage, data, arg) raise CreateException('Invalid create state for monitor')
def create(cls, stage, data, arg): if stage == 0: return ( 1, None, Send('Forward me a message from the user you want to filter on' )) elif stage == 1: if arg.forward_from: return (2, (arg.forward_from.id, []), AskChild()) else: return (1, None, Send('That is not what I asked you to do, try again')) elif stage == 2 and isinstance(arg, MessageHandler): data[1].append(arg) return (2, data, AskChild()) elif stage == 2 and isinstance(arg, NoChild): return (-1, None, Done(IsFrom(data[0], data[1]))) else: print(stage, data, arg) raise CreateException('Invalid create state for isFrom')
def create(cls, stage, data, arg): if stage == 0: return ( 1, None, Send( 'Send me the new message text. Valid placeholders:\n%h -> The user handle\n%f -> The first name of the user\n%l The last name of the user\n%n -> The name of the user\n%s -> The original message\n%d -> The current amount (used for counting and voting, only replaces first)' )) elif stage == 1: if arg.text: return (2, [arg.text, []], AskChild()) else: return (1, data, Send('Please send text and nothing else')) elif stage == 2 and isinstance(arg, MessageHandler): data[1].append(arg) return (2, data, AskChild()) elif stage == 2 and isinstance(arg, NoChild): return (-1, None, Done(BuildMessage(data[1], data[0]))) else: print(stage, data, arg) raise CreateException('Invalid create state for buildMessage')
def create(cls, stage, data, arg): if stage == 0: return (1, None, AskCacheKey(default={})) elif stage == 1 and arg: return (2, arg, Send('Which value should the counter be updated to?')) elif stage == 2 and arg: if arg.text and re.match(r'-?[\d]+', arg.text): return (3, (data, int(arg.text), []), AskChild()) else: return (2, arg, Send('That is not a valid number of votes')) elif stage == 3 and arg: if isinstance(arg, MessageHandler): data[2].append(arg) return (3, data, AskChild()) else: key, count, children = data return (-1, None, Done(SetCount(key, count, children))) else: print(stage, data, arg) raise CreateException('Invalid create state for Count')
def create(cls, stage, data, arg): if stage == 0: return (1, None, Send(cls._initial_message())) elif stage == 1 and arg: if arg.text: url = cls._parse_url(arg.text) if url: return (2, url, Send('What is the start index? (0 for the first item)')) return (1, None, Send('Please reply with a url to an RSS feed')) elif stage == 2 and arg: if arg.text and re.match(r'[\d]+$', arg.text): return (3, (data, int(arg.text), []), Send('Should the title be shown?', buttons=Send.YES_NO)) else: return (2, None, Send('Ivalid index, try again')) elif stage == 3: if isinstance(arg, str): if arg == 'yes': data[2].append('title') return (4, data, Send('Should the discription be shown?', buttons=Send.YES_NO)) else: return (3, data, Send('I created those buttons for a reason...', buttons=Send.YES_NO)) elif stage == 4: if isinstance(arg, str): if arg == 'yes': data[2].append('description') return (5, data, Send('Should the link be shown?', buttons=Send.YES_NO)) else: return (4, data, Send('Please use these buttons:', buttons=Send.YES_NO)) elif stage == 5: if isinstance(arg, str): if arg == 'yes': data[2].append('link') (url, index, show) = data return (-1, None, Done(SendRSS(url, show, index))) else: return (5, data, Send('I\'m just a bot, I cannot read human', buttons=Send.YES_NO)) else: print(stage, data, arg) raise CreateException('Invalid create state for sendTextMessage')
def create(cls, stage, data, arg): if stage == 0: return (1, None, Send('Send me the percentage of messages to keep')) elif stage == 1: match = re.match(r'([\d]?[\d])%?$', arg.text) if match: val = int(match.group(1)) return (2, (val, []), AskChild()) elif re.match(r'0?\.[\d]+$', arg.text): val = float(arg.text) * 100 return (2, (val, []), AskChild()) else: return (1, None, Send('That is not a valid percentage')) elif stage == 2 and isinstance(arg, MessageHandler): data[1].append(arg) return (2, data, AskChild()) elif stage == 2 and isinstance(arg, NoChild): percentage, children = data return (-1, None, Done(PercentageFilter(percentage, children))) else: print(stage, data, arg) raise CreateException('Invalid create state for percentageFilter')
def create(cls, stage, data, arg): if stage == 0: return (1, [], Send("Send ma a sticker to send")) elif stage == 1 and arg: if arg.sticker: data.append(arg.sticker.file_id) return (2, data, Send('Do you want to add another sticker?', buttons=Send.YES_NO)) else: return (1, data, Send("That is not a sticker, try again")) elif stage == 2: if isinstance(arg, str): if arg == 'yes': return (1, data, Send("Send ma a new sticker")) else: return (-1, None, Done(SendStickers(data))) else: return (2, data, Send("Clicking a button is hard apparently", buttons=Send.YES_NO)) else: print(stage, data, arg) raise CreateException('Invalid create state for sendStickers')
def _create_step(cls, stage, data, arg, current_string, next_string, min, max, opt_string=''): buttons = [[ InlineKeyboardButton(text='yes', callback_data='yes'), InlineKeyboardButton(text='no', callback_data='no') ]] if stage % 2 == 1: if isinstance(arg, Message): return (stage, data, Send('Please reply by clicking the buttons', buttons=buttons)) else: if arg == 'yes': return (stage + 1, data, Send('Send me a number to filter the %s on. %s' % (current_string, opt_string))) else: data.append(None) if next_string is None: return data else: return (stage + 2, data, Send('Do you want to filter on the %s?' % next_string, buttons=buttons)) elif stage % 2 is 0: if arg.text: try: val = int(arg.text) if val < min or val >= max: raise ValueError() data.append(val) if next_string is None: return data else: return (stage + 1, data, Send('Do you want to filter on the %s?' % next_string, buttons=buttons)) except ValueError: return (stage, data, Send('Invalid value, please try again')) else: return ( stage, data, Send('It would be nice if you actually tried to send text') )
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')
def create(cls, stage, data, arg): buttons = [[ InlineKeyboardButton(text='horizontal', callback_data='horizontal'), InlineKeyboardButton(text='vertical', callback_data='vertical') ]] if stage == 0: return (1, None, Send('Please send me the text to reply')) elif stage == 1 and arg: if not arg.text: return ( 1, None, Send('Please reply with text, not with something else')) return (2, arg.text, Send('Should previews be shown for URLs?', buttons=Send.YES_NO)) elif stage == 2 and arg: if isinstance(arg, Message): return (2, data, Send('Please reply by clicking the buttons', buttons=Send.YES_NO)) else: return (3, (data, arg == 'yes'), Send('Do you want to show buttons?', buttons=Send.YES_NO)) elif stage == 3: if arg == 'yes': return (4, data, Send('Should the buttons be horizontal of vertical?', buttons=buttons)) else: message, url = data obj = cls._from_dict( { 'messages': [message], 'preview': url, 'buttons': None }, []) return (-1, None, Done(obj)) elif stage == 4: if isinstance(arg, str): return (5, (data, arg == 'horizontal', []), Send('Now send me the text for the button')) else: return (4, data, Send('I created those buttons for a reason', buttons=buttons)) elif stage == 5: if arg.text: data[2].append(arg.text) return (6, data, Send('Dou you want to add more buttons?', buttons=Send.YES_NO)) else: return (5, data, Send('Invalid message, it should contain text')) elif stage == 6: if isinstance(arg, str): if arg == 'yes': return (5, data, Send('Send me the text for the next button')) else: ((text, url), is_horizontal, buttons) = data if is_horizontal: buttons = [buttons] else: buttons = [[button] for button in buttons] obj = cls._from_dict( { 'messages': [text], 'preview': url, 'buttons': buttons }, []) return (-1, None, Done(obj)) else: return ( 4, data, Send( 'I know you are a smart kid, but that was not a button press', buttons=Send.YES_NO)) else: print(stage, data, arg) raise CreateException('Invalid create state for sendTextMessage')