def reply_afk(bot: Bot, update: Update): message = update.effective_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: return chat = bot.get_chat(user_id) 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 = "{} is AFK!".format(fst_name) else: res = "{} is AFK!\nReason:\n{}".format( fst_name, reason) message.reply_text(res)
def reply_afk(bot: Bot, update: Update): message = update.effective_message # type: Optional[Message] userc = update.effective_user # type: Optional[User] userc_id = userc.id if message.entities and message.parse_entities( [MessageEntity.TEXT_MENTION, MessageEntity.MENTION]): entities = message.parse_entities( [MessageEntity.TEXT_MENTION, MessageEntity.MENTION]) chk_users = [] for ent in entities: if ent.type == MessageEntity.TEXT_MENTION: user_id = ent.user.id fst_name = ent.user.first_name if user_id in chk_users: return chk_users.append(user_id) if 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 if user_id in chk_users: return chk_users.append(user_id) try: chat = bot.get_chat(user_id) except BadRequest: print("Error: Could not fetch userid {} for AFK module". format(user_id)) return fst_name = chat.first_name else: return check_afk(bot, update, user_id, fst_name, userc_id) elif message.reply_to_message: user_id = message.reply_to_message.from_user.id fst_name = message.reply_to_message.from_user.first_name check_afk(bot, update, user_id, fst_name, userc_id)
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 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 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 messages from" "them to give me control! (Like a voodoo doll, I need a piece to be able to" "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