]>
apps.delaporte.us Git - matrix-goal-bot.git/blob - bot.py
1 from datetime
import datetime
as dt
5 import simplematrixbotlib
as botlib
7 from goals
.db
import Data
, pretty_print
10 SERVER
= os
.environ
.get('MATRIX_SERVER')
11 BOT_NAME
= os
.environ
.get('GOAL_BOT')
12 BOT_PWD
= os
.environ
.get('GOAL_BOT_PWD')
14 DATE_FORMAT
= '%Y-%m-%d'
17 return dt
.now().strftime(DATE_FORMAT
)
19 creds
= botlib
.Creds(SERVER
, BOT_NAME
, BOT_PWD
)
20 bot
= botlib
.Bot(creds
)
23 logger
= logging
.getLogger(__name__
)
24 logging
.basicConfig(filename
='goals.log', encoding
='utf-8', level
=logging
.DEBUG
)
25 logging
.getLogger('nio').setLevel(logging
.WARNING
)
27 async def do_goal(match
):
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
50 if match
.command('setup'):
51 result
= Data().setup()
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}']
59 if match
.command('list'):
60 goals
= Data().get_all_goals()
61 results
= ['# Goals list...']
63 results
.append(str(goal
))
67 if match
.command('list-done'):
68 goals
= Data().get_done_goals()
69 results
= ['# Completed Goals...']
71 results
.append(str(goal
))
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
)
82 # return [f'Added task with id {result}']
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}']
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
)
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
)
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
)
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
)
118 if match
.command('list-all'):
119 goals
= Data().get_all_goals()
120 results
= ['# Everything is...']
122 _
,tasks
= Data().get_goal(goal
.id)
123 details
= pretty_print(goal
, tasks
)
127 if match
.command('visions'):
128 items
= Data().get_all_visions()
129 results
= ['# Visions']
131 results
.append('-- No current visions --')
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
)
146 return [do_goal
.__doc
__]
148 @bot.listener.on_message_event
149 async def goals(room
, message
):
150 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
152 if not match
.is_not_from_this_bot() and 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.']