]> us-delaporte.pagekite.me Git - matrix-goal-bot.git/blob - bot.py
839bf0719b3136d914a168fbc69d2011554753a5
[matrix-goal-bot.git] / bot.py
1 import os
2 from datetime import datetime as dt
3
4 import simplematrixbotlib as botlib
5
6 from school import (get_assignments, STUDENTS)
7
8 from chore.db import Data as ChoreData
9
10 CHORES = ChoreData()
11
12 SERVER = os.environ.get('MATRIX_SERVER')
13 BOT_NAME = os.environ.get('GOAL_BOT')
14 BOT_PWD = os.environ.get('GOAL_BOT_PWD')
15
16 DATE_FORMAT = '%Y-%m-%d'
17
18 def today():
19 return dt.now().strftime(DATE_FORMAT)
20
21 creds = botlib.Creds(SERVER, BOT_NAME, BOT_PWD)
22 bot = botlib.Bot(creds)
23 PREFIX = '!goal'
24
25 @bot.listener.on_message_event
26 async def goals(room, message):
27 match = botlib.MessageMatch(room, message, bot, PREFIX)
28
29 if not match.is_not_from_this_bot() and match.prefix():
30 return
31
32 if match.command("echo"):
33 await bot.api.send_text_message(
34 room.room_id, " ".join(arg for arg in match.args())
35 )
36
37 help_text = '''
38 # Print this help text
39 !school help
40 # List available students
41 !school list
42 # Get today's assignments
43 !school get [NAME]
44 # Get an assignment for a date
45 !school get [Name] 2026-03-09
46 '''
47 if match.command("help"):
48 await bot.api.send_text_message(
49 room.room_id, help_text)
50
51 if match.command("list"):
52 student_list = f"Known students are {STUDENTS}."
53 await bot.api.send_text_message(
54 room.room_id, student_list)
55
56 person = ''
57 if match.command("get"):
58 print(match.args())
59 if(len(match.args()) < 2):
60 result = ['Please specify person to get assignments for.']
61 else:
62 person = match.args()[1]
63
64 if(len(match.args()) > 2):
65 day = match.args()[2]
66 else:
67 day = today()
68
69 if(person and day):
70 await bot.api.send_text_message(room.room_id, f'Fetching assignments for {person} on {day}')
71 results = get_assignments(person, day)
72 else:
73 results = ['Oops.']
74
75 if len(results) < 3:
76 results = [f'No assignments found for {person} on {day}.']
77
78 for line in results:
79
80 await bot.api.send_text_message(
81 room.room_id, line)
82
83
84 @bot.listener.on_message_event
85 async def chores(room, message):
86 match = botlib.MessageMatch(room, message, bot, '!chore')
87
88 if not match.is_not_from_this_bot() and match.prefix():
89 return
90
91 if match.command('list'):
92
93 await bot.api.send_text_message(
94 room.room_id, 'Listing chores...')
95
96 result = CHORES.list()
97 for item in result:
98 await bot.api.send_text_message(
99 room.room_id, item)
100
101 await bot.api.send_text_message(
102 room.room_id, '-- End of List --')
103
104 if match.command('setup'):
105 await bot.api.send_text_message(
106 room.room_id, f'Doing chore setup...')
107 result = CHORES.setup()
108 result = await bot.api.send_text_message(
109 room.room_id, f'Result: {result}')
110
111
112 if match.command('add'):
113 new_chore = ' '.join(match.args()[1:])
114
115 await bot.api.send_text_message(
116 room.room_id, f'Adding chore...{new_chore}...')
117
118 result = CHORES.add(new_chore)
119 await bot.api.send_text_message(
120 room.room_id, result)
121
122
123
124 if match.command('check'):
125 chore_id = match.args()[1]
126
127 result = CHORES.check(chore_id)
128 await bot.api.send_text_message(
129 room.room_id, result)
130
131 if match.command('done'):
132 chore_id = match.args()[1]
133 result = CHORES.done(chore_id)
134 await bot.api.send_text_message(
135 room.room_id, result)
136
137 if match.command('undo'):
138 chore_id = match.args()[1]
139 result = CHORES.undo(chore_id)
140 await bot.api.send_text_message(
141 room.room_id, result)
142
143
144 if match.command('remove'):
145 chore_id = match.args()[1]
146 result = CHORES.remove(chore_id)
147 await bot.api.send_text_message(
148 room.room_id, result)
149
150
151 bot.run()
152