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) sql.set_rules(chat_id, markdown_rules) update.effective_message.reply_text( "Successfully set rules for this group.")
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): 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.HTML, reply_markup=keyboard, reply_to_message_id=reply, 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, reply_to_message_id=reply, ) 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, 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") 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
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