This commit is contained in:
retoor 2024-12-29 17:39:57 +01:00
parent d74e68d593
commit 4f9e7ada33
5 changed files with 62 additions and 143 deletions

View File

@ -1,59 +0,0 @@
import random
from nio import AsyncClient, RoomMessageText
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()

View File

@ -1,23 +0,0 @@
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()

View File

@ -1,29 +0,0 @@
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

View File

@ -1,8 +1,36 @@
# Written by retoor@molodetz.nl
# This script initializes a web application using aiohttp and runs it asynchronously with a thread pool executor.
# Imports aiohttp for web server functionality and concurrent.futures for handling asynchronous execution.
#
# MIT License
#
# Copyright (c) 2023 Future Contributor
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import asyncio import asyncio
from concurrent.futures import ThreadPoolExecutor as Executor from concurrent.futures import ThreadPoolExecutor as Executor
from aiohttp import web from aiohttp import web
from rwebgui.app import Application from rwebgui.app import Application

View File

@ -1,18 +1,41 @@
# Written by retoor@molodetz.nl
# This module defines a Component class that facilitates WebSocket communication and management of various tasks in an asynchronous environment. It allows dynamic creation of child components and interaction through callbacks and events.
# Imports used: aiohttp (external library for client-server communication)
# MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import asyncio import asyncio
import time import time
import uuid import uuid
from aiohttp import web from aiohttp import web
class Component: class Component:
@classmethod @classmethod
def define(cls): def define(cls):
return cls return cls
def __init__(self, app, id_, description=None, ws: web.WebSocketResponse = None): def __init__(self, app, id_, description=None, ws: web.WebSocketResponse = None):
self.id = id_ self.id = id_
self.ws = ws self.ws = ws
self.app = app self.app = app
@ -23,12 +46,11 @@ class Component:
self._running = False self._running = False
if not hasattr(self, "Children"): if not hasattr(self, "Children"):
return return
for name in dir(self.Children): for name in dir(self.Children):
if name.startswith("__"): if name.startswith("__"):
continue continue
obj = getattr(self.Children, name) obj = getattr(self.Children, name)
instance = obj(app=self.app, id_=name, ws=ws) instance = obj(app=self.app, id_=name, ws=ws)
self.add_child(instance) self.add_child(instance)
instance.app = self instance.app = self
@ -64,19 +86,15 @@ class Component:
if name.startswith("task_") and hasattr(self, name) if name.startswith("task_") and hasattr(self, name)
] ]
for child in self.children: for child in self.children:
tasks_ += child.tasks # .extend(await child.get_tasks()) tasks_ += child.tasks
return tasks_ return tasks_
async def communicate(self, event_id=None): async def communicate(self, event_id=None):
async for msg in self.ws: async for msg in self.ws:
if msg.type == web.WSMsgType.TEXT: if msg.type == web.WSMsgType.TEXT:
# Echo the message back to the client
# print(f"Received message: {msg.data}")
data = msg.json() data = msg.json()
if not event_id: if not event_id:
pass pass
# return data
else: else:
if data.get("event_id") == event_id: if data.get("event_id") == event_id:
return data return data
@ -91,7 +109,6 @@ class Component:
if hasattr(self, method_name): if hasattr(self, method_name):
method = getattr(self, method_name) method = getattr(self, method_name)
await method(data) await method(data)
print("JAAJ")
for child in self.children: for child in self.children:
await child.trigger(id_, event, data) await child.trigger(id_, event, data)
@ -121,11 +138,8 @@ class Component:
if callback: if callback:
response = await self.communicate(event_id=event_id) response = await self.communicate(event_id=event_id)
return response["result"] return response["result"]
# print("GLUKT")
# return response['result']
return True return True
# return await future
async def get_attr(self, key, default=None): async def get_attr(self, key, default=None):
result = await self.call("getAttr", [self.id, key], True) result = await self.call("getAttr", [self.id, key], True)
@ -157,7 +171,6 @@ class Component:
async def toggle(self): async def toggle(self):
value = await self.get_style("display", "block") value = await self.get_style("display", "block")
if value == "none": if value == "none":
value = "" value = ""
else: else:
@ -179,7 +192,7 @@ class Component:
async def get_tasks(self): async def get_tasks(self):
tasks = self.tasks tasks = self.tasks
for child in self.children: for child in self.children:
tasks += child.tasks # .extend(await child.get_tasks()) tasks += child.tasks
return tasks return tasks
async def set_running(self): async def set_running(self):
@ -197,8 +210,6 @@ class Component:
try: try:
async for msg in self.ws: async for msg in self.ws:
if msg.type == web.WSMsgType.TEXT: if msg.type == web.WSMsgType.TEXT:
# Echo the message back to the client
# print(f"Received message: {msg.data}")
data = msg.json() data = msg.json()
response = {"event_id": data["event_id"], "success": True} response = {"event_id": data["event_id"], "success": True}
response["time_start"] = time.time() response["time_start"] = time.time()
@ -218,23 +229,14 @@ class Component:
response["time_end"] - response["time_start"] response["time_end"] - response["time_start"]
) )
await self.ws.send_json(response) await self.ws.send_json(response)
# await ws.send_str(f"Echo: {msg.data}")
elif msg.type == web.WSMsgType.ERROR: elif msg.type == web.WSMsgType.ERROR:
print(f"WebSocket error: {self.ws.exception()}") print(f"WebSocket error: {self.ws.exception()}")
except Exception as ex: except Exception as ex:
print(ex) print(ex)
pass pass
# async def the_task():
# while True:
# time.sleep(1)
# while True:
tasks.append(events) tasks.append(events)
await asyncio.gather(*[task() for task in tasks]) await asyncio.gather(*[task() for task in tasks])
# await asyncio.create_task(asyncio.gather(*[task() for task in tasks]))
# await tasks()
print("AFTERR")
def add_child(self, child): def add_child(self, child):
child.app = self.app child.app = self.app