def get_note_type(msg: Message): data_type = None content = None text = "" raw_text = msg.text or msg.caption args = raw_text.split(None, 2) # use python's maxsplit to separate cmd and args note_name = args[1] buttons = [] # determine what the contents of the filter are - text, image, sticker, etc if len(args) >= 3: offset = len(args[2]) - len( raw_text) # set correct offset relative to command + notename text, buttons = button_markdown_parser( args[2], entities=msg.parse_entities() or msg.parse_caption_entities(), offset=offset, ) data_type = Types.BUTTON_TEXT if buttons else Types.TEXT elif msg.reply_to_message: entities = msg.reply_to_message.parse_entities() msgtext = msg.reply_to_message.text or msg.reply_to_message.caption if len(args) >= 2 and msg.reply_to_message.text: # not caption, text text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.BUTTON_TEXT if buttons else Types.TEXT elif msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id data_type = Types.STICKER elif msg.reply_to_message.document: content = msg.reply_to_message.document.file_id text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.DOCUMENT elif msg.reply_to_message.photo: content = msg.reply_to_message.photo[ -1].file_id # last elem = best quality text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.PHOTO elif msg.reply_to_message.audio: content = msg.reply_to_message.audio.file_id text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.AUDIO elif msg.reply_to_message.voice: content = msg.reply_to_message.voice.file_id text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.VOICE elif msg.reply_to_message.video: content = msg.reply_to_message.video.file_id text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.VIDEO return note_name, text, data_type, content, buttons
def get_welcome_type(msg: Message): data_type = None content = None text = "" args = msg.text.split(None, 1) # use python's maxsplit to separate cmd and args buttons = [] # determine what the contents of the filter are - text, image, sticker, etc if len(args) >= 2: offset = len(args[1]) - len( msg.text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(args[1], entities=msg.parse_entities(), offset=offset) if buttons: data_type = Types.BUTTON_TEXT else: data_type = Types.TEXT elif msg.reply_to_message and msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id text = msg.reply_to_message.caption data_type = Types.STICKER elif msg.reply_to_message and msg.reply_to_message.document: content = msg.reply_to_message.document.file_id text = msg.reply_to_message.caption data_type = Types.DOCUMENT elif msg.reply_to_message and msg.reply_to_message.photo: content = msg.reply_to_message.photo[ -1].file_id # last elem = best quality text = msg.reply_to_message.caption data_type = Types.PHOTO elif msg.reply_to_message and msg.reply_to_message.audio: content = msg.reply_to_message.audio.file_id text = msg.reply_to_message.caption data_type = Types.AUDIO elif msg.reply_to_message and msg.reply_to_message.voice: content = msg.reply_to_message.voice.file_id text = msg.reply_to_message.caption data_type = Types.VOICE elif msg.reply_to_message and msg.reply_to_message.video: content = msg.reply_to_message.video.file_id text = msg.reply_to_message.caption data_type = Types.VIDEO return text, data_type, content, buttons
def filters(update: Update, _): chat = update.effective_chat msg = update.effective_message args = msg.text.split(None, 1) if len(args) < 2: return extracted = split_quotes(args[1]) if len(extracted) < 1: return # set trigger -> lower, so as to avoid adding duplicate filters with # different cases keyword = extracted[0].lower() is_sticker = False is_document = False is_image = False is_voice = False is_audio = False is_video = False buttons = [] # determine what the contents of the filter are - text, image, sticker, etc if len(extracted) >= 2: # set correct offset relative to command + notename offset = len(extracted[1]) - len(msg.text) content, buttons = button_markdown_parser( extracted[1], entities=msg.parse_entities(), offset=offset) content = content.strip() if not content: msg.reply_text( "There is no note message - You can't JUST have buttons, you need a message to go with it!" ) return elif msg.reply_to_message and msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id is_sticker = True elif msg.reply_to_message and msg.reply_to_message.document: content = msg.reply_to_message.document.file_id is_document = True elif msg.reply_to_message and msg.reply_to_message.photo: # last elem = best quality content = msg.reply_to_message.photo[-1].file_id is_image = True elif msg.reply_to_message and msg.reply_to_message.audio: content = msg.reply_to_message.audio.file_id is_audio = True elif msg.reply_to_message and msg.reply_to_message.voice: content = msg.reply_to_message.voice.file_id is_voice = True elif msg.reply_to_message and msg.reply_to_message.video: content = msg.reply_to_message.video.file_id is_video = True else: msg.reply_text("You didn't specify what to reply with!") return # Add the filter # Note: perhaps handlers can be removed somehow using sql.get_chat_filters for handler in dispatcher.handlers.get(HANDLER_GROUP, []): if handler.filters == (keyword, chat.id): dispatcher.remove_handler(handler, HANDLER_GROUP) sql.add_filter( chat.id, keyword, content, is_sticker, is_document, is_image, is_audio, is_voice, is_video, buttons, ) msg.reply_text("Handler '{}' added!".format(keyword)) raise DispatcherHandlerStop