Repository navigation
feat: add app_mention event handler #14
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0f19ab1
feat: add app_mention event handler
srtaalej 8752dfc
refactor: remove commented event handlers
srtaalej 7db511f
refactor: lint
srtaalej 97b43a0
Merge branch 'ai-apps' into feature/app-mention
mwbrooks b41054a
fix(flake8): remove unused listeners_constants.py
mwbrooks 9b9b051
feat: update app_mentioned_callback to not require conversations history
mwbrooks 38c8883
docs: update the app_mentioned_callback comment block
mwbrooks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,9 @@ | ||
| from listeners import actions | ||
| from listeners import assistant | ||
| from listeners import events | ||
|
|
||
|
|
||
| def register_listeners(app): | ||
|
|
||
| actions.register(app) | ||
| assistant.register(app) | ||
|
|
||
| # The following event listeners demonstrate how to implement the same on your own. | ||
| # from listeners import events | ||
| # events.register(app) | ||
| events.register(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,6 @@ | ||
| # This sample app repository contains event listener code to help developers understand what's happening under the hood. | ||
| # We recommend using assistant middleware instead of these event listeners. | ||
| # For more details, refer to https://tools.slack.dev/bolt-python/concepts/assistant/. | ||
|
|
||
| from typing import Dict, Any | ||
|
|
||
| from slack_bolt import App | ||
| from slack_bolt.request.payload_utils import is_event | ||
|
|
||
| from .assistant_thread_started import start_thread_with_suggested_prompts | ||
| from .asssistant_thread_context_changed import save_new_thread_context | ||
| from .user_message import respond_to_user_message | ||
| from .app_mentioned import app_mentioned_callback | ||
|
|
||
|
|
||
| def register(app: App): | ||
| app.event("assistant_thread_started")(start_thread_with_suggested_prompts) | ||
| app.event("assistant_thread_context_changed")(save_new_thread_context) | ||
| app.event("message", matchers=[is_user_message_event_in_assistant_thread])(respond_to_user_message) | ||
| app.event("message", matchers=[is_message_event_in_assistant_thread])(just_ack) | ||
|
|
||
|
|
||
| def is_message_event_in_assistant_thread(body: Dict[str, Any]) -> bool: | ||
| if is_event(body): | ||
| return body["event"]["type"] == "message" and body["event"].get("channel_type") == "im" | ||
| return False | ||
|
|
||
|
|
||
| def is_user_message_event_in_assistant_thread(body: Dict[str, Any]) -> bool: | ||
| return is_message_event_in_assistant_thread(body) and body["event"].get("subtype") in (None, "file_share") | ||
|
|
||
|
|
||
| def just_ack(): | ||
| pass | ||
| app.event("app_mention")(app_mentioned_callback) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| from logging import Logger | ||
| from slack_sdk import WebClient | ||
| from slack_bolt import Say | ||
|
|
||
| from ..llm_caller import call_llm | ||
| from ..views.feedback_block import create_feedback_block | ||
|
|
||
| """ | ||
| Handles the event when the app is mentioned in a Slack conversation | ||
| and generates an AI response. | ||
| """ | ||
|
|
||
|
|
||
| def app_mentioned_callback(client: WebClient, event: dict, logger: Logger, say: Say): | ||
| try: | ||
| channel_id = event.get("channel") | ||
| team_id = event.get("team") | ||
| text = event.get("text") | ||
| thread_ts = event.get("thread_ts") or event.get("ts") | ||
| user_id = event.get("user") | ||
|
|
||
| client.assistant_threads_setStatus( | ||
| channel_id=channel_id, | ||
| thread_ts=thread_ts, | ||
| status="thinking...", | ||
| loading_messages=[ | ||
| "Teaching the hamsters to type faster…", | ||
| "Untangling the internet cables…", | ||
| "Consulting the office goldfish…", | ||
| "Polishing up the response just for you…", | ||
| "Convincing the AI to stop overthinking…", | ||
| ], | ||
| ) | ||
|
|
||
| returned_message = call_llm([{"role": "user", "content": text}]) | ||
|
|
||
| stream_response = client.chat_startStream( | ||
| channel=channel_id, recipient_team_id=team_id, recipient_user_id=user_id, thread_ts=thread_ts | ||
| ) | ||
|
|
||
| stream_ts = stream_response["ts"] | ||
|
|
||
| # Loop over OpenAI response stream | ||
| # https://platform.openai.com/docs/api-reference/responses/create | ||
| for event in returned_message: | ||
| if event.type == "response.output_text.delta": | ||
| client.chat_appendStream(channel=channel_id, ts=stream_ts, markdown_text=f"{event.delta}") | ||
| else: | ||
| continue | ||
|
|
||
| feedback_block = create_feedback_block() | ||
| client.chat_stopStream(channel=channel_id, ts=stream_ts, blocks=feedback_block) | ||
|
|
||
| except Exception as e: | ||
| logger.exception(f"Failed to handle a user message event: {e}") | ||
| say(f":warning: Something went wrong! ({e})") |
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⭐ praise: Thanks so much for removing this! I'm a fan of the unlock this brings for adjacent events. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🪓 suggestion(non-blocking): I think later we should remove this in favor of a search method to showcase other AI focused features?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Super +1