]> apps.delaporte.us Git - matrix-goal-bot.git/blob - bot.py
Fixes for vision
[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 add-vision - add a new vision (collection of goals)
39 done ID - mark task done by ID
40 goal-done ID - mark goal done by ID
41 list - list incomplete goals
42 list-all - list everything
43 list-done - list completed goals
44 print ID - print a goal and all tasks
45 print-vision ID - print a vision and all goals
46 set-vision GOAL_ID VISION_ID- associate GOAL_ID with VISION_ID
47 visions - list incomplete visions
48 '''
49
50 if match.command('setup'):
51 result = Data().setup()
52 return [result]
53
54 if match.command('add'):
55 new = ' '.join(match.args()[1:])
56 result = Data().add_goal(new)
57 return [f'Goal added with id {result}']
58
59 if match.command('list'):
60 goals = Data().get_all_goals()
61 results = ['# Goals list...']
62 for goal in goals:
63 results.append(str(goal))
64 return results
65
66
67 if match.command('list-done'):
68 goals = Data().get_done_goals()
69 results = ['# Completed Goals...']
70 for goal in goals:
71 results.append(str(goal))
72 return results
73
74
75 if match.command('add-task'):
76 goal_id = int(match.args()[1])
77 new = ' '.join(match.args()[2:])
78 result = Data().add_task(goal_id=goal_id, name=new)
79 goal,tasks = Data().get_goal(goal_id)
80 results = pretty_print(goal, tasks)
81 return results
82 # return [f'Added task with id {result}']
83
84 if match.command('add-vision'):
85 new = ' '.join(match.args()[1:])
86 result = Data().add_vision(new)
87 return [f'Vision added with id {result}']
88
89 if match.command('print'):
90 goal_id = int(match.args()[1])
91 logger.debug(f'Searching for goal {goal_id}...')
92 goal,tasks = Data().get_goal(goal_id)
93 results = pretty_print(goal, tasks)
94 return results
95
96
97 if match.command('print-vision'):
98 get_id = int(match.args()[1])
99 vision,goals = Data().get_vision(get_id)
100 results = pretty_print(vision, goals)
101 return results
102
103 if match.command('goal-done'):
104 goal_id = int(match.args()[1])
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']
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()[2])
141 Data().assign_vision(goal_id=goal_id, vision_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
148 @bot.listener.on_message_event
149 async def goals(room, message):
150 match = botlib.MessageMatch(room, message, bot, PREFIX)
151
152 if not match.is_not_from_this_bot() and match.prefix():
153 return
154
155 try:
156 if match.prefix():
157 result_lines = await do_goal(match)
158 for line in result_lines:
159 await bot.api.send_text_message(
160 room.room_id, str(line))
161 except Exception as ex:
162 return ['Ye cannot get ye flask.']
163
164
165 bot.run()
166