def get_message_type(msg: Message): data_type = None content = None text = "" raw_text = msg.text or msg.caption args = raw_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( raw_text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(args[1], 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) >= 1 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 elif msg.reply_to_message.video_note: content = msg.reply_to_message.video_note.file_id text, buttons = button_markdown_parser(msgtext, entities=entities) data_type = Types.VIDEO_NOTE return text, data_type, content, buttons
def send_rules(update, chat_id, from_pm=False): bot = dispatcher.bot user = update.effective_user # type: Optional[User] try: chat = bot.get_chat(chat_id) except BadRequest as excp: if excp.message == "Chat not found" and from_pm: bot.send_message( user.id, "The rules shortcut for this chat hasn't been set properly! Ask admins to " "fix this.") return else: raise rules, buttons = button_markdown_parser(sql.get_rules(chat_id)) text = "The rules for *{}* are:\n\n{}".format(escape_markdown(chat.title), rules) if from_pm and rules: bot.send_message(user.id, text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup( build_keyboard_alternate(buttons))) elif from_pm: bot.send_message( user.id, "The group admins haven't set any rules for this chat yet. " "This probably doesn't mean it's lawless though...!") elif rules: update.effective_message.reply_text( "Click the button below to get this group's rules.", reply_markup=InlineKeyboardMarkup([[ InlineKeyboardButton(text="Rules", url="t.me/{}?start={}".format( bot.username, chat_id)) ]])) else: update.effective_message.reply_text( "The group admins haven't set any rules for this chat yet. " "This probably doesn't mean it's lawless though...!")
def get_welcome_type(msg: Message): data_type = None content = None text = "" try: if msg.reply_to_message: if msg.reply_to_message.text: args = msg.reply_to_message.text else: args = msg.reply_to_message.caption else: args = msg.text.split(None, 1) # use python's maxsplit to separate cmd and args except AttributeError: args = False buttons = [] # determine what the contents of the filter are - text, image, sticker, etc if args: if msg.reply_to_message: if msg.reply_to_message.caption: argumen = msg.reply_to_message.caption elif msg.reply_to_message.text: argumen = msg.reply_to_message.text else: argumen = args[1] offset = len(argumen) - len(msg.text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(argumen, entities=msg.parse_entities(), offset=offset) if buttons: data_type = Types.BUTTON_TEXT else: data_type = Types.TEXT if msg.reply_to_message and msg.reply_to_message.sticker: content = msg.reply_to_message.sticker.file_id text = None 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 elif msg.reply_to_message and msg.reply_to_message.video_note: content = msg.reply_to_message.video_note.file_id text = None data_type = Types.VIDEO_NOTE return text, data_type, content, buttons
def filters(bot: Bot, update: Update): chat = update.effective_chat user = update.effective_user msg = update.effective_message args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text conn = connected(bot, update, chat, user.id) if conn: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = tld(chat.id, "cust_filters_local") else: chat_name = chat.title 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: offset = len(extracted[1]) - len( msg.text) # set correct offset relative to command + notename content, buttons = button_markdown_parser( extracted[1], entities=msg.parse_entities(), offset=offset) content = content.strip() if not content: msg.reply_text(tld(chat.id, "cust_filters_err_btn_only")) 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: content = msg.reply_to_message.photo[ -1].file_id # last elem = best quality 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(tld(chat.id, "cust_filters_err_empty")) 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(tld(chat.id, "cust_filters_add_success").format(keyword, chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop
# 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: offset = len(extracted[1]) - len( msg.text) # set correct offset relative to command + notename content, buttons = button_markdown_parser( extracted[1], entities=msg.parse_entities(), offset=offset) content = content.strip() if not content: msg.reply_text(tld(chat.id, "cust_filters_err_btn_only")) 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: content = msg.reply_to_message.photo[
def filters(update, context): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user msg = update.effective_message # type: Optional[Message] args = msg.text.split( None, 1) # use python's maxsplit to separate Cmd, keyword, and reply_text conn = connected(context.bot, update, chat, user.id) if conn is not False: chat_id = conn chat_name = dispatcher.bot.getChat(conn).title else: chat_id = update.effective_chat.id if chat.type == "private": chat_name = tl(update.effective_message, "catatan lokal") else: chat_name = chat.title if not msg.reply_to_message and len(args) < 2: send_message( update.effective_message, tl(update.effective_message, "Anda harus memberi nama untuk filter ini!")) return if msg.reply_to_message: if len(args) < 2: send_message( update.effective_message, tl(update.effective_message, "Anda harus memberi nama untuk filter ini!")) return else: keyword = args[1] else: 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() # 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) text, file_type, file_id = get_filter_type(msg) if not msg.reply_to_message and len(extracted) >= 2: offset = len(extracted[1]) - len( msg.text) # set correct offset relative to command + notename text, buttons = button_markdown_parser(extracted[1], entities=msg.parse_entities(), offset=offset) text = text.strip() if not text: send_message( update.effective_message, tl( update.effective_message, "Tidak ada pesan catatan - Anda tidak bisa HANYA menekan tombol, Anda perlu pesan untuk melakukannya!" )) return elif msg.reply_to_message and len(args) >= 2: if msg.reply_to_message.text: text_to_parsing = msg.reply_to_message.text elif msg.reply_to_message.caption: text_to_parsing = msg.reply_to_message.caption else: text_to_parsing = "" offset = len(text_to_parsing ) # set correct offset relative to command + notename text, buttons = button_markdown_parser(text_to_parsing, entities=msg.parse_entities(), offset=offset) text = text.strip() elif not text and not file_type: send_message( update.effective_message, tl(update.effective_message, "Anda harus memberi nama untuk filter ini!")) return elif msg.reply_to_message: if msg.reply_to_message.text: text_to_parsing = msg.reply_to_message.text elif msg.reply_to_message.caption: text_to_parsing = msg.reply_to_message.caption else: text_to_parsing = "" offset = len(text_to_parsing ) # set correct offset relative to command + notename text, buttons = button_markdown_parser(text_to_parsing, entities=msg.parse_entities(), offset=offset) text = text.strip() if (msg.reply_to_message.text or msg.reply_to_message.caption) and not text: send_message( update.effective_message, tl( update.effective_message, "Tidak ada pesan catatan - Anda tidak bisa HANYA menekan tombol, Anda perlu pesan untuk melakukannya!" )) return else: send_message(update.effective_message, tl(update.effective_message, "Invalid filter!")) return sql.new_add_filter(chat_id, keyword, text, file_type, file_id, buttons) send_message(update.effective_message, tl(update.effective_message, "Handler '{}' ditambahkan di *{}*!").format( keyword, chat_name), parse_mode=telegram.ParseMode.MARKDOWN) raise DispatcherHandlerStop
def send_rules(update, chat_id, from_pm=False): bot = dispatcher.bot user = update.effective_user try: chat = bot.get_chat(chat_id) except BadRequest as excp: if excp.message == "Chat not found" and from_pm: bot.send_message(user.id, tl(update.effective_message, "Pintasan aturan untuk obrolan ini belum diatur dengan benar! Mintalah admin untuk " "perbaiki ini.")) return else: raise conn = connected(bot, update, chat, user.id, need_admin=False) if conn: chat = dispatcher.bot.getChat(conn) chat_id = conn chat_name = dispatcher.bot.getChat(conn).title rules, buttons = button_markdown_parser(sql.get_rules(chat_id)) try: text = tl(update.effective_message, "Peraturan untuk *{}* adalah:\n\n{}").format(escape_markdown(chat.title), rules) except TypeError: send_message(update.effective_message, tl(update.effective_message, "Anda bisa lakukan command ini pada grup, bukan pada PM")) return "" is_private = sql.get_private_rules(chat_id) if from_pm and rules: bot.send_message(user.id, text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(build_keyboard_alternate(buttons))) elif from_pm: if conn: bot.send_message(user.id, tl(update.effective_message, "Admin grup belum menetapkan aturan apa pun untuk *{}*. " "Bukan berarti obrolan ini tanpa hukum...!").format( chat_name), parse_mode="markdown") else: bot.send_message(user.id, tl(update.effective_message, "Admin grup belum menetapkan aturan apa pun untuk obrolan ini. " "Bukan berarti obrolan ini tanpa hukum...!")) elif rules: if (update.effective_message.chat.type == "private" or not is_private) and rules: if not is_private: send_message(update.effective_message, text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(build_keyboard_alternate(buttons))) else: bot.send_message(user.id, text, parse_mode=ParseMode.MARKDOWN, reply_markup=InlineKeyboardMarkup(build_keyboard_alternate(buttons))) else: send_message(update.effective_message, tl(update.effective_message, "Hubungi saya di PM untuk mendapatkan aturan grup ini"), reply_markup=InlineKeyboardMarkup( [[InlineKeyboardButton(text=tl(update.effective_message, "Peraturan"), url="t.me/{}?start={}".format(bot.username, chat_id))]])) else: if conn: send_message(update.effective_message, tl(update.effective_message, "Admin grup belum menetapkan aturan apa pun untuk *{}*. " "Bukan berarti obrolan ini tanpa hukum...!").format(chat_name), parse_mode="markdown") else: send_message(update.effective_message, tl(update.effective_message, "Admin grup belum menetapkan aturan apa pun untuk obrolan ini. " "Bukan berarti obrolan ini tanpa hukum...!"))