]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - bot.py
Add help
[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, pretty_print
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 Prefix: !goal
30
31 Synax:
32 !goal COMMAND ARGS
33
34 Commands:
35 help - show this help
36 add - add a goal
37 add-task ID - add a task under the goal specified by ID
38 list - list incomplete goals
39 list-all - list everything
40 print ID - print a goal and all tasks
41 done ID - mark task done by ID
42 goal-done ID - mark goal done by ID
43
44 '''
45
46 if match.command('setup'):
47 result = Data().setup()
48 return [result]
49
50 if match.command('add'):
51 new = ' '.join(match.args()[1:])
52 result = Data().add_goal(new)
53 return [f'Goal added with id {result}']
54
55 if match.command('list'):
56 goals = Data().get_all_goals()
57 results = ['Goals list...']
58 for goal in goals:
59 results.append(str(goal))
60 return results
61
62 if match.command('add-task'):
63 goal_id = int(match.args()[1])
64 new = ' '.join(match.args()[2:])
65 result = Data().add_task(goal_id=goal_id, name=new)
66 return [f'Added task with id {result}']
67
68 if match.command('print'):
69 goal_id = int(match.args()[1])
70 logger.debug(f'Searching for goal {goal_id}...')
71 goal,tasks = Data().get_goal(goal_id)
72 results = pretty_print(goal, tasks)
73 return results
74
75 if match.command('goal-done'):
76 goal_id = int(match.args()[1])
77 logger.debug(f'Searching for goal {goal_id}...')
78 Data().complete_goal(goal_id)
79 goal,tasks = Data().get_goal(goal_id)
80 results = pretty_print(goal, tasks)
81 return results
82
83
84 if match.command('done'):
85 task_id = int(match.args()[1])
86 goal_id = Data().complete_task(task_id)
87 goal,tasks = Data().get_goal(goal_id)
88 results = pretty_print(goal, tasks)
89 return results
90
91 if match.command('list-all'):
92 goals = Data().get_all_goals()
93 results = ['Everything is...']
94 for goal in goals:
95 _,tasks = Data().get_goal(goal.id)
96 details = pretty_print(goal, tasks)
97 results += details
98 return results
99
100 return [do_goal.__doc__]
101
102
103 @bot.listener.on_message_event
104 async def goals(room, message):
105 match = botlib.MessageMatch(room, message, bot, PREFIX)
106
107 if not match.is_not_from_this_bot() and match.prefix():
108 return
109
110 if match.prefix():
111 result_lines = await do_goal(match)
112 for line in result_lines:
113 await bot.api.send_text_message(
114 room.room_id, str(line))
115
116
117 bot.run()
118