]>
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 print-vision ID - print a vision and all goals
42 done ID - mark task done by ID
43 goal-done ID - mark goal done by ID
44 set-vision GOAL_ID VISION_ID - associate GOAL_ID with VISION_ID
45 visions - list incomplete visions
51 if match
.command('setup'):
52 result
= Data().setup()
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}']
60 if match
.command('list'):
61 goals
= Data().get_all_goals()
62 results
= ['Goals list...']
64 results
.append(str(goal
))
67 if match
.command('add-task'):
68 goal_id
= int(match
.args()[1])
69 new
= ' '.join(match
.args()[2:])
70 result
= Data().add_task(goal_id
=goal_id
, name
=new
)
71 return [f
'Added task with id {result}']
73 if match
.command('add-vision'):
74 new
= ' '.join(match
.args()[1:])
75 result
= Data().add_vision(new
)
76 return [f
'Vision added with id {result}']
78 if match
.command('print'):
79 goal_id
= int(match
.args()[1])
80 logger
.debug(f
'Searching for goal {goal_id}...')
81 goal
,tasks
= Data().get_goal(goal_id
)
82 results
= pretty_print(goal
, tasks
)
86 if match
.command('print-vision'):
87 get_id
= int(match
.args()[1])
88 vision
,goals
= Data().get_vision(get_id
)
89 results
= pretty_print(vision
, goals
)
92 if match
.command('goal-done'):
93 goal_id
= int(match
.args()[1])
94 logger
.debug(f
'Searching for goal {goal_id}...')
95 Data().complete_goal(goal_id
)
96 goal
,tasks
= Data().get_goal(goal_id
)
97 results
= pretty_print(goal
, tasks
)
101 if match
.command('done'):
102 task_id
= int(match
.args()[1])
103 goal_id
= Data().complete_task(task_id
)
104 goal
,tasks
= Data().get_goal(goal_id
)
105 results
= pretty_print(goal
, tasks
)
108 if match
.command('list-all'):
109 goals
= Data().get_all_goals()
110 results
= ['Everything is...']
112 _
,tasks
= Data().get_goal(goal
.id)
113 details
= pretty_print(goal
, tasks
)
117 if match
.command('visions'):
118 items
= Data().get_all_visions()
119 results
= ['Visions are']
121 results
.append('-- No current visions --')
128 if match
.command('set-vision'):
129 goal_id
= int(match
.args()[1])
130 vision_id
= int(match
.args()[1])
131 Data().assign_vision(goal_id
, vision_id
)
132 vision
,goals
= Data().get_vision(vision_id
)
133 results
= pretty_print(vision
, goals
)
136 return [do_goal
.__doc
__]
137 except Exception as ex
:
138 return ['Ye cannot get ye flask.']
140 @bot.listener.on_message_event
141 async def goals(room
, message
):
142 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
144 if not match
.is_not_from_this_bot() and match
.prefix():
148 result_lines
= await do_goal(match
)
149 for line
in result_lines
:
150 await bot
.api
.send_text_message(
151 room
.room_id
, str(line
))