neldierto
neldierto

Reputation: 31

Matrix Synapse: Can't join room

my question is regarding Matrix Synapse. Does anyone have an idea why I always get a 404 error with the message {"errcode":"M_UNKNOWN","error":"Can't join remote room because no servers that are in the room have been provided."} when I try to join a room? I have searched the internet and even asked ChatGPT, but I haven't found a solution.

I have tried two methods:

Method 1:

    def join_room(self, room_id):
        servers = ["matrix.example.com"]
        try:
            client = MatrixClient(self.server_url, token=self.access_token)

            client.api.federation_servers = servers

            room = client.join_room(room_id)

            client.logout()
        except Exception as e:
            print("An error occurred while joining the room:", str(e))

Method 2:

    def join_room(self, room_id):
        # Join room URL
        room_id = urllib.parse.quote(room_id)
        join_room_url = f"{self.server_url}/_matrix/client/r0/rooms/{room_id}/join?access_token={self.access_token}"

        # Send POST request to join room
        response = requests.post(join_room_url)

        # Check if room joining was successful
        if response.status_code == 200:
            print("Room joining successful!")
            # Perform further actions on the server here
        else:
            print("Room joining failed. Code: " + str(response.status_code) + " " + str(response.content))

I get the same error with both methods. I am able to successfully log in, log out, create rooms, leave rooms, send messages to a room, and read messages from a room. The room ID looks like this: !JaElJiWesZHJMYUZGi:HOMESERVER_URL.

Does anyone have an idea why this problem exists and how I can fix it?

Upvotes: 1

Views: 1430

Answers (1)

neldierto
neldierto

Reputation: 31

I found a solution by my self. The problem was that the user i want to join the room with had no invitation to the room although it was a public room.

To solve this problem i created a new room with user1 and then invite user2:

    async def invite(
        self,
        room_id,
        user_id,
):
    """Invite a user to a room.

    :param room_id: The room id of the room to invite the user to.
    :type room_id: str
    :param user_id: The user id of the user to invite.
    :type user_id: str
    :return: True if the request was successful, False otherwise.
    :rtype: bool
    """
    path = ["rooms", room_id, "invite"]
    query_parameters = {"access_token": self.access_token}
    body = {"user_id": user_id}

    url = f"{self.server_url}{Api._build_path(path, query_parameters)}"
    response = requests.post(url, json=body)

    if response.status_code == 200:
        print('User invited successfully:', response.json().get('user_id'))
    else:
        print('Failed to invite user:', response.text)

    return response.ok

My code uses a class that stores the access token and the home server url. Thats why i use self.access_token.

After i invited the user2 i have to login with user2 and call the join endpoint with the room id, like i did in the question.

The docu of the python nio library that i used to design my functions you can find here: https://matrix-nio.readthedocs.io/en/latest/_modules/nio/api.html

Upvotes: 1

Related Questions