def reply_afk(update, context): message = update.effective_message # type: Optional[Message] entities = message.parse_entities([MessageEntity.TEXT_MENTION, MessageEntity.MENTION]) if message.entities and entities: for ent in entities: if ent.type == MessageEntity.TEXT_MENTION: user_id = ent.user.id fst_name = ent.user.first_name elif ent.type == MessageEntity.MENTION: user_id = get_user_id(message.text[ent.offset:ent.offset + ent.length]) if not user_id: # Should never happen, since for a user to become AFK they must have spoken. Maybe changed username? return try: chat = context.bot.get_chat(user_id) except BadRequest: print("Error in afk can't get user id {}".format(user_id)) return fst_name = chat.first_name else: return if sql.is_afk(user_id): valid, reason = sql.check_afk_status(user_id) if valid: if not reason: res = "Hang on... {} away from keyboard!".format(fst_name) else: res = "{} is away from keyboard! says it's because of: \n{}".format(fst_name, reason) send_message(update.effective_message, res)
def extract_unt_fedban(message: Message, args: List[str]) -> (Optional[int], Optional[str]): prev_message = message.reply_to_message split_text = message.text.split(None, 1) if len(split_text) < 2: return id_from_reply(message) # only option possible text_to_parse = split_text[1] text = "" entities = list(message.parse_entities([MessageEntity.TEXT_MENTION])) if len(entities) > 0: ent = entities[0] else: ent = None # if entity offset matches (command end/text start) then all good if entities and ent and ent.offset == len( message.text) - len(text_to_parse): ent = entities[0] user_id = ent.user.id text = message.text[ent.offset + ent.length:] elif len(args) >= 1 and args[0][0] == '@': user = args[0] user_id = get_user_id(user) if not user_id and not str(user_id).isdigit(): message.reply_text( "I don't have this user's information in my database so, you'll not be able to interact with them" "Try replying to that person's msg or forward their message so i can act upon them" ) return None, None else: user_id = user_id res = message.text.split(None, 2) if len(res) >= 3: text = res[2] elif len(args) >= 1 and args[0].isdigit(): user_id = int(args[0]) res = message.text.split(None, 2) if len(res) >= 3: text = res[2] elif prev_message: user_id, text = id_from_reply(message) else: return None, None try: message.bot.get_chat(user_id) except BadRequest as excp: if excp.message in ("User_id_invalid", "Chat not found") and not str(user_id).isdigit(): message.reply_text( "I don't seem to have interacted with this user before - please forward a message from " "them to give me control! (like a voodoo doll, I need a piece of them to be able " "to execute certain commands...)") return None, None elif excp.message != "Chat not found": LOGGER.exception("Exception %s on user %s", excp.message, user_id) return None, None elif not str(user_id).isdigit(): return None, None return user_id, text