]> apps.delaporte.us Git - matrix-goal-bot.git/blob - bot.py
Add ability to list complete goals
[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 list-done - list completed goals
41 print ID - print a goal and all tasks
42 print-vision ID - print a vision and all goals
43 done ID - mark task done by ID
44 goal-done ID - mark goal done by ID
45 set-vision GOAL_ID VISION_ID - associate GOAL_ID with VISION_ID
46 visions - list incomplete visions
47
48 '''
49
50 try:
51
52 if match.command('setup'):
53 result = Data().setup()
54 return [result]
55
56 if match.command('add'):
57 new = ' '.join(match.args()[1:])
58 result = Data().add_goal(new)
59 return [f'Goal added with id {result}']
60
61 if match.command('list'):
62 goals = Data().get_all_goals()
63 results = ['# Goals list...']
64 for goal in goals:
65 results.append(str(goal))
66 return results
67
68
69 if match.command('list-done'):
70 goals = Data().get_done_goals()
71 results = ['# Completed Goals...']
72 for goal in goals:
73 results.append(str(goal))
74 return results
75
76
77 if match.command('add-task'):
78 goal_id = int(match.args()[1])
79 new = ' '.join(match.args()[2:])
80 result = Data().add_task(goal_id=goal_id, name=new)
81 return [f'Added task with id {result}']
82
83 if match.command('add-vision'):
84 new = ' '.join(match.args()[1:])
85 result = Data().add_vision(new)
86 return [f'Vision added with id {result}']
87
88 if match.command('print'):
89 goal_id = int(match.args()[1])
90 logger.debug(f'Searching for goal {goal_id}...')
91 goal,tasks = Data().get_goal(goal_id)
92 results = pretty_print(goal, tasks)
93 return results
94
95
96 if match.command('print-vision'):
97 get_id = int(match.args()[1])
98 vision,goals = Data().get_vision(get_id)
99 results = pretty_print(vision, goals)
100 return results
101
102 if match.command('goal-done'):
103 goal_id = int(match.args()[1])
104 logger.debug(f'Searching for goal {goal_id}...')
105 Data().complete_goal(goal_id)
106 goal,tasks = Data().get_goal(goal_id)
107 results = pretty_print(goal, tasks)
108 return results
109
110
111 if match.command('done'):
112 task_id = int(match.args()[1])
113 goal_id = Data().complete_task(task_id)
114 goal,tasks = Data().get_goal(goal_id)
115 results = pretty_print(goal, tasks)
116 return results
117
118 if match.command('list-all'):
119 goals = Data().get_all_goals()
120 results = ['Everything is...']
121 for goal in goals:
122 _,tasks = Data().get_goal(goal.id)
123 details = pretty_print(goal, tasks)
124 results += details
125 return results
126
127 if match.command('visions'):
128 items = Data().get_all_visions()
129 results = ['Visions are']
130 if not items:
131 results.append('-- No current visions --')
132
133 for item in items:
134 results.append(item)
135 return results
136
137
138 if match.command('set-vision'):
139 goal_id = int(match.args()[1])
140 vision_id = int(match.args()[1])
141 Data().assign_vision(goal_id, vision_id)
142 vision,goals = Data().get_vision(vision_id)
143 results = pretty_print(vision, goals)
144 return results
145
146 return [do_goal.__doc__]
147 except Exception as ex:
148 return ['Ye cannot get ye flask.']
149
150 @bot.listener.on_message_event
151 async def goals(room, message):
152 match = botlib.MessageMatch(room, message, bot, PREFIX)
153
154 if not match.is_not_from_this_bot() and match.prefix():
155 return
156
157 if match.prefix():
158 result_lines = await do_goal(match)
159 for line in result_lines:
160 await bot.api.send_text_message(
161 room.room_id, str(line))
162
163
164 bot.run()
165