]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
310319fe347834e0ba7355d469548cbd76fcfe1e
[matrix-goal-bot.git] / matrix_goal_bot / bot.py
1 '''
2 Prefix: .goal
3
4 Synax:
5 .goal COMMAND ARGS
6
7 Commands:
8 help - show this help
9 extra.help - show extended help
10 add - add a goal
11 clear - clear completed tasks under current goal
12 task ID - add a task under the goal specified by ID
13 close ID - mark goal done by ID
14 done ID - mark task done by ID
15 list - list incomplete goals
16 print GOAL_ID - print a goal and all tasks
17 '''
18
19 ex_help = '''
20 More commands:
21 add.vision - add a new vision (collection of goals)
22 close.vision ID - mark vision as complete by ID
23 dump - dump the full data set - BEWARE - so many messages
24 later GOAL_ID - put off goal for 14 days
25 list.later - list all 'later' goals
26 print.vision ID - print a vision and all goals
27 set.vision GOAL_ID VISION_ID- associate GOAL_ID with VISION_ID
28 visions - list incomplete visions
29 list.done - list completed goals
30 remove ID - remove a task by ID
31 start GOAL_ID - un-put-off a goal (reverse of later)
32 visionless - list goals with no set vision
33 '''
34
35
36 from datetime import datetime as dt
37 import logging
38 import os
39
40 import simplematrixbotlib as botlib
41
42 from .db import Data, pretty_print
43
44 DATE_FORMAT = '%Y-%m-%d'
45
46 def today():
47 return dt.now().strftime(DATE_FORMAT)
48
49 logger = logging.getLogger(__name__)
50
51 snark = [
52 'Ye cannot get ye flask.',
53 'Ye flask must be load bearing, or something...',
54 '''You have:
55 no tea
56 ''',
57 'How are you gentlemen?',
58 'All your base are belong to us.',
59 'You have been eaten by a groo.'
60 ]
61
62 class GoalBot():
63 def __init__(self, matrix_server,
64 bot_name,
65 bot_pwd,
66 db_file=None,
67 bot_prefix='.goal'):
68
69 creds = botlib.Creds(matrix_server, bot_name, bot_pwd)
70 self.bot = botlib.Bot(creds)
71 self.prefix = bot_prefix
72 self.data = Data(db_file)
73
74 def run(self):
75 logger.debug('Running goal bot...')
76 self.bot.run()
77
78 async def handle_message(self, room, message):
79 match = botlib.MessageMatch(room, message, self.bot, self.prefix)
80
81 if not match.is_not_from_this_bot():
82 # Let's not talk to our-self
83 return
84
85 logger.debug(f'Message: {message}')
86 if not match.prefix():
87 logger.debug(f'{message} - did not match {self.prefix}')
88
89 if match.prefix():
90 try:
91 result_lines = await self._handle_match(match)
92 for line in result_lines:
93 await self.bot.api.send_text_message(
94 room.room_id, str(line))
95 except Exception as ex:
96 logger.warn(f'Caught exception: {str(ex)}')
97 dumb_msg_idx = len(str(ex)) % len(snark)
98 dumb_msg = snark[dumb_msg_idx]
99 await self.bot.api.send_text_message(
100 room.room_id, dumb_msg)
101
102 await self.bot.api.send_text_message(
103 room.room_id, str(ex))
104
105
106 async def _handle_match(self, match):
107 logger.debug(f'Try to match {str(match)}')
108
109 if match.command('setup'):
110 result = self.data.setup()
111 return [result]
112
113 if match.command('add'):
114 new = ' '.join(match.args()[1:])
115 goal_id = self.data.add_goal(new)
116 results = [f'Goal added with id {goal_id}']
117
118 goals = self.data.get_all_goals()
119 results.append('# Goals list...')
120 for goal in goals:
121 results.append(str(goal))
122
123 return results
124
125 if match.command('list'):
126 logger.debug('Asked to list current goals...')
127 goals = self.data.get_current_goals()
128 results = ['# Goals list...']
129 for goal in goals:
130 results.append(str(goal))
131 return results
132
133
134 if match.command('list.done'):
135 goals = self.data.get_done_goals()
136 results = ['# Completed Goals...']
137 for goal in goals:
138 results.append(str(goal))
139 return results
140
141 if match.command('task'):
142 new = ' '.join(match.args()[1:])
143 goal_id = self.data.quick_add_task(new)
144 goal,tasks = self.data.get_goal(goal_id)
145 results = pretty_print(goal, tasks)
146 return results
147
148 if match.command('add.vision'):
149 new = ' '.join(match.args()[1:])
150 result = self.data.add_vision(new)
151 return [f'Vision added with id {result}']
152
153 if match.command('print'):
154 if len(match.args()) > 1:
155 goal_id = int(match.args()[1])
156 else:
157 goal_id = self.data._get_pref_goal_id()
158
159 logger.debug(f'Searching for goal {goal_id}...')
160 goal,tasks = self.data.get_goal(goal_id)
161 results = pretty_print(goal, tasks)
162
163 return results
164
165
166 if match.command('print.vision'):
167 get_id = int(match.args()[1])
168 vision,goals = self.data.get_vision(get_id)
169 results = pretty_print(vision, goals)
170 return results
171
172 if match.command('close'):
173 goal_id = int(match.args()[1])
174 vision_id = self.data.complete_goal(goal_id)
175 vision,goals = self.data.get_vision(vision_id)
176 results = pretty_print(vision, goals)
177 return results
178
179
180 if match.command('clear'):
181 if len(match.args()) > 1:
182 goal_id = int(match.args()[1])
183 else:
184 goal_id = self.data._get_pref_goal_id()
185 _ = self.data.clear(goal_id)
186
187 goal,tasks = self.data.get_goal(goal_id)
188 results = pretty_print(goal, tasks)
189 return results
190
191
192 if match.command('start'):
193 goal_id = int(match.args()[1])
194 vision,goals = self.data.start_goal(goal_id)
195 results = pretty_print(vision, goals)
196 return results
197
198 if match.command('later'):
199 goal_id = int(match.args()[1])
200 vision_id = self.data.put_off_goal(goal_id)
201 vision,goals = self.data.get_vision(vision_id)
202 results = pretty_print(vision, goals)
203 return results
204
205
206 if match.command('close.vision'):
207 vision_id = int(match.args()[1])
208 self.data.complete_vision(vision_id)
209 vision,goals = self.data.get_vision(vision_id)
210 results = pretty_print(vision, goals)
211 return results
212
213 if match.command('done'):
214 ids = match.args()[1:]
215 for task_id in ids:
216 goal_id = self.data.complete_task(int(task_id))
217 # Print the goal of the last closed task
218 goal,tasks = self.data.get_goal(goal_id)
219 results = pretty_print(goal, tasks)
220 return results
221
222 if match.command('dump'):
223 goals = self.data.get_all_goals()
224 results = ['# Everything ... ']
225 for goal in goals:
226 _,tasks = self.data.get_goal(goal.id)
227 details = pretty_print(goal, tasks)
228 results += details
229 return results
230
231
232 if match.command('list.later'):
233 goals = self.data.get_all_goals()
234 results = ['# All Goals ... ']
235 return results
236
237 if match.command('visions'):
238 items = self.data.get_all_visions()
239 results = ['# Visions']
240 if not items:
241 results.append('-- No current visions --')
242
243 for item in items:
244 results.append(item)
245 return results
246
247 if match.command('visionless'):
248 items = self.data.visionless()
249 results = ['# Goals with no vision']
250 if not items:
251 results.append('-- No visionless goals --')
252
253 for item in items:
254 results.append(item)
255 return results
256
257 if match.command('set.vision'):
258 goal_id = int(match.args()[1])
259 vision_id = int(match.args()[2])
260 self.data.assign_vision(goal_id=goal_id, vision_id=vision_id)
261 vision,goals = self.data.get_vision(vision_id)
262 results = pretty_print(vision, goals)
263 return results
264
265
266 if match.command('rename'):
267 goal_id = int(match.args()[1])
268 name = ' '.join(match.args()[2:])
269 self.data.rename_goal(goal_id=goal_id, name=name)
270 goal,tasks = self.data.get_goal(goal_id)
271 results = pretty_print(goal, tasks)
272 return results
273
274 if match.command('remove'):
275 task_id = int(match.args()[1])
276 result = self.data.remove_task(task_id)
277 return [result]
278
279 if match.command('extra.help'):
280 return [ex_help]
281
282 # Help text
283 return [__doc__]
284
285
286