]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
Allow completion of multiple tasks
[matrix-goal-bot.git] / matrix_goal_bot / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from .db import Data, pretty_print
8
9 SERVER = os.environ.get('MATRIX_SERVER')
10 BOT_NAME = os.environ.get('GOAL_BOT')
11 BOT_PWD = os.environ.get('GOAL_BOT_PWD')
12
13 def get_bot():
14 creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
15 return botlib.Bot(creds)
16
17 DATE_FORMAT = '%Y-%m-%d'
18
19 def today():
20 return dt.now().strftime(DATE_FORMAT)
21
22 PREFIX = '!goal'
23
24 logger = logging.getLogger(__name__)
25 logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG)
26 logging.getLogger('nio').setLevel(logging.WARNING)
27
28 async def do_goal(match):
29 '''
30 Prefix: !goal
31
32 Synax:
33 !goal COMMAND ARGS
34
35 Commands:
36 help - show this help
37 add - add a goal
38 add-task ID - add a task under the goal specified by ID
39 add-vision - add a new vision (collection of goals)
40 close-goal ID - mark goal done by ID
41 close-vision ID - mark vision as complete by ID
42 done ID - mark task done by ID
43 list - list incomplete goals
44 list-all - list everything
45 list-done - list completed goals
46 print GOAL_ID - print a goal and all tasks
47 print-vision ID - print a vision and all goals
48 set-vision GOAL_ID VISION_ID- associate GOAL_ID with VISION_ID
49 visions - list incomplete visions
50 visionless - list goals with no set vision
51 '''
52
53 if match.command('setup'):
54 result = Data().setup()
55 return [result]
56
57 if match.command('add'):
58 new = ' '.join(match.args()[1:])
59 result = Data().add_goal(new)
60 return [f'Goal added with id {result}']
61
62 if match.command('list'):
63 goals = Data().get_all_goals()
64 results = ['# Goals list...']
65 for goal in goals:
66 results.append(str(goal))
67 return results
68
69
70 if match.command('list.done'):
71 goals = Data().get_done_goals()
72 results = ['# Completed Goals...']
73 for goal in goals:
74 results.append(str(goal))
75 return results
76
77
78 if match.command('add.task'):
79 goal_id = int(match.args()[1])
80 new = ' '.join(match.args()[2:])
81 result = Data().add_task(goal_id=goal_id, name=new)
82 goal,tasks = Data().get_goal(goal_id)
83 results = pretty_print(goal, tasks)
84 return results
85 # return [f'Added task with id {result}']
86
87 if match.command('add.vision'):
88 new = ' '.join(match.args()[1:])
89 result = Data().add_vision(new)
90 return [f'Vision added with id {result}']
91
92 if match.command('print'):
93 goal_id = int(match.args()[1])
94 logger.debug(f'Searching for goal {goal_id}...')
95 goal,tasks = Data().get_goal(goal_id)
96 results = pretty_print(goal, tasks)
97 return results
98
99
100 if match.command('print.vision'):
101 get_id = int(match.args()[1])
102 vision,goals = Data().get_vision(get_id)
103 results = pretty_print(vision, goals)
104 return results
105
106 if match.command('close.goal'):
107 goal_id = int(match.args()[1])
108 vision_id = Data().complete_goal(goal_id)
109 vision,goals = Data().get_vision(vision_id)
110 results = pretty_print(vision, goals)
111 return results
112
113 if match.command('close.vision'):
114 vision_id = int(match.args()[1])
115 Data().complete_vision(vision_id)
116 vision,goals = Data().get_vision(vision_id)
117 results = pretty_print(vision, goals)
118 return results
119
120 if match.command('done'):
121 ids = match.args()[1:]
122 for task_id in ids:
123 goal_id = Data().complete_task(int(task_id))
124 # Print the goal of the last closed task
125 goal,tasks = Data().get_goal(goal_id)
126 results = pretty_print(goal, tasks)
127 return results
128
129 if match.command('list.all'):
130 goals = Data().get_all_goals()
131 results = ['# Everything is...']
132 for goal in goals:
133 _,tasks = Data().get_goal(goal.id)
134 details = pretty_print(goal, tasks)
135 results += details
136 return results
137
138 if match.command('visions'):
139 items = Data().get_all_visions()
140 results = ['# Visions']
141 if not items:
142 results.append('-- No current visions --')
143
144 for item in items:
145 results.append(item)
146 return results
147
148 if match.command('visions'):
149 items = Data().visionless()
150 results = ['# Goals with no vision']
151 if not items:
152 results.append('-- No visionless goals --')
153
154 for item in items:
155 results.append(item)
156 return results
157
158 if match.command('set.vision'):
159 goal_id = int(match.args()[1])
160 vision_id = int(match.args()[2])
161 Data().assign_vision(goal_id=goal_id, vision_id=vision_id)
162 vision,goals = Data().get_vision(vision_id)
163 results = pretty_print(vision, goals)
164 return results
165
166
167 if match.command('rename'):
168 goal_id = int(match.args()[1])
169 name = ' '.join(match.args()[2:])
170 Data().rename_goal(goal_id=goal_id, name=name)
171 goal,tasks = Data().get_goal(goal_id)
172 results = pretty_print(goal, tasks)
173 return results
174
175 # Help text
176 return [do_goal.__doc__]
177
178 async def goal_bot(room, message, bot):
179 match = botlib.MessageMatch(room, message, bot, PREFIX)
180
181 if not match.is_not_from_this_bot() and match.prefix():
182 return
183
184 try:
185 if match.prefix():
186 result_lines = await do_goal(match)
187 for line in result_lines:
188 await bot.api.send_text_message(
189 room.room_id, str(line))
190 except Exception as ex:
191 line ='Ye cannot get ye flask.'
192 await bot.api.send_text_message(
193 room.room_id, str(line))
194 await bot.api.send_text_message(
195 room.room_id, str(ex))
196
197