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