From 00925a640b4da7b58b4a05158a320f0f66b06fcd Mon Sep 17 00:00:00 2001 From: Joao Figueiredo Date: Mon, 28 Apr 2025 01:26:33 +0100 Subject: [PATCH] now responds too a message --- .env.example | 5 ++++- Dockerfile | 6 ++++++ main.py | 19 +++++++++++++++++-- requirements.txt | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index ca01beb..45a2dcd 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,7 @@ HOMESERVER_URL = "https://matrix.org" USER_ID = "@botbot_user:matrix.org" PASSWORD = "botbot_password" -LOG_LEVEL=INFO \ No newline at end of file +LOG_LEVEL=INFO + +# comma-separated list of rooms or aliases to join on startup +BOT_ROOMS=!room1:matrix.org,!another-room:matrix.org,!someroomalias:matrix.org \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index b276d97..8713cae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,6 +3,12 @@ FROM python:3.11-slim # Set working directory WORKDIR /app +# Install system dependencies +RUN apt-get update && \ + apt-get install -y --no-install-recommends \ + libolm-dev build-essential python3-dev && \ + rm -rf /var/lib/apt/lists/* + # Install dependencies COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt diff --git a/main.py b/main.py index fd86fb4..213cd69 100644 --- a/main.py +++ b/main.py @@ -28,7 +28,8 @@ async def message_callback(room: MatrixRoom, event: RoomMessageText): logger.debug("Message callback triggered") if event.sender == USER_ID: return - + + body = event.body.strip().lower() logger.info("Message from %s in %s: %s", event.sender, room.display_name, event.body) if event.body.strip().lower() == "!ping": @@ -38,13 +39,27 @@ async def message_callback(room: MatrixRoom, event: RoomMessageText): content={"msgtype": "m.text", "body": "Pong!"} ) logger.info("Replied with Pong! to %s", event.sender) + elif body== "hello botbot": + await client.room_send( + room_id=room.room_id, + message_type="m.room.message", + content={ + "msgtype": "m.text", + "body": "Hello! How can I assist you today?", + }, + ignore_unverified_devices=True + ) + logger.info("Replied with greeting to %s", event.sender) async def main(): global client logger.debug(HOMESERVER_URL) logger.debug(USER_ID) # Configure client with persistent store - config = AsyncClientConfig(store_sync_tokens=True) + config = AsyncClientConfig( + store_sync_tokens=True, + encryption_enabled=True, + ) client = AsyncClient( HOMESERVER_URL, USER_ID, diff --git a/requirements.txt b/requirements.txt index e7d6f44..3dae8f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,2 @@ -matrix-nio[aio,http]>=0.25.0 +matrix-nio[e2e]>=0.25.0 python-dotenv>=1.0.0 \ No newline at end of file