diff --git a/README.md b/README.md index 8cc86cd..fa6f6cb 100644 --- a/README.md +++ b/README.md @@ -85,11 +85,11 @@ Every incoming request is routed to a "listener". This directory groups each lis Configures the new Slack Assistant features, providing a dedicated side panel UI for users to interact with the AI chatbot. This module includes: -`assistant.py`, which contains two listeners: -* The `@assistant.thread_started` listener receives an event when users start new app thread. -* The `@assistant.user_message` listener processes user messages in app threads or from the app **Chat** and **History** tab. +- The `assistant_thread_started.py` file, which responds to new app threads with a list of suggested prompts. +- The `message.py` file, which responds to user messages sent to app threads or from the **Chat** and **History** tab with an LLM generated response. -`ai/llm_caller.py`, which handles OpenAI API integration and message formatting. It includes the `call_llm()` function that sends conversation threads to OpenAI's models. +### `ai/` +The `llm_caller.py` file, which handles OpenAI API integration and message formatting. It includes the `call_llm()` function that sends conversation threads to OpenAI's models. ## App Distribution / OAuth diff --git a/listeners/assistant/__init__.py b/listeners/assistant/__init__.py index f0235ac..5763994 100644 --- a/listeners/assistant/__init__.py +++ b/listeners/assistant/__init__.py @@ -1,7 +1,14 @@ -from slack_bolt import App +from slack_bolt import App, Assistant -from .assistant import assistant +from .assistant_thread_started import assistant_thread_started +from .message import message +# Refer to https://docs.slack.dev/tools/bolt-python/concepts/ai-apps#assistant for more details on the Assistant class def register(app: App): + assistant = Assistant() + + assistant.thread_started(assistant_thread_started) + assistant.user_message(message) + app.assistant(assistant) diff --git a/listeners/assistant/assistant_thread_started.py b/listeners/assistant/assistant_thread_started.py new file mode 100644 index 0000000..9d2990f --- /dev/null +++ b/listeners/assistant/assistant_thread_started.py @@ -0,0 +1,41 @@ +from logging import Logger +from typing import Dict, List + +from slack_bolt import Say, SetSuggestedPrompts + + +def assistant_thread_started( + say: Say, + set_suggested_prompts: SetSuggestedPrompts, + logger: Logger, +): + """ + Handle the assistant thread start event by greeting the user and setting suggested prompts. + + Args: + say: Function to send messages to the thread from the app + set_suggested_prompts: Function to configure suggested prompt options + logger: Logger instance for error tracking + """ + try: + say("How can I help you?") + + prompts: List[Dict[str, str]] = [ + { + "title": "What does Slack stand for?", + "message": "Slack, a business communication service, was named after an acronym. Can you guess what it stands for?", + }, + { + "title": "Write a draft announcement", + "message": "Can you write a draft announcement about a new feature my team just released? It must include how impactful it is.", + }, + { + "title": "Suggest names for my Slack app", + "message": "Can you suggest a few names for my Slack app? The app helps my teammates better organize information and plan priorities and action items.", + }, + ] + + set_suggested_prompts(prompts=prompts) + except Exception as e: + logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e) + say(f":warning: Something went wrong! ({e})") diff --git a/listeners/assistant/assistant.py b/listeners/assistant/message.py similarity index 59% rename from listeners/assistant/assistant.py rename to listeners/assistant/message.py index e0df965..b61de90 100644 --- a/listeners/assistant/assistant.py +++ b/listeners/assistant/message.py @@ -1,58 +1,15 @@ from logging import Logger from typing import Dict, List -from slack_bolt import Assistant, BoltContext, Say, SetStatus, SetSuggestedPrompts +from slack_bolt import BoltContext, Say, SetStatus from slack_sdk import WebClient from ai.llm_caller import call_llm from ..views.feedback_block import create_feedback_block -# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details -assistant = Assistant() - -@assistant.thread_started -def start_assistant_thread( - say: Say, - set_suggested_prompts: SetSuggestedPrompts, - logger: Logger, -): - """ - Handle the assistant thread start event by greeting the user and setting suggested prompts. - - Args: - say: Function to send messages to the thread from the app - set_suggested_prompts: Function to configure suggested prompt options - logger: Logger instance for error tracking - """ - try: - say("How can I help you?") - - prompts: List[Dict[str, str]] = [ - { - "title": "What does Slack stand for?", - "message": "Slack, a business communication service, was named after an acronym. Can you guess what it stands for?", - }, - { - "title": "Write a draft announcement", - "message": "Can you write a draft announcement about a new feature my team just released? It must include how impactful it is.", - }, - { - "title": "Suggest names for my Slack app", - "message": "Can you suggest a few names for my Slack app? The app helps my teammates better organize information and plan priorities and action items.", - }, - ] - - set_suggested_prompts(prompts=prompts) - except Exception as e: - logger.exception(f"Failed to handle an assistant_thread_started event: {e}", e) - say(f":warning: Something went wrong! ({e})") - - -# This listener is invoked when the human user sends a reply in the assistant thread -@assistant.user_message -def respond_in_assistant_thread( +def message( client: WebClient, context: BoltContext, logger: Logger, diff --git a/manifest.json b/manifest.json index 15aa21f..2123a0d 100644 --- a/manifest.json +++ b/manifest.json @@ -23,7 +23,6 @@ "app_mentions:read", "assistant:write", "im:history", - "channels:read", "chat:write" ] } @@ -31,7 +30,6 @@ "settings": { "event_subscriptions": { "bot_events": [ - "assistant_thread_context_changed", "assistant_thread_started", "message.im", "app_mention"