]>
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 list - list incomplete goals
39 list-all - list everything
40 print ID - print a goal and all tasks
41 done ID - mark task done by ID
42 goal-done ID - mark goal done by ID
43 set-vision GOAL_ID VISION_ID - associate GOAL_ID with VISION_ID
44 visions - list incomplete visions
48 if match
.command('setup'):
49 result
= Data().setup()
52 if match
.command('add'):
53 new
= ' '.join(match
.args()[1:])
54 result
= Data().add_goal(new
)
55 return [f
'Goal added with id {result}']
57 if match
.command('list'):
58 goals
= Data().get_all_goals()
59 results
= ['Goals list...']
61 results
.append(str(goal
))
64 if match
.command('add-task'):
65 goal_id
= int(match
.args()[1])
66 new
= ' '.join(match
.args()[2:])
67 result
= Data().add_task(goal_id
=goal_id
, name
=new
)
68 return [f
'Added task with id {result}']
70 if match
.command('add-vision'):
71 new
= ' '.join(match
.args()[1:])
72 result
= Data().add_vision(new
)
73 return [f
'Vision added with id {result}']
75 if match
.command('print'):
76 goal_id
= int(match
.args()[1])
77 logger
.debug(f
'Searching for goal {goal_id}...')
78 goal
,tasks
= Data().get_goal(goal_id
)
79 results
= pretty_print(goal
, tasks
)
83 if match
.command('print-vision'):
84 get_id
= int(match
.args()[1])
85 vision
,goals
= Data().get_vision(get_id
)
86 results
= pretty_print(vision
, goals
)
89 if match
.command('goal-done'):
90 goal_id
= int(match
.args()[1])
91 logger
.debug(f
'Searching for goal {goal_id}...')
92 Data().complete_goal(goal_id
)
93 goal
,tasks
= Data().get_goal(goal_id
)
94 results
= pretty_print(goal
, tasks
)
98 if match
.command('done'):
99 task_id
= int(match
.args()[1])
100 goal_id
= Data().complete_task(task_id
)
101 goal
,tasks
= Data().get_goal(goal_id
)
102 results
= pretty_print(goal
, tasks
)
105 if match
.command('list-all'):
106 goals
= Data().get_all_goals()
107 results
= ['Everything is...']
109 _
,tasks
= Data().get_goal(goal
.id)
110 details
= pretty_print(goal
, tasks
)
114 if match
.command('visions'):
115 items
= Data().get_all_visions()
116 results
= ['Visions are']
120 results
.append('-- No current visions --')
124 if match
.command('set-vision'):
125 goal_id
= int(match
.args()[1])
126 vision_id
= int(match
.args()[1])
127 result
= Data().assign_vision(goal_id
, vision_id
)
130 return [do_goal
.__doc
__]
133 @bot.listener.on_message_event
134 async def goals(room
, message
):
135 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
137 if not match
.is_not_from_this_bot() and match
.prefix():
141 result_lines
= await do_goal(match
)
142 for line
in result_lines
:
143 await bot
.api
.send_text_message(
144 room
.room_id
, str(line
))