Skip to content

Commit

Permalink
feat: Send ephemeral confirmation on /subscribe and /unsubscribe.
Browse files Browse the repository at this point in the history
Signed-off-by: BURG3R5 <[email protected]>
  • Loading branch information
BURG3R5 committed Apr 17, 2022
1 parent 494fb92 commit bfcb096
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions slack_bot/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,29 @@ def run(self, raw_json: MultiDict) -> dict[str, Any] | None:
current_channel: str = "#" + json["channel_name"]
command: str = json["command"]
args: list[str] = str(json["text"]).split()
result: dict[str, Any] | None = None
if command == "/subscribe" and len(args) > 0:
self.run_subscribe_command(current_channel=current_channel, args=args)
result = self.run_subscribe_command(
current_channel=current_channel,
args=args,
)
elif command == "/unsubscribe" and len(args) > 0:
self.run_unsubscribe_command(current_channel=current_channel, args=args)
result = self.run_unsubscribe_command(
current_channel=current_channel,
args=args,
)
elif command == "/list":
return self.run_list_command(current_channel=current_channel)
result = self.run_list_command(current_channel=current_channel)
elif command == "/help":
return self.run_help_command()
result = self.run_help_command()
Storage.export_subscriptions(self.subscriptions)
return None
return result

def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
def run_subscribe_command(
self,
current_channel: str,
args: list[str],
) -> dict[str, Any]:
"""
Triggered by "/subscribe". Adds the passed events to the channel's subscriptions.
:param current_channel: Name of the current channel.
Expand Down Expand Up @@ -93,8 +104,13 @@ def run_subscribe_command(self, current_channel: str, args: list[str]) -> None:
events=new_events,
)
}
return self.run_list_command(current_channel=current_channel, ephemeral=True)

def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None:
def run_unsubscribe_command(
self,
current_channel: str,
args: list[str],
) -> dict[str, Any]:
"""
Triggered by "/unsubscribe". Removes the passed events from the channel's subscriptions.
:param current_channel: Name of the current channel.
Expand Down Expand Up @@ -130,11 +146,17 @@ def run_unsubscribe_command(self, current_channel: str, args: list[str]) -> None
events=events,
)
)
return self.run_list_command(current_channel=current_channel, ephemeral=True)

def run_list_command(self, current_channel: str) -> dict[str, Any]:
def run_list_command(
self,
current_channel: str,
ephemeral: bool = False,
) -> dict[str, Any]:
"""
Triggered by "/list". Sends a message listing the current channel's subscriptions.
:param current_channel: Name of the current channel.
:param ephemeral: Whether message should be ephemeral or not.
:return: Message containing subscriptions for the passed channel.
"""
blocks: list[dict] = []
Expand Down Expand Up @@ -165,7 +187,7 @@ def run_list_command(self, current_channel: str) -> dict[str, Any]:
},
]
return {
"response_type": "in_channel",
"response_type": "ephemeral" if ephemeral else "in_channel",
"blocks": blocks,
}

Expand Down

0 comments on commit bfcb096

Please sign in to comment.