]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - matrix_goal_bot/bot.py
4046eeb39221f92bd56535eaf7e0e7be8ab96d2d
[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 return
79
80 if match.prefix():
81 try:
82 result_lines = await self._handle_match(match)
83 for line in result_lines:
84 await self.bot.api.send_text_message(
85 room.room_id, str(line))
86 except Exception as ex:
87 await self.bot.api.send_text_message(
88 room.room_id, str(ex))
89
90
91 async def _handle_match(self, match):
92
93 if match.command('setup'):
94 result = self.data.setup()
95 return [result]
96
97 if match.command('add'):
98 new = ' '.join(match.args()[1:])
99 result = self.data.add_goal(new)
100 return [f'Goal added with id {result}']
101
102 if match.command('list'):
103 goals = self.data.get_all_goals()
104 results = ['# Goals list...']
105 for goal in goals:
106 results.append(str(goal))
107 return results
108
109
110 if match.command('list.done'):
111 goals = self.data.get_done_goals()
112 results = ['# Completed Goals...']
113 for goal in goals:
114 results.append(str(goal))
115 return results
116
117 if match.command('task'):
118 new = ' '.join(match.args()[1:])
119 goal_id = self.data.quick_add_task(new)
120 goal,tasks = self.data.get_goal(goal_id)
121 results = pretty_print(goal, tasks)
122 return results
123
124 if match.command('add.vision'):
125 new = ' '.join(match.args()[1:])
126 result = self.data.add_vision(new)
127 return [f'Vision added with id {result}']
128
129 if match.command('print'):
130 if len(match.args()) > 1:
131 goal_id = int(match.args()[1])
132 else:
133 goal_id = self.data._get_pref_goal_id()
134
135 logger.debug(f'Searching for goal {goal_id}...')
136 goal,tasks = self.data.get_goal(goal_id)
137 results = pretty_print(goal, tasks)
138
139 return results
140
141
142 if match.command('print.vision'):
143 get_id = int(match.args()[1])
144 vision,goals = self.data.get_vision(get_id)
145 results = pretty_print(vision, goals)
146 return results
147
148 if match.command('close.goal'):
149 goal_id = int(match.args()[1])
150 vision_id = self.data.complete_goal(goal_id)
151 vision,goals = self.data.get_vision(vision_id)
152 results = pretty_print(vision, goals)
153 return results
154
155 if match.command('close.vision'):
156 vision_id = int(match.args()[1])
157 self.data.complete_vision(vision_id)
158 vision,goals = self.data.get_vision(vision_id)
159 results = pretty_print(vision, goals)
160 return results
161
162 if match.command('done'):
163 ids = match.args()[1:]
164 for task_id in ids:
165 goal_id = self.data.complete_task(int(task_id))
166 # Print the goal of the last closed task
167 goal,tasks = self.data.get_goal(goal_id)
168 results = pretty_print(goal, tasks)
169 return results
170
171 if match.command('list.all'):
172 goals = self.data.get_all_goals()
173 results = ['# Everything is...']
174 for goal in goals:
175 _,tasks = self.data.get_goal(goal.id)
176 details = pretty_print(goal, tasks)
177 results += details
178 return results
179
180 if match.command('visions'):
181 items = self.data.get_all_visions()
182 results = ['# Visions']
183 if not items:
184 results.append('-- No current visions --')
185
186 for item in items:
187 results.append(item)
188 return results
189
190 if match.command('visionless'):
191 items = self.data.visionless()
192 results = ['# Goals with no vision']
193 if not items:
194 results.append('-- No visionless goals --')
195
196 for item in items:
197 results.append(item)
198 return results
199
200 if match.command('set.vision'):
201 goal_id = int(match.args()[1])
202 vision_id = int(match.args()[2])
203 self.data.assign_vision(goal_id=goal_id, vision_id=vision_id)
204 vision,goals = self.data.get_vision(vision_id)
205 results = pretty_print(vision, goals)
206 return results
207
208
209 if match.command('rename'):
210 goal_id = int(match.args()[1])
211 name = ' '.join(match.args()[2:])
212 self.data.rename_goal(goal_id=goal_id, name=name)
213 goal,tasks = self.data.get_goal(goal_id)
214 results = pretty_print(goal, tasks)
215 return results
216
217 if match.command('remove'):
218 task_id = int(match.args()[1])
219 result = self.data.remove_task(task_id)
220 return [result]
221
222 if match.command('extra.help'):
223 return [ex_help]
224
225 # Help text
226 return [__doc__]
227
228
229