generated from retoor/boeh
feat: scaffold boeh matrix bot with async client, env secrets, and makefile build pipeline
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
from nio import AsyncClient, RoomMessageText
|
||||
import random
|
||||
|
||||
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):
|
||||
self.url = url
|
||||
self.username = username
|
||||
self.password = password
|
||||
self.client = AsyncClient(url, username)
|
||||
|
||||
async def login(self):
|
||||
try:
|
||||
response = await self.client.login(self.password)
|
||||
print(f"Logged in. Serving {self.username}.")
|
||||
return response
|
||||
except Exception as e:
|
||||
print(f"Login error: {e}")
|
||||
return None
|
||||
|
||||
async def handle_message(self, room, event):
|
||||
specific_user_id = "@joewilliams007:matrix.org"
|
||||
|
||||
if isinstance(event, RoomMessageText):
|
||||
if event.sender == specific_user_id:
|
||||
response_text = self.generate_boeh()
|
||||
try:
|
||||
await self.client.room_send(
|
||||
room.room_id,
|
||||
message_type="m.room.message",
|
||||
content={"msgtype": "m.text", "body": response_text},
|
||||
)
|
||||
print(f"Response to {event.sender}: " + response_text)
|
||||
except Exception as e:
|
||||
print(f"Failed to send message: {e}")
|
||||
|
||||
async def start(self):
|
||||
login_response = await self.login()
|
||||
if not login_response:
|
||||
return
|
||||
|
||||
self.client.add_event_callback(self.handle_message, RoomMessageText)
|
||||
|
||||
await self.client.sync_forever(timeout=30000)
|
||||
|
||||
async def stop(self):
|
||||
await self.client.close()
|
||||
@@ -0,0 +1,23 @@
|
||||
import asyncio
|
||||
|
||||
from boeh import BooeehBot, env
|
||||
|
||||
|
||||
async def main_async():
|
||||
url = "https://matrix.org"
|
||||
username = "@retoor2:matrix.org"
|
||||
password = env.secret4
|
||||
bot = BooeehBot(url, username, password)
|
||||
|
||||
try:
|
||||
await bot.start()
|
||||
except KeyboardInterrupt:
|
||||
await bot.stop()
|
||||
|
||||
|
||||
def main():
|
||||
asyncio.run(main_async())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,29 @@
|
||||
import base64
|
||||
import os
|
||||
|
||||
secret = None
|
||||
secret2 = None
|
||||
secret3 = None
|
||||
secret4 = None
|
||||
|
||||
if __name__ == "__main__":
|
||||
secret = input("Type secret: ")
|
||||
print(base64.b64encode(secret.encode()).decode())
|
||||
else:
|
||||
|
||||
try:
|
||||
secret = base64.b64decode(os.getenv("SECRET", "").encode()).decode()
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
secret2 = base64.b64decode(os.getenv("SECRET2", "").encode()).decode()
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
secret3 = base64.b64decode(os.getenv("SECRET3", "").encode()).decode()
|
||||
except:
|
||||
pass
|
||||
try:
|
||||
secret4 = base64.b64decode(os.getenv("SECRET4", "").encode()).decode()
|
||||
except:
|
||||
pass
|
||||
Reference in New Issue
Block a user