Formatting.

This commit is contained in:
retoor 2024-12-16 23:19:22 +01:00
parent b745d310fd
commit a28df0371f
2 changed files with 17 additions and 21 deletions

View File

@ -1,14 +1,13 @@
import asyncio
import os
from nio import AsyncClient, RoomMessageText from nio import AsyncClient, RoomMessageText
class BooeehBot: class BooeehBot:
def __init__(self, url, username, password): def __init__(self, url, username, password):
self.url = url self.url = url
self.username = username self.username = username
self.password = password self.password = password
self.client = AsyncClient(url, username) self.client = AsyncClient(url, username)
async def login(self): async def login(self):
try: try:
response = await self.client.login(self.password) response = await self.client.login(self.password)
@ -17,10 +16,10 @@ class BooeehBot:
except Exception as e: except Exception as e:
print(f"Login error: {e}") print(f"Login error: {e}")
return None return None
async def handle_message(self, room, event): async def handle_message(self, room, event):
specific_user_id = '@joewilliams007:matrix.org' specific_user_id = "@joewilliams007:matrix.org"
if isinstance(event, RoomMessageText): if isinstance(event, RoomMessageText):
if event.sender == specific_user_id: if event.sender == specific_user_id:
response_text = "booeeeh" response_text = "booeeeh"
@ -28,26 +27,20 @@ class BooeehBot:
await self.client.room_send( await self.client.room_send(
room.room_id, room.room_id,
message_type="m.room.message", message_type="m.room.message",
content={ content={"msgtype": "m.text", "body": response_text},
"msgtype": "m.text",
"body": response_text
}
) )
print(f"Response to {event.sender}: " + response_text) print(f"Response to {event.sender}: " + response_text)
except Exception as e: except Exception as e:
print(f"Failed to send message: {e}") print(f"Failed to send message: {e}")
async def start(self): async def start(self):
login_response = await self.login() login_response = await self.login()
if not login_response: if not login_response:
return return
self.client.add_event_callback(self.handle_message, RoomMessageText) self.client.add_event_callback(self.handle_message, RoomMessageText)
await self.client.sync_forever(timeout=30000) await self.client.sync_forever(timeout=30000)
async def stop(self): async def stop(self):
await self.client.close() await self.client.close()

View File

@ -1,20 +1,23 @@
import asyncio import asyncio
from boeh import env
from boeh import BooeehBot from boeh import BooeehBot, env
async def main_async(): async def main_async():
url = "https://matrix.org" url = "https://matrix.org"
username = "@retoor2:matrix.org" username = "@retoor2:matrix.org"
password = env.secret4 password = env.secret4
bot = BooeehBot(url, username, password) bot = BooeehBot(url, username, password)
try: try:
await bot.start() await bot.start()
except KeyboardInterrupt: except KeyboardInterrupt:
await bot.stop() await bot.stop()
def main(): def main():
asyncio.run(main_async()) asyncio.run(main_async())
if __name__ == "__main__": if __name__ == "__main__":
main() main()