From e5f12d0ae596c2127b9630b29e3d48933d2eb937 Mon Sep 17 00:00:00 2001 From: retoor Date: Mon, 16 Dec 2024 23:15:42 +0100 Subject: [PATCH] first commit --- .gitignore | 166 +++++++++++++++++++++++++++++++++++++++++++ Makefile | 28 ++++++++ pyproject.toml | 3 + setup.cfg | 25 +++++++ src/boeh/__init__.py | 53 ++++++++++++++ src/boeh/__main__.py | 20 ++++++ src/boeh/env.py | 29 ++++++++ 7 files changed, 324 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 pyproject.toml create mode 100644 setup.cfg create mode 100644 src/boeh/__init__.py create mode 100644 src/boeh/__main__.py create mode 100644 src/boeh/env.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8cb2598 --- /dev/null +++ b/.gitignore @@ -0,0 +1,166 @@ +.vscode +.history +*.db* + +# ---> Python +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +#.idea/ + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6f44c5b --- /dev/null +++ b/Makefile @@ -0,0 +1,28 @@ +BIN = ./.venv/bin/ +PYTHON = ./.venv/bin/python +PIP = ./.venv/bin/pip +APP_NAME=boeh + +all: install build + +ensure_repo: + -@git init + +ensure_env: ensure_repo + -@python3 -m venv .venv + +install: ensure_env + $(PIP) install -e . + +format: + $(PIP) install shed + . $(BIN)/activate && shed + +build: + $(MAKE) format + $(PIP) install build + $(PYTHON) -m build + +run: + $(BIN)$(APP_NAME) --port=3028 + diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..07de284 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools", "wheel"] +build-backend = "setuptools.build_meta" \ No newline at end of file diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..42a92f3 --- /dev/null +++ b/setup.cfg @@ -0,0 +1,25 @@ +[metadata] +name = boeh +version = 1.0.0 +description = Service that says boeh when Joe talks. +author = retoor +author_email = retoor@molodetz.nl +license = MIT +long_description = file: README.md +long_description_content_type = text/markdown + +[options] +packages = find: +package_dir = + = src +python_requires = >=3.7 +install_requires = + app @ git+https://retoor.molodetz.nl/retoor/app + matrix-nio + +[options.packages.find] +where = src + +[options.entry_points] +console_scripts = + boeh = boeh.__main__:main diff --git a/src/boeh/__init__.py b/src/boeh/__init__.py new file mode 100644 index 0000000..fd7c294 --- /dev/null +++ b/src/boeh/__init__.py @@ -0,0 +1,53 @@ +import asyncio +import os +from nio import AsyncClient, RoomMessageText + +class BooeehBot: + 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"User logged in: {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 = "booeeeh" + 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() + + + diff --git a/src/boeh/__main__.py b/src/boeh/__main__.py new file mode 100644 index 0000000..804a2d6 --- /dev/null +++ b/src/boeh/__main__.py @@ -0,0 +1,20 @@ +import asyncio +from boeh import env +from boeh import BooeehBot + +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() \ No newline at end of file diff --git a/src/boeh/env.py b/src/boeh/env.py new file mode 100644 index 0000000..4869bc7 --- /dev/null +++ b/src/boeh/env.py @@ -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