47 lines
1.5 KiB
Python
Raw Normal View History

2024-12-16 23:15:42 +01:00
from nio import AsyncClient, RoomMessageText
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
class BooeehBot:
def __init__(self, url, username, password):
self.url = url
self.username = username
self.password = password
self.client = AsyncClient(url, username)
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
async def login(self):
try:
response = await self.client.login(self.password)
print(f"User logged in: {self.username}")
return response
except Exception as e:
print(f"Login error: {e}")
return None
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
async def handle_message(self, room, event):
2024-12-16 23:19:22 +01:00
specific_user_id = "@joewilliams007:matrix.org"
2024-12-16 23:15:42 +01:00
if isinstance(event, RoomMessageText):
if event.sender == specific_user_id:
response_text = "booeeeh"
try:
await self.client.room_send(
room.room_id,
message_type="m.room.message",
2024-12-16 23:19:22 +01:00
content={"msgtype": "m.text", "body": response_text},
2024-12-16 23:15:42 +01:00
)
print(f"Response to {event.sender}: " + response_text)
except Exception as e:
print(f"Failed to send message: {e}")
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
async def start(self):
login_response = await self.login()
if not login_response:
return
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
self.client.add_event_callback(self.handle_message, RoomMessageText)
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
await self.client.sync_forever(timeout=30000)
2024-12-16 23:19:22 +01:00
2024-12-16 23:15:42 +01:00
async def stop(self):
await self.client.close()