Add room invite handling
This commit is contained in:
parent
749b00878e
commit
e5a9f85752
1 changed files with 22 additions and 0 deletions
|
|
@ -9,6 +9,7 @@ from nio import (
|
||||||
RoomMessageText,
|
RoomMessageText,
|
||||||
ReactionEvent,
|
ReactionEvent,
|
||||||
RoomSendResponse,
|
RoomSendResponse,
|
||||||
|
InviteMemberEvent
|
||||||
)
|
)
|
||||||
|
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
|
@ -79,6 +80,24 @@ def add_user_to_kc_group(username: str, group_name: str) -> tuple[bool, str]:
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
# Matrix Event Handlers
|
# Matrix Event Handlers
|
||||||
# ------------------------------------------------------------------------------
|
# ------------------------------------------------------------------------------
|
||||||
|
async def on_invite(client: AsyncClient, room: MatrixRoom, event: InviteMemberEvent):
|
||||||
|
"""Auto-joins rooms when invited by an authorized user/homeserver."""
|
||||||
|
# Ensure the invite is actually meant for this bot
|
||||||
|
if event.state_key != client.user_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Only accept invites from my domain
|
||||||
|
if not event.sender.endswith(":starling.mynest.love"): return
|
||||||
|
|
||||||
|
print(f"Received invite for room {room.room_id} from {event.sender}. Joining...")
|
||||||
|
|
||||||
|
response = await client.join(room.room_id)
|
||||||
|
if hasattr(response, "room_id"):
|
||||||
|
print(f"Successfully joined room {room.room_id}")
|
||||||
|
else:
|
||||||
|
print(f"Failed to join room {room.room_id}: {response}")
|
||||||
|
|
||||||
|
|
||||||
async def on_message(client: AsyncClient, room: MatrixRoom, event: RoomMessageText):
|
async def on_message(client: AsyncClient, room: MatrixRoom, event: RoomMessageText):
|
||||||
"""Listens for `!request <group_name>` commands."""
|
"""Listens for `!request <group_name>` commands."""
|
||||||
# Ignore messages sent by the bot itself
|
# Ignore messages sent by the bot itself
|
||||||
|
|
@ -231,6 +250,9 @@ async def main():
|
||||||
client.add_event_callback(
|
client.add_event_callback(
|
||||||
lambda room, event: on_reaction(client, room, event), ReactionEvent
|
lambda room, event: on_reaction(client, room, event), ReactionEvent
|
||||||
)
|
)
|
||||||
|
client.add_event_callback(
|
||||||
|
lambda room, event: on_invite(client, room, event), InviteMemberEvent
|
||||||
|
)
|
||||||
|
|
||||||
print(f"Connecting to Matrix homeserver {MATRIX_HOMESERVER}...")
|
print(f"Connecting to Matrix homeserver {MATRIX_HOMESERVER}...")
|
||||||
await client.login(MATRIX_BOT_PASSWORD)
|
await client.login(MATRIX_BOT_PASSWORD)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue