Skip to content

Commit

Permalink
project
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarthak5598 committed Jul 3, 2024
1 parent fef6a9d commit 8bdf15e
Showing 1 changed file with 61 additions and 7 deletions.
68 changes: 61 additions & 7 deletions src/sammich/plugins/project.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
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):
Expand All @@ -15,18 +18,69 @@ def __init__(self, client: SlackClient, settings: CaseInsensitiveDict, storage:

@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."
)
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]

options = [
{"text": {"type": "plain_text", "text": project[:75]}, "value": project}
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 self.web_client.chat_postMessage(
channel=channel_id, blocks=blocks, text="Available Projects"
)

await command.say(message)
@action(action_id=re.compile(r"project_select_action_.*"), block_id=None)
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)

0 comments on commit 8bdf15e

Please sign in to comment.