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