generated from slack-samples/bolt-python-starter-template
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: /ask-bolty posts ephemeral message with prompt quote
- Loading branch information
Showing
2 changed files
with
31 additions
and
9 deletions.
There are no files selected for viewing
This file contains 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 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,22 +1,45 @@ | ||
from slack_bolt import Ack, Say, BoltContext | ||
from logging import Logger | ||
from ai.providers import get_provider_response | ||
from slack_sdk import WebClient | ||
|
||
""" | ||
Callback for handling the 'ask-bolty' command. It acknowledges the command, retrieves the user's ID and prompt, | ||
checks if the prompt is empty, and responds with either an error message or the provider's response. | ||
""" | ||
|
||
|
||
def ask_callback(ack: Ack, command, say: Say, logger: Logger, context: BoltContext): | ||
def ask_callback(client: WebClient, ack: Ack, command, say: Say, logger: Logger, context: BoltContext): | ||
try: | ||
ack() | ||
user_id = context["user_id"] | ||
channel_id = context["channel_id"] | ||
prompt = command["text"] | ||
|
||
if prompt == "": | ||
say(text="Please provide a question.") | ||
client.chat_postEphemeral( | ||
channel=channel_id, user=user_id, text="Looks like you didn't provide a prompt. Try again." | ||
) | ||
else: | ||
say(text=get_provider_response(user_id, prompt)) | ||
client.chat_postEphemeral( | ||
channel=channel_id, | ||
user=user_id, | ||
blocks=[ | ||
{ | ||
"type": "rich_text", | ||
"elements": [ | ||
{ | ||
"type": "rich_text_quote", | ||
"elements": [{"type": "text", "text": prompt}], | ||
}, | ||
{ | ||
"type": "rich_text_section", | ||
"elements": [{"type": "text", "text": get_provider_response(user_id, prompt)}], | ||
}, | ||
], | ||
} | ||
], | ||
) | ||
except Exception as e: | ||
logger.error(e) | ||
say(text=f"An unexpected error occured: {e}") | ||
client.chat_postEphemeral(channel=channel_id, user=user_id, text=f"An unexpected error occured: {e}") |