commit 2cc416ac008cbd295a5c1f56c133519444beb1ef Author: KKlochko <91247378+KKlochko@users.noreply.github.com> Date: Fri Jun 16 16:00:33 2023 +0200 Initial commit diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..74f39bf --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +/venv/ +/crypto_store/ +/session.txt +/.env +/.idea +/config.toml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..74f39bf --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +/venv/ +/crypto_store/ +/session.txt +/.env +/.idea +/config.toml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e46f477 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,10 @@ +FROM python:3.10-slim + +WORKDIR ./code + +COPY ./requirements.txt . + +RUN pip install python-olm --extra-index-url https://gitlab.matrix.org/api/v4/projects/27/packages/pypi/simple +RUN pip install -r requirements.txt + +COPY . . \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..25e78ee --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +### This is a template for writing a bot using simplematrixbotlib + +#### Setup + +First clone repo and create `.env` and `config.toml` + +Examples: + +`.env` +```dotenv +USERNAME=@catbot:example.com +PASSWORD=password +SERVER=https://example.com +``` + +[allowlist and blocklist formats](https://simple-matrix-bot-lib.readthedocs.io/en/latest/examples.html#id2) + +`config.toml` +```toml +[simplematrixbotlib.config] +allowlist = [] +blocklist = [] +admin_id = '@admin:example.com' +``` \ No newline at end of file diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..9549f61 --- /dev/null +++ b/config.toml @@ -0,0 +1,4 @@ +[simplematrixbotlib.config] +allowlist = [] +blocklist = [] +admin_id = '@admin:example.com' \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..832973f --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,12 @@ +version: '3' + +services: + bot: + build: . + volumes: + - '.:/code' + - './crypto_store/:/code/crypto_store/' + - './session.txt:/code/session.txt' + command: 'python src/main.py' + env_file: '.env' + diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ef20dfe --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +simplematrixbotlib~=2.8.0 +environs~=9.5.0 +aiohttp~=3.8.3 +python-olm +matrix-nio[e2e] diff --git a/src/handlers/__init__.py b/src/handlers/__init__.py new file mode 100644 index 0000000..438bcc7 --- /dev/null +++ b/src/handlers/__init__.py @@ -0,0 +1,14 @@ +import nio +import simplematrixbotlib as botlib + +from .help import help_handler + + +def setup(bot: botlib.Bot, prefix: str): + @bot.listener.on_message_event + async def help_command(room: nio.rooms.MatrixRoom, message: nio.events.room_events.Event): + match = botlib.MessageMatch(room, message, bot, prefix=prefix) + + if match.is_not_from_this_bot() and match.prefix() and match.command('help'): + await help_handler(bot=bot, room_id=room.room_id, sender=message.sender, admin_id=bot.config.admin_id) + diff --git a/src/handlers/help.py b/src/handlers/help.py new file mode 100644 index 0000000..96c677c --- /dev/null +++ b/src/handlers/help.py @@ -0,0 +1,10 @@ +import simplematrixbotlib as botlib + + +async def help_handler(room_id: str, bot: botlib.Bot, sender: str, admin_id: str): + msg: str = 'Hi **{}**\n\n' \ + 'Prefix: `!`\n' \ + 'Its a simplematrixbotlib template!\n' \ + 'Admin contact: **{}**' + + await bot.api.send_markdown_message(room_id=room_id, message=msg.format(sender, admin_id)) diff --git a/src/main.py b/src/main.py new file mode 100644 index 0000000..7fdfe5d --- /dev/null +++ b/src/main.py @@ -0,0 +1,40 @@ +from dataclasses import dataclass +from environs import Env +import simplematrixbotlib as botlib +from handlers import setup + +env = Env() +env.read_env() + +USERNAME = env('USERNAME') +PASSWORD = env('PASSWORD') +SERVER = env('SERVER') + + +@dataclass +class MyConfig(botlib.Config): + _admin_id: str = 'unknown' + _lang: str = 'en' + + @property + def admin_id(self) -> str: + return self._admin_id + + @admin_id.setter + def admin_id(self, value: str) -> None: + self._admin_id = value + + +config = MyConfig() +config.load_toml('config.toml') +config.encryption_enabled = True +config.emoji_verify = True +config.ignore_unverified_devices = True +config.store_path = './crypto_store/' + +creds = botlib.Creds(SERVER, USERNAME, PASSWORD) +bot = botlib.Bot(creds, config) +PREFIX = '!' + +setup(bot, prefix=PREFIX) +bot.run()