Ejemplo n.º 1
0
def retrieve_and_format_user(bot: Bot,
                             user_id: Union[str, int],
                             do_link: bool = False,
                             prefer_username: bool = False,
                             id_fallback: bool = False,
                             user_tag: bool = None,
                             id_tag: bool = None,
                             html_escape: bool = True) -> str:
    """
    Gets a user's name by given user id.

    If you want to use it in html you should probably keep html character escaping enabled (`html_escape=True`).
    You can specify to wrap the user name string or id string in html tags, with the user_tag or id_tag arguments.
    If the name is "Max Mustermann", with `user_tag="b"` it will return "<b>Max Mustermann</b>".

    Could return `None` if that user was either not found, or had no usable data.

    :param bot: The bot instance to use to check the user.
    :param user_id: the id of the telegram user.
    :param do_link: If it should do a link instead of bold or anything provided in `user_tag` or `id_tag`.
    :param prefer_username: if you should prefer the @username over the real name
    :param id_fallback: if no real name and no @username exists, if it should fallback to the user id: user#123456
    :param user_tag: Name of the html tag to wrap the user string in (not the @username)
    :param id_tag: Name of the html tag to wrap the user id in. (`id_fallback` must be `True`)
    :param html_escape: If html tags in strings should be escaped. Default: True

    :return: the (formatted) name
    """
    try:
        user = bot.get_chat_member(user_id, user_id).user
    except TgApiException:
        if id_fallback:
            name = "user#" + str(user_id)
            name = escape(name) if (name is not None and html_escape) else name
            if do_link:
                return '<a href="tg://user?id={id}">{name}</a>'.format(
                    id=user_id, name=name)
            if id_tag:
                return "<{tag}>{name}</{tag}>".format(tag=id_tag, name=name)
            # end if
        # end if
        return None
    # end try
    return format_user(user,
                       do_link=do_link,
                       prefer_username=prefer_username,
                       id_fallback=id_fallback,
                       user_tag=user_tag,
                       id_tag=id_tag,
                       html_escape=html_escape)
Ejemplo n.º 2
0
def retrieve_and_is_member(bot: Bot, chat_id: int, user_id: int) -> bool:
    """
    Checks if a user is still in a chat/group.
    If lookup fails, `False` is assumed.

    :param bot: The bot to execute the get_chat_member request with.
    :param chat_id: id of the chat the user should be in
    :param user_id: id of the user

    :return:
    """
    try:
        chat_member = bot.get_chat_member(chat_id, user_id)
        logger.debug(f'get_chat_member result: {chat_member}')
        return is_member(chat_member)
    except TgApiException:
        logger.exception(
            'calling get_chat_member failed. Defaulting to \'no admin\'.')
        return False
Ejemplo n.º 3
0
def retrieve_and_is_admin(bot: Bot,
                          chat_id: int,
                          user_id: int,
                          right: Union[None, str] = None) -> bool:
    """
    Checks if a user is admin (or even creator) of a chat/group. Optionally a specific right can be requested to exist.
    If lookup fails, `False` is assumed.

    :param bot: The bot to execute the get_chat_member request with.
    :param chat_id:
    :param user_id:
    :param right: Right to require, e.g. `"can_promote_members"`.

    :return:
    """
    try:
        chat_member = bot.get_chat_member(chat_id, user_id)
        logger.debug(f'get_chat_member result: {chat_member}')
        return is_admin(chat_member, right=right)
    except TgApiException:
        logger.exception(
            'calling get_chat_member failed. Defaulting to \'no admin\'.')
        return False