def set_rules(bot: Bot, update: Update): chat_id = update.effective_chat.id msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split(None, 1) # use python's maxsplit to separate cmd and args if len(args) == 2: txt = args[1] offset = len(txt) - len( raw_text) # set correct offset relative to command markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) sql.set_rules(chat_id, markdown_rules) update.effective_message.reply_text( "Successfully set rules for this group.")
def set_frules(bot: Bot, update: Update, args: List[str]): chat = update.effective_chat # type: Optional[Chat] user = update.effective_user # type: Optional[User] fed_id = sql.get_fed_id(chat.id) if not fed_id: update.effective_message.reply_text( tld(chat.id, "This chat is not in any federation!")) return if is_user_fed_admin(fed_id, user.id) == False: update.effective_message.reply_text( tld(chat.id, "Only fed admins can do this!")) return if len(args) >= 1: msg = update.effective_message # type: Optional[Message] raw_text = msg.text args = raw_text.split( None, 1) # use python's maxsplit to separate cmd and args if len(args) == 2: txt = args[1] offset = len(txt) - len( raw_text) # set correct offset relative to command markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) x = sql.set_frules(fed_id, markdown_rules) if not x: update.effective_message.reply_text( tld( chat.id, "Big F! There is an error while setting federation rules! If you wondered why please ask it in support group!" )) return rules = sql.get_fed_info(fed_id).fed_name update.effective_message.reply_text( tld(chat.id, f"Rules are set for {rules}!")) else: update.effective_message.reply_text( tld(chat.id, "Please write rules to set it up!"))
def set_rules(update, context): chat_id = update.effective_chat.id msg = update.effective_message # type: Optional[Message] raw_text = msg.text # use python's maxsplit to separate cmd and args args = raw_text.split(None, 1) if len(args) == 2: txt = args[1] # set correct offset relative to command offset = len(txt) - len(raw_text) markdown_rules = markdown_parser(txt, entities=msg.parse_entities(), offset=offset) RULES_DATA.find_one_and_update({'_id': chat_id}, {"$set": { 'rules': markdown_rules }}, upsert=True) update.effective_message.reply_text( "Successfully set rules for this group.")
def send(update, message, keyboard, backup_message): try: msg = update.effective_message.reply_text(message, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard, disable_web_page_preview=True) except IndexError: msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: the current message was " "invalid due to markdown issues. Could be " "due to the user's name."), parse_mode=ParseMode.MARKDOWN) except KeyError: msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: the current message is " "invalid due to an issue with some misplaced " "curly brackets. Please update"), parse_mode=ParseMode.MARKDOWN) except BadRequest as excp: if excp.message == "Button_url_invalid": msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: the current message has an invalid url " "in one of its buttons. Please update."), parse_mode=ParseMode.MARKDOWN) elif excp.message == "Unsupported url protocol": msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: the current message has buttons which " "use url protocols that are unsupported by " "telegram. Please update."), parse_mode=ParseMode.MARKDOWN) elif excp.message == "Wrong url host": msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: the current message has some bad urls. " "Please update."), parse_mode=ParseMode.MARKDOWN) LOGGER.warning(message) LOGGER.warning(keyboard) LOGGER.exception("Could not parse! got invalid url host errors") else: msg = update.effective_message.reply_text(markdown_parser(backup_message + "\nNote: An error occured when sending the " "custom message. Please update."), parse_mode=ParseMode.MARKDOWN) LOGGER.exception() return msg
def send(update, message, keyboard, backup_message): chat = update.effective_chat cleanserv = sql.clean_service(chat.id) reply = update.message.message_id # Clean service welcome if cleanserv: try: dispatcher.bot.delete_message(chat.id, update.message.message_id) except BadRequest: pass reply = False try: msg = update.effective_message.reply_text( message, parse_mode=ParseMode.MARKDOWN, reply_markup=keyboard, reply_to_message_id=reply, ) except BadRequest as excp: if excp.message == "Button_url_invalid": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has an invalid url " "in one of its buttons. Please update." ), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) elif excp.message == "Unsupported url protocol": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has buttons which " "use url protocols that are unsupported by " "telegram. Please update." ), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) elif excp.message == "Wrong url host": msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: the current message has some bad urls. " "Please update." ), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.warning(message) LOGGER.warning(keyboard) LOGGER.exception("Could not parse! got invalid url host errors") elif excp.message == "Have no rights to send a message": return else: msg = update.effective_message.reply_text( markdown_parser( backup_message + "\nNote: An error occured when sending the " "custom message. Please update." ), parse_mode=ParseMode.MARKDOWN, reply_to_message_id=reply, ) LOGGER.exception() return msg