Compare commits
5
Commits
b745d310fd
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c47c0f166 | ||
|
|
3d6ad4bef0 | ||
|
|
40ce71244f | ||
|
|
6f0be04dd9 | ||
|
|
b9f2c55d75 |
+16
-12
@@ -1,8 +1,18 @@
|
|||||||
import asyncio
|
|
||||||
import os
|
|
||||||
from nio import AsyncClient, RoomMessageText
|
from nio import AsyncClient, RoomMessageText
|
||||||
|
import random
|
||||||
|
|
||||||
class BooeehBot:
|
class BooeehBot:
|
||||||
|
|
||||||
|
def generate_boeh(self):
|
||||||
|
boeh = "b"
|
||||||
|
for _ in range(random.randint(1, 10)):
|
||||||
|
boeh += "o"
|
||||||
|
for _ in range(random.randint(1, 5)):
|
||||||
|
boeh += "e"
|
||||||
|
for _ in range(random.randint(1, 3)):
|
||||||
|
boeh += "e"
|
||||||
|
return boeh
|
||||||
|
|
||||||
def __init__(self, url, username, password):
|
def __init__(self, url, username, password):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.username = username
|
self.username = username
|
||||||
@@ -12,26 +22,23 @@ class BooeehBot:
|
|||||||
async def login(self):
|
async def login(self):
|
||||||
try:
|
try:
|
||||||
response = await self.client.login(self.password)
|
response = await self.client.login(self.password)
|
||||||
print(f"User logged in: {self.username}")
|
print(f"Logged in. Serving {self.username}.")
|
||||||
return response
|
return response
|
||||||
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 = self.generate_boeh()
|
||||||
try:
|
try:
|
||||||
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:
|
||||||
@@ -48,6 +55,3 @@ class BooeehBot:
|
|||||||
|
|
||||||
async def stop(self):
|
async def stop(self):
|
||||||
await self.client.close()
|
await self.client.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
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"
|
||||||
@@ -13,8 +14,10 @@ async def main_async():
|
|||||||
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()
|
||||||
Reference in New Issue
Block a user