]>
apps.delaporte.us Git - matrix-goal-bot.git/blob - bot.py
649cfe69e753333464ed4dd65cb6ee02ad218a7b
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
):
32 # logger.debug(f'match {str(match)}')
33 if match
.command('help'):
36 if match
.command('setup'):
37 result
= Data().setup()
40 if match
.command('add'):
41 new
= ' '.join(match
.args()[1:])
42 result
= Data().add_goal(new
)
43 return [f
'Goal added with id {result}']
45 if match
.command('list'):
46 goals
= Data().list_goals()
47 results
= ['Goals list...']
49 results
.append(str(goal
))
52 if match
.command('add-task'):
53 goal_id
= int(match
.args()[1])
54 new
= ' '.join(match
.args()[2:])
55 result
= Data().add_task(goal_id
=goal_id
, name
=new
)
56 return [f
'Added task with id {result}']
58 if match
.command('print'):
59 goal_id
= int(match
.args()[1])
60 logger
.debug(f
'Searching for goal {goal_id}...')
61 goal
,tasks
= Data().get_goal(goal_id
)
62 results
= pretty_print(goal
, tasks
)
65 if match
.command('goal-done'):
66 goal_id
= int(match
.args()[1])
67 logger
.debug(f
'Searching for goal {goal_id}...')
68 Data().complete_goal(goal_id
)
69 goal
,tasks
= Data().get_goal(goal_id
)
70 results
= pretty_print(goal
, tasks
)
74 if match
.command('done'):
75 task_id
= int(match
.args()[1])
76 goal_id
= Data().complete_task(task_id
)
77 goal
,tasks
= Data().get_goal(goal_id
)
78 results
= pretty_print(goal
, tasks
)
84 @bot.listener.on_message_event
85 async def goals(room
, message
):
86 match
= botlib
.MessageMatch(room
, message
, bot
, PREFIX
)
88 if not match
.is_not_from_this_bot() and match
.prefix():
92 result_lines
= await do_goal(match
)
93 for line
in result_lines
:
94 await bot
.api
.send_text_message(
95 room
.room_id
, str(line
))