Skip to content

Commit 8267b1b

Browse files
authored
fix: Fix several small bugs.
2 parents b3b57d3 + bfcb096 commit 8267b1b

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

main.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ def test_post() -> str:
4545
try:
4646
name: str = request.json["name"]
4747
except KeyError:
48-
name: str = "unknown"
48+
name: str = "(empty JSON)"
49+
except TypeError:
50+
name: str = "(invalid JSON)"
4951
return (
5052
f"This server is working, and to prove it to you, "
5153
f"I'll guess your name!\nYour name is... {name}!"

models/github/event_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def convert_str_to_event_type(event_keyword: str) -> EventType | None:
5252
:return: `EventType` member corresponding to the keyword.
5353
"""
5454
for event_type in EventType:
55-
if event_type.value == event_keyword:
55+
if event_type.keyword == event_keyword:
5656
return event_type
5757
print("Event not in enum")
5858
return None

slack_bot/runner.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,29 @@ def run(self, raw_json: MultiDict) -> dict[str, Any] | None:
3131
current_channel: str = "#" + json["channel_name"]
3232
command: str = json["command"]
3333
args: list[str] = str(json["text"]).split()
34+
result: dict[str, Any] | None = None
3435
if command == "/subscribe" and len(args) > 0:
35-
self.run_subscribe_command(current_channel=current_channel, args=args)
36+
result = self.run_subscribe_command(
37+
current_channel=current_channel,
38+
args=args,
39+
)
3640
elif command == "/unsubscribe" and len(args) > 0:
37-
self.run_unsubscribe_command(current_channel=current_channel, args=args)
41+
result = self.run_unsubscribe_command(
42+
current_channel=current_channel,
43+
args=args,
44+
)
3845
elif command == "/list":
39-
return self.run_list_command(current_channel=current_channel)
46+
result = self.run_list_command(current_channel=current_channel)
4047
elif command == "/help":
41-
return self.run_help_command()
48+
result = self.run_help_command()
4249
Storage.export_subscriptions(self.subscriptions)
43-
return None
50+
return result
4451

45-
def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
52+
def run_subscribe_command(
53+
self,
54+
current_channel: str,
55+
args: list[str],
56+
) -> dict[str, Any]:
4657
"""
4758
Triggered by "/subscribe". Adds the passed events to the channel's subscriptions.
4859
:param current_channel: Name of the current channel.
@@ -93,8 +104,13 @@ def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
93104
events=new_events,
94105
)
95106
}
107+
return self.run_list_command(current_channel=current_channel, ephemeral=True)
96108

97-
def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None:
109+
def run_unsubscribe_command(
110+
self,
111+
current_channel: str,
112+
args: list[str],
113+
) -> dict[str, Any]:
98114
"""
99115
Triggered by "/unsubscribe". Removes the passed events from the channel's subscriptions.
100116
:param current_channel: Name of the current channel.
@@ -130,11 +146,17 @@ def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None
130146
events=events,
131147
)
132148
)
149+
return self.run_list_command(current_channel=current_channel, ephemeral=True)
133150

134-
def run_list_command(self, current_channel: str) -> dict[str, Any]:
151+
def run_list_command(
152+
self,
153+
current_channel: str,
154+
ephemeral: bool = False,
155+
) -> dict[str, Any]:
135156
"""
136157
Triggered by "/list". Sends a message listing the current channel's subscriptions.
137158
:param current_channel: Name of the current channel.
159+
:param ephemeral: Whether message should be ephemeral or not.
138160
:return: Message containing subscriptions for the passed channel.
139161
"""
140162
blocks: list[dict] = []
@@ -149,7 +171,9 @@ def run_list_command(self, current_channel: str) -> dict[str, Any]:
149171
)
150172
if channel is None:
151173
continue
152-
events_string = ", ".join(event.keyword for event in channel.events)
174+
events_string = ", ".join(
175+
f"`{event.name.lower()}`" for event in channel.events
176+
)
153177
blocks += [
154178
{
155179
"type": "section",
@@ -163,7 +187,7 @@ def run_list_command(self, current_channel: str) -> dict[str, Any]:
163187
},
164188
]
165189
return {
166-
"response_type": "in_channel",
190+
"response_type": "ephemeral" if ephemeral else "in_channel",
167191
"blocks": blocks,
168192
}
169193

utils/storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def export_subscriptions(subscriptions: dict[str, set[Channel]]) -> None:
2323
with open(".data", mode="w", encoding="utf-8") as file:
2424
exportable_dict: dict[str, dict[str, list[str]]] = {
2525
repo: {
26-
channel.name: [event.value for event in channel.events]
26+
channel.name: [event.keyword for event in channel.events]
2727
for channel in channels
2828
}
2929
for repo, channels in subscriptions.items()

0 commit comments

Comments
 (0)