]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - bot.py
0a40532ed72c217d7e81a2aeceb20efb071c5387
[matrix-goal-bot.git] / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from goals.db import Data
8
9
10 SERVER = os.environ.get('MATRIX_SERVER')
11 BOT_NAME = os.environ.get('GOAL_BOT')
12 BOT_PWD = os.environ.get('GOAL_BOT_PWD')
13
14 DATE_FORMAT = '%Y-%m-%d'
15
16 def today():
17 return dt.now().strftime(DATE_FORMAT)
18
19 creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
20 bot = botlib.Bot(creds)
21 PREFIX = '!goal'
22
23 logger = logging.getLogger(__name__)
24 logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG)
25 logging.getLogger('nio').setLevel(logging.WARNING)
26
27 async def do_goal(match):
28 '''
29 Bot help here?
30
31 '''
32 # logger.debug(f'match {str(match)}')
33 if match.command('help'):
34 return 'help'
35
36 if match.command('setup'):
37 result = Data().setup()
38 return result
39
40 if match.command('add'):
41 new = ' '.join(match.args()[1:])
42 result = Data().add_goal(new)
43 return result
44
45 if match.command('list'):
46 goals = Data().list_goals()
47 return goals
48
49 if match.command('add-task'):
50 result = Data().add_task(goal_id=goal_id, name=name)
51 return result
52
53 if match.command('print'):
54 goal_id = new = match.args()[1]
55 goal,tasks = Data().get_goal(id)
56
57 results = []
58 results.append(f' ## {goal}')
59 for task in tasks:
60 if(task.complete_date):
61 results.append(f' - [x] {task}')
62 else:
63 results.append(f' - [ ] {task}')
64
65 return 'Ye flask must be load bearing or something.'
66
67
68 @bot.listener.on_message_event
69 async def goals(room, message):
70 match = botlib.MessageMatch(room, message, bot, PREFIX)
71
72 if not match.is_not_from_this_bot() and match.prefix():
73 return
74
75 if match.prefix():
76 result = await do_goal(match)
77 await bot.api.send_text_message(
78 room.room_id, result)
79
80
81 bot.run()
82