Ejemplo n.º 1
0
    def load_members(self,
                     group_peer: Peer or AsyncTask,
                     limit: int = 0) -> List[User] or None:
        """Load Group members by peer

        :param group_peer: Peer or AsyncTask (in which located Group)
        :param limit: count members
        :return: list of User's
        """
        group_peer = get_peer(group_peer)
        out_peer = self.__get_out_peer(group_peer)
        if out_peer is None:
            return None

        request = groups_pb2.RequestLoadMembers(group=out_peer, limit=limit)
        members = self.internal.groups.LoadMembers(request).members
        request = sequence_and_updates_pb2.RequestGetReferencedEntitites(
            group_members=[
                sequence_and_updates_pb2.GroupMembersSubset(
                    group_peer=out_peer,
                    member_ids=[member.uid for member in members])
            ])
        return [
            User.from_api(x) for x in
            self.internal.updates.GetReferencedEntitites(request).users
        ]
Ejemplo n.º 2
0
 def get_user_bot_groups(self, uid):
     request = messaging_pb2.RequestLoadDialogs(
         min_date=0,
         limit=100000,
     )
     result = self.bot.internal.messaging.LoadDialogs(request)
     response = self.bot.internal.updates.GetReferencedEntitites(
         sequence_and_updates_pb2.RequestGetReferencedEntitites(
             groups=list(result.group_peers)
         )
     ).groups
     return [x for x in response if x.self_member.uid != 0]
Ejemplo n.º 3
0
    def get_messages_by_id(self, mids: List[UUID
                                            or AsyncTask]) -> List[Message]:
        """Find and return messages by UUIDs

        :param mids: list of UUID or AsyncTask (in which located UUID)
        :return: list of Messages
        """
        mids = get_uuids(mids)
        request = sequence_and_updates_pb2.RequestGetReferencedEntitites(
            mids=mids)
        result = ReferencedEntities.from_api(
            self.internal.updates.GetReferencedEntitites(request))
        return result.messages
Ejemplo n.º 4
0
    def get_user_by_id(self, uid):
        req = messaging_pb2.RequestLoadDialogs(
            min_date=0,
            limit=1,
            peers_to_load=[
                peers_pb2.Peer(type=peers_pb2.PEERTYPE_PRIVATE, id=uid)
            ])
        result = self.internal.messaging.LoadDialogs(req)
        users_list = self.internal.updates.GetReferencedEntitites(
            sequence_and_updates_pb2.RequestGetReferencedEntitites(
                users=list(result.user_peers)))

        for user in users_list.users:
            if hasattr(user, "id") and user.id == uid:
                return user
        return None
Ejemplo n.º 5
0
 def get_nicks_from_ids(self, uids):
     uids = [int(uid) for uid in uids]
     request = search_pb2.RequestSimpleSearch(
         criteria=[
             search_pb2.SimpleSearchCondition(
                 userProfile=search_pb2.SimpleUserProfileSearchCondition(query_string=c)
             ) for c in ascii_lowercase
         ]
     )
     result = self.bot.internal.search.SimpleSearch(request)
     users_list = self.bot.internal.updates.GetReferencedEntitites(
         sequence_and_updates_pb2.RequestGetReferencedEntitites(
             users=list(result.user_out_peers)
         )
     )
     return [user.data.nick.value for user in users_list.users if int(user.id) in uids]
Ejemplo n.º 6
0
    def find_group_by_id(self, group_id: int) -> Group or None:
        """Find and return Group by id

        :param group_id: group's
        :return: Group or None if could not find
        """
        out_peer = self.__get_out_peer(Peer(group_id, PeerType.PEERTYPE_GROUP))
        if out_peer is None:
            return None

        request = sequence_and_updates_pb2.RequestGetReferencedEntitites(
            groups=[out_peer])
        result = self.internal.updates.GetReferencedEntitites(request).groups
        self.manager.add_out_peer(
            peers_pb2.OutPeer(id=result[0].id,
                              type=PeerType.PEERTYPE_GROUP,
                              access_hash=result[0].access_hash))
        return Group.from_api(result[0])
Ejemplo n.º 7
0
    def get_user_by_nick(self, nick):
        """Returns User object by nickname

        :param nick: user's nickname
        :return: User object
        """
        outpeer = self.find_user_outpeer_by_nick(nick)
        if not (outpeer.id and outpeer.access_hash):
            return

        result = self.internal.updates.GetReferencedEntitites(
            sequence_and_updates_pb2.RequestGetReferencedEntitites(users=[
                peers_pb2.UserOutPeer(uid=outpeer.id,
                                      access_hash=outpeer.access_hash)
            ]))
        for user in result.users:
            if hasattr(user.data, "nick") and user.data.nick.value == nick:
                return user
Ejemplo n.º 8
0
    def load_members(
        self,
        group_peer: Peer or AsyncTask,
        limit: int = 0,
        cursor: bytes = b"",
        cursor_flag: bool = False
    ) -> List[User] or List[User] and bytes or None:
        """Load Group members by peer

        :param group_peer: Peer or AsyncTask (in which located Group)
        :param limit: count members
        :param cursor: bytes object that specify to the user from whom to start (returned from this method)
        :param cursor_flag: returned cursor? (True/False)
        :return: list of User's
        """
        group_peer = get_peer(group_peer)
        out_peer = self.__get_out_peer(group_peer)
        if out_peer is None:
            return None

        request = groups_pb2.RequestLoadMembers(group=out_peer,
                                                limit=limit,
                                                next=BytesValue(value=cursor))
        response = self.internal.groups.LoadMembers(request)
        members = response.members
        cursor = response.cursor.value
        request = sequence_and_updates_pb2.RequestGetReferencedEntitites(
            group_members=[
                sequence_and_updates_pb2.GroupMembersSubset(
                    group_peer=out_peer,
                    member_ids=[member.uid for member in members])
            ])
        if cursor_flag:
            return [
                User.from_api(x) for x in
                self.internal.updates.GetReferencedEntitites(request).users
            ], cursor
        return [
            User.from_api(x) for x in
            self.internal.updates.GetReferencedEntitites(request).users
        ]
Ejemplo n.º 9
0
    def search_users_by_nick_substring(self, query):
        """Returns list of User objects by substring of nickname (not complete coincidence!)

        :param query: user's nickname
        :return: list User objects
        """
        user_peers = self.internal.search.PeerSearch(
            search_pb2.RequestPeerSearch(query=[
                search_pb2.SearchCondition(
                    searchPeerTypeCondition=search_pb2.SearchPeerTypeCondition(
                        peer_type=search_pb2.SEARCHPEERTYPE_CONTACTS)),
                search_pb2.SearchCondition(
                    searchPieceText=search_pb2.SearchPieceText(query=query))
            ])).user_peers
        users_list = self.internal.updates.GetReferencedEntitites(
            sequence_and_updates_pb2.RequestGetReferencedEntitites(
                users=list(user_peers)))
        result = []

        for user in users_list.users:
            if hasattr(user.data, "nick") and query in user.data.nick.value:
                result.append(user)
        return result
Ejemplo n.º 10
0
 def get_groups(self):
     contacts = self.bot.internal.messaging.LoadDialogs(
         messaging_pb2.RequestLoadDialogs()).group_peers
     return self.bot.internal.updates.GetReferencedEntitites(
         sequence_and_updates_pb2.RequestGetReferencedEntitites(
             groups=contacts)).groups