@@ -482,6 +482,45 @@ async def leave_room(
482482 if "not in room" not in e .message or raise_not_in_room :
483483 raise
484484
485+ async def knock_room (
486+ self ,
487+ room_id_or_alias : RoomID | RoomAlias ,
488+ reason : str | None = None ,
489+ servers : list [str ] | None = None ,
490+ ) -> RoomID :
491+ """
492+ Knock on a room, i.e. request to join it by its ID or alias, with an optional list of
493+ servers to ask about the ID from.
494+
495+ See also: `API reference <https://spec.matrix.org/v1.3/client-server-api/#post_matrixclientv3knockroomidoralias>`__
496+
497+ Args:
498+ room_id_or_alias: The ID of the room to knock on, or an alias pointing to the room.
499+ reason: The reason for knocking on the room. This will be supplied as the ``reason`` on
500+ the updated `m.room.member`_ event.
501+ servers: A list of servers to ask about the room ID to knock. Not applicable for aliases,
502+ as aliases already contain the necessary server information.
503+
504+ Returns:
505+ The ID of the room the user knocked on.
506+ """
507+ data = {}
508+ if reason :
509+ data ["reason" ] = reason
510+ query_params = CIMultiDict ()
511+ for server_name in servers or []:
512+ query_params .add ("server_name" , server_name )
513+ content = await self .api .request (
514+ Method .POST ,
515+ Path .v3 .knock [room_id_or_alias ],
516+ content = data ,
517+ query_params = query_params ,
518+ )
519+ try :
520+ return content ["room_id" ]
521+ except KeyError :
522+ raise MatrixResponseError ("`room_id` not in response." )
523+
485524 async def forget_room (self , room_id : RoomID ) -> None :
486525 """
487526 Stop remembering a particular room, i.e. forget it.
0 commit comments