Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the project slash command #14

Merged
merged 5 commits into from
Jun 26, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 62 additions & 7 deletions src/sammich/plugins/project.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,87 @@
import json
import re

from machine.clients.slack import SlackClient
from machine.plugins.base import MachineBasePlugin
from machine.plugins.decorators import command
from machine.plugins.decorators import action, command
from machine.storage import PluginStorage
from machine.utils.collections import CaseInsensitiveDict

PROJECTS_PER_PAGE = 100


class ProjectPlugin(MachineBasePlugin):
def __init__(self, client: SlackClient, settings: CaseInsensitiveDict, storage: PluginStorage):
super().__init__(client, settings, storage)

with open("data/projects.json") as f:
self.project_data = json.load(f)

@command("/project")
async def project(self, command):
text = command.text.strip()
project_name = text.strip().lower()
project_name = command.text.strip().lower()
channel_id = command._cmd_payload["channel_id"]

project = self.project_data.get(project_name)

if project:
project_list = "\n".join(project)
message = f"Hello, here the information about '{project_name}':\n{project_list}"
await command.say(message)
else:
message = (
f"Hello, the project '{project_name}' is not recognized. "
"Please try different query."
await self.show_project_page(channel_id)

async def show_project_page(self, channel_id):
projects = list(self.project_data.keys())

if not projects:
await self.web_client.chat_postMessage(
channel=channel_id, text="No projects available."
)
arkid15r marked this conversation as resolved.
Show resolved Hide resolved
return

# Calculate the number of dropdowns needed
num_dropdowns = (len(projects) + PROJECTS_PER_PAGE - 1) // PROJECTS_PER_PAGE

blocks = []
for i in range(num_dropdowns):
start_index = i * PROJECTS_PER_PAGE
end_index = start_index + PROJECTS_PER_PAGE
project_slice = projects[start_index:end_index]
arkid15r marked this conversation as resolved.
Show resolved Hide resolved

options = [
{"text": {"type": "plain_text", "text": project[:75]}, "value": project}
arkid15r marked this conversation as resolved.
Show resolved Hide resolved
for project in project_slice
]

blocks.append(
{
"type": "section",
"block_id": f"project_select_block_{i}",
"text": {
"type": "mrkdwn",
"text": f"Select a project (Page {i + 1}):",
},
"accessory": {
"type": "static_select",
"placeholder": {
"type": "plain_text",
"text": f"Select a project (Page {i + 1})",
},
"options": options,
"action_id": f"project_select_action_{i}",
},
}
)

await command.say(message)
await self.web_client.chat_postMessage(
channel=channel_id, blocks=blocks, text="Available Projects"
)
arkid15r marked this conversation as resolved.
Show resolved Hide resolved

@action(action_id=re.compile(r"project_select_action_.*"), block_id=None)
arkid15r marked this conversation as resolved.
Show resolved Hide resolved
async def handle_dropdown_selection(self, action):
selected_project = action.payload.actions[0].selected_option.value
project = self.project_data.get(selected_project)
project_list = "\n".join(project)
message = f"Hello, here is the information about '{selected_project}':\n{project_list}"
await action.say(message)