]> apps.delaporte.us Git - matrix-chore-bot.git/commitdiff
Update to support separate prefix and db file
authorEdward Delaporte <edward@delaporte.us>
Tue, 7 Apr 2026 00:52:56 +0000 (19:52 -0500)
committerEdward Delaporte <edward@delaporte.us>
Tue, 7 Apr 2026 00:52:56 +0000 (19:52 -0500)
.gitignore
app.py
matrix_chore_bot/bot.py
matrix_chore_bot/db.py

index 1fd634e4f93e021f4e2580c493f3ef78eb52c650..b43ba60d7aff935b34e865dde7c2c9f1b7114785 100644 (file)
@@ -1,6 +1,9 @@
 # Python does that
 __pycache__
 
+# Database
+*.db
+
 # Makefile
 .venv
 *.egg-info
diff --git a/app.py b/app.py
index fcc26524e1e055b4c0989906d08f72a825b9040d..682b96afefbe40273ef68b2ea1122f31c5adaeba 100644 (file)
--- a/app.py
+++ b/app.py
@@ -1,22 +1,28 @@
 import os
 
-import simplematrixbotlib as botlib
+from matrix_chore_bot.bot import ChoreBot
 
-from matrix_chore_bot.bot import chore_bot
+kwargs = {}
+# How to connect to Matrix
+kwargs['matrix_server'] = os.environ.get('MATRIX_SERVER')
+kwargs['bot_name'] = os.environ.get('MATRIX_BOT')
+kwargs['bot_pwd'] = os.environ.get('MATRIX_BOT_PWD')
 
-SERVER = os.environ.get('MATRIX_SERVER')
-BOT_NAME = os.environ.get('MATRIX_BOT')
-BOT_PWD = os.environ.get('MATRIX_BOT_PWD')
-BOT_PREFIX = os.environ.get('MATRIX_BOT_PREFIX', '!chore')
+# Prefix to listen for
+kwargs['bot_prefix'] = os.environ.get('MATRIX_BOT_PREFIX', '!chore')
 
-def get_bot():
-    creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
-    return botlib.Bot(creds)
+# Where to store data
+DATA_DIR = os.environ.get('MATRIX_DATA_DIR', '')
+DB_FILE = os.environ.get('MATRIX_DB_FILE', 'chore.db')
+kwargs['db_file'] = os.path.join(DATA_DIR, DB_FILE)
 
-bot = get_bot()
+print(kwargs)
 
-@bot.listener.on_message_event
+chore_bot = ChoreBot(**kwargs) 
+
+@chore_bot.bot.listener.on_message_event
 async def do_bot_stuff(room, message):
-    await chore_bot(room, message, bot, BOT_PREFIX)
+    '''Pass messages to our bot'''
+    await chore_bot.handle_message(room, message)
 
-bot.run()
+chore_bot.run()
index 067c2f906d196170c07c95456c669484ce0dc74c..c1a76d336464253f3ca51bee7b8f2f48ec9da6e3 100644 (file)
@@ -1,3 +1,19 @@
+'''
+Prefix: !chore
+
+Synax:
+!chore COMMAND ARGS
+
+Commands:
+help - show this help
+add - add a chore 
+check - check status of a chore
+done - mark a chore as done
+list - print the chore list
+filter TERM - list all chores that include TERM
+undo - mark a chore as undone
+'''
+
 from datetime import datetime as dt
 import logging
 import os
@@ -6,100 +22,94 @@ import simplematrixbotlib as botlib
 
 from .db import Data as ChoreData
 
-SERVER = os.environ.get('MATRIX_SERVER')
-BOT_NAME = os.environ.get('MATRIX_BOT')
-BOT_PWD = os.environ.get('MATRIX_BOT_PWD')
-
-CHORES = ChoreData()
-
 logger = logging.getLogger(__name__)
 logging.basicConfig(filename='matrix-chore-bot.log', encoding='utf-8', level=logging.DEBUG)
 logging.getLogger('nio').setLevel(logging.WARNING)
 
-async def chore_bot(room, message, bot, prefix='!chore'):
-    match = botlib.MessageMatch(room, message, bot, prefix)
+class ChoreBot():
+    def __init__(self, matrix_server,
+                 bot_name,
+                 bot_pwd,
+                 db_file=None,
+                 bot_prefix='!chore'):
+
+        creds = botlib.Creds(matrix_server, bot_name, bot_pwd)
+        self.bot = botlib.Bot(creds)
+        self.prefix = bot_prefix
+        self.data = ChoreData(db_file=db_file)
+
+    def run(self):
+        self.bot.run()
+
+    async def handle_message(self, room, message):
+        match = botlib.MessageMatch(room, message, self.bot, self.prefix)
+
+        if not match.is_not_from_this_bot() and match.prefix():
+            return
+
+        if match.prefix():
+            try:
+                result_lines = await self._handle_match(match)
+                for line in result_lines:
+                    await self.bot.api.send_text_message(
+                        room.room_id, str(line))
+            except Exception as ex:
+                line = f'Ye flask must be load bearing, or something... {str(ex)}'
+                await self.bot.api.send_text_message(
+                    room.room_id, str(line))
 
-    if not match.is_not_from_this_bot() and match.prefix():
-        return
 
-    if match.prefix():
-        try:
-            result_lines = await handle_chore(match)
-            for line in result_lines:
-                await bot.api.send_text_message(
-                    room.room_id, str(line))
-        except Exception as ex:
-            line = f'Ye flask must be load bearing, or something... {str(ex)}'
-            await bot.api.send_text_message(
-                room.room_id, str(line))
-
-
-async def handle_chore(match):
-    '''
-    Prefix: !chore
-
-    Synax:
-        !chore COMMAND ARGS
-
-    Commands:
-      help - show this help
-      add - add a chore 
-      check - check status of a chore
-      done - mark a chore as done
-      list - print the chore list
-      filter TERM - list all chores that include TERM
-      undo - mark a chore as undone
-    '''
-
-    if match.command('list'):
-        results = ['## Listing all...']
-        for item in CHORES.list():
-            results.append(item)
-        results.append('-- End of List --')
-        return results
-
-    if match.command('filter'):
-        term = ' '.join(match.args()[1:])
-        results = [f'## List matching {term}']
-        for item in CHORES.filter(term):
-            results.append(item)
-        results.append('-- End of List --')
-        return results
-    
-    if match.command('setup'):
-        results = []
-        results.append('Doing chore setup...')
-        result = CHORES.setup()
-        results.append(str(result))
-        return results
-
-    if match.command('add'):
-        new_chore = ' '.join(match.args()[1:])
-        result = CHORES.add(new_chore)
-        return [result]
-
-    if match.command('check'):
-        chore_id = match.args()[1]
-        result = CHORES.check(chore_id)
-        return [result]
-
-    if match.command('done'):
-        ids = match.args()[1:]
-        results = []
-        for chore_id in ids:
-            result = CHORES.done(chore_id)
-            results.append(result)
-        return results
-
-    if match.command('undo'):
-        chore_id = match.args()[1]
-        result = CHORES.undo(chore_id)
-        return [result]
-
-    if match.command('remove'):
-        chore_id = match.args()[1]
-        result = CHORES.remove(chore_id)
-        return [result]
-
-    return [handle_chore.__doc__]
+    async def _handle_match(self, match):
+
+        if match.command('list'):
+            results = ['## Listing all...']
+            for item in self.data.list():
+                results.append(item)
+            results.append('-- End of List --')
+            return results
+
+        if match.command('filter'):
+            term = ' '.join(match.args()[1:])
+            results = [f'## List matching {term}']
+            for item in self.data.filter(term):
+                results.append(item)
+            results.append('-- End of List --')
+            return results
+        
+        if match.command('setup'):
+            results = []
+            results.append('Doing chore setup...')
+            result = self.data.setup()
+            results.append(str(result))
+            return results
+
+        if match.command('add'):
+            new_chore = ' '.join(match.args()[1:])
+            result = self.data.add(new_chore)
+            return [result]
+
+        if match.command('check'):
+            chore_id = match.args()[1]
+            result = self.data.check(chore_id)
+            return [result]
+
+        if match.command('done'):
+            ids = match.args()[1:]
+            results = []
+            for chore_id in ids:
+                result = self.data.done(chore_id)
+                results.append(result)
+            return results
+
+        if match.command('undo'):
+            chore_id = match.args()[1]
+            result = self.data.undo(chore_id)
+            return [result]
+
+        if match.command('remove'):
+            chore_id = match.args()[1]
+            result = self.data.remove(chore_id)
+            return [result]
+
+        return [__doc__]
 
index 140f0a682b8ad4c99b530e4e66655df9bd63f6ce..1a07bec5af581eae1c1f12d5be2420265443ac5c 100644 (file)
@@ -13,9 +13,6 @@ DATE_FORMAT = '%Y-%m-%d'
 def today():
     return dt.now().strftime(DATE_FORMAT)
 
-DEFAULT_DB_FILE = os.path.join(os.environ.get('MATRIX_DATA_DIR', ''), 'chores.db')
-DB_FILE = os.environ.get('MATRIX_CHORE_DB_FILE', DEFAULT_DB_FILE)
-
 logger = logging.getLogger(__name__)
 logging.basicConfig(filename='chore.log', encoding='utf-8', level=logging.DEBUG)