]>
us-delaporte.pagekite.me 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
46 if match
.command('setup'):
47 result
= Data().setup()
50 if match
.command('add'):
51 new
= ' '.join(match
.args()[1:])
52 result
= Data().add_goal(new
)
53 return [f
'Goal added with id {result}']
55 if match
.command('list'):
56 goals
= Data().get_all_goals()
57 results
= ['Goals list...']
59 results
.append(str(goal
))
62 if match
.command('add-task'):
63 goal_id
= int(match
.args()[1])
64 new
= ' '.join(match
.args()[2:])
65 result
= Data().add_task(goal_id
=goal_id
, name
=new
)
66 return [f
'Added task with id {result}']
68 if match
.command('print'):
69 goal_id
= int(match
.args()[1])
70 logger
.debug(f
'Searching for goal {goal_id}...')
71 goal
,tasks
= Data().get_goal(goal_id
)
72 results
= pretty_print(goal
, tasks
)
75 if match
.command('goal-done'):
76 goal_id
= int(match
.args()[1])
77 logger
.debug(f
'Searching for goal {goal_id}...')
78 Data().complete_goal(goal_id
)
79 goal
,tasks
= Data().get_goal(goal_id
)
80 results
= pretty_print(goal
, tasks
)
84 if match
.command('done'):
85 task_id
= int(match
.args()[1])
86 goal_id
= Data().complete_task(task_id
)
87 goal
,tasks
= Data().get_goal(goal_id
)
88 results
= pretty_print(goal
, tasks
)
91 if match
.command('list-all'):
92 goals
= Data().get_all_goals()
93 results
= ['Everything is...']
95 _
,tasks
= Data().get_goal(goal
.id)
96 details
= pretty_print(goal
, tasks
)
100 return [do_goal
.__doc
__]
103 @bot.listener.on_message_event
104 async def goals(room
, message
):
105 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
107 if not match
.is_not_from_this_bot() and match
.prefix():
111 result_lines
= await do_goal(match
)
112 for line
in result_lines
:
113 await bot
.api
.send_text_message(
114 room
.room_id
, str(line
))