]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - bot.py
Fixup;
[matrix-goal-bot.git] / bot.py
1 from datetime import datetime as dt
2 import logging
3 import os
4
5 import simplematrixbotlib as botlib
6
7 from goals.db import Data, pretty_print
8
9
10 SERVER = os.environ.get('MATRIX_SERVER')
11 BOT_NAME = os.environ.get('GOAL_BOT')
12 BOT_PWD = os.environ.get('GOAL_BOT_PWD')
13
14 DATE_FORMAT = '%Y-%m-%d'
15
16 def today():
17 return dt.now().strftime(DATE_FORMAT)
18
19 creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
20 bot = botlib.Bot(creds)
21 PREFIX = '!goal'
22
23 logger = logging.getLogger(__name__)
24 logging.basicConfig(filename='goals.log', encoding='utf-8', level=logging.DEBUG)
25 logging.getLogger('nio').setLevel(logging.WARNING)
26
27 async def do_goal(match):
28 '''
29 Prefix: !goal
30
31 Synax:
32 !goal COMMAND ARGS
33
34 Commands:
35 help - show this help
36 add - add a goal
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
45
46 '''
47
48 if match.command('setup'):
49 result = Data().setup()
50 return [result]
51
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}']
56
57 if match.command('list'):
58 goals = Data().get_all_goals()
59 results = ['Goals list...']
60 for goal in goals:
61 results.append(str(goal))
62 return results
63
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}']
69
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}']
74
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)
80 return results
81
82
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)
87 return results
88
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)
95 return results
96
97
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)
103 return results
104
105 if match.command('list-all'):
106 goals = Data().get_all_goals()
107 results = ['Everything is...']
108 for goal in goals:
109 _,tasks = Data().get_goal(goal.id)
110 details = pretty_print(goal, tasks)
111 results += details
112 return results
113
114 if match.command('visions'):
115 items = Data().get_all_visions()
116 results = ['Visions are']
117 for item in items:
118 results.append(item)
119 else:
120 results.append('-- No current visions --')
121 return results
122
123
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)
128 return [result]
129
130 return [do_goal.__doc__]
131
132
133 @bot.listener.on_message_event
134 async def goals(room, message):
135 match = botlib.MessageMatch(room, message, bot, PREFIX)
136
137 if not match.is_not_from_this_bot() and match.prefix():
138 return
139
140 if 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))
145
146
147 bot.run()
148