Skip to content

Commit

Permalink
feat: /ask-bolty posts ephemeral message with prompt quote
Browse files Browse the repository at this point in the history
  • Loading branch information
srtaalej committed Aug 8, 2024
1 parent e018b0f commit 3357cdc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
9 changes: 4 additions & 5 deletions ai/providers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
from .anthropic import AnthropicAPI
from .openai import OpenAI_API
from ..ai_constants import DEFAULT_SYSTEM_CONTENT
from logging import Logger
from state_store.get_user_state import get_user_state
from typing import Optional, List

"""
New AI providers must be added below
New AI providers must be added below.
`get_available_providers()`
This function retrieves available API models from different AI providers.
It combines the available models into a single dictionary.
`_get_provider()`
`_get_provider()`
This function returns an instance of the appropriate API provider based on the given provider name.
`get_provider_response`()
This function retrieves the user's selected API provider and model,
sets the model, and generates a response.
Note that context is an optional parameter because some functionalities,
such as commands, do not allow access to conversation history if the bot
Note that context is an optional parameter because some functionalities,
such as commands, do not allow access to conversation history if the bot
isn't in the channel where the command is run.
"""
Expand Down
31 changes: 27 additions & 4 deletions listeners/commands/ask_command.py
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}")

0 comments on commit 3357cdc

Please sign in to comment.