Skip to content

Commit 249e5dd

Browse files
srtaalejzimeg
andauthored
docs: improve readability with docstrings (#21)
Co-authored-by: Eden Zimbelman <eden.zimbelman@salesforce.com>
1 parent 20419a8 commit 249e5dd

11 files changed

Lines changed: 64 additions & 26 deletions

File tree

ai/llm_caller.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import os
2-
from typing import List, Dict
2+
from typing import Dict, List
33

44
import openai
55
from openai import Stream
66
from openai.types.responses import ResponseStreamEvent
77

8-
98
DEFAULT_SYSTEM_CONTENT = """
109
You're an assistant in a Slack workspace.
1110
Users in the workspace will ask you to help them write something or to think better about a specific topic.

app.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import os
21
import logging
2+
import os
33

44
from dotenv import load_dotenv
55

app_oauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import logging
22
import os
3+
34
from slack_bolt import App, BoltResponse
4-
from slack_bolt.oauth.callback_options import CallbackOptions, SuccessArgs, FailureArgs
5+
from slack_bolt.oauth.callback_options import CallbackOptions, FailureArgs, SuccessArgs
56
from slack_bolt.oauth.oauth_settings import OAuthSettings
6-
77
from slack_sdk.oauth.installation_store import FileInstallationStore
88
from slack_sdk.oauth.state_store import FileOAuthStateStore
99

listeners/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
from listeners import actions
2-
from listeners import assistant
3-
from listeners import events
1+
from slack_bolt import App
42

3+
from listeners import actions, assistant, events
54

6-
def register_listeners(app):
5+
6+
def register_listeners(app: App):
77
actions.register(app)
88
assistant.register(app)
99
events.register(app)

listeners/actions/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from slack_bolt import App
2+
23
from .actions import handle_feedback
34

45

listeners/actions/actions.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
1-
import logging
1+
from logging import Logger
22

3+
from slack_bolt import Ack
4+
from slack_sdk import WebClient
35

4-
# Handle feedback buttons (thumbs up/down)
5-
def handle_feedback(ack, body, client, logger: logging.Logger):
6+
7+
def handle_feedback(ack: Ack, body: dict, client: WebClient, logger: Logger):
8+
"""
9+
Handles user feedback on AI-generated responses via thumbs up/down buttons.
10+
11+
Args:
12+
ack: Function to acknowledge the action request
13+
body: Action payload containing feedback details (message, channel, user, action value)
14+
client: Slack WebClient for making API calls
15+
logger: Logger instance for debugging and error tracking
16+
"""
617
try:
718
ack()
819
message_ts = body["message"]["ts"]

listeners/assistant/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from slack_bolt import App
2+
23
from .assistant import assistant
34

45

listeners/assistant/assistant.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1-
import logging
1+
from logging import Logger
22
from typing import Dict, List
33

44
from slack_bolt import Assistant, BoltContext, Say, SetStatus, SetSuggestedPrompts
55
from slack_bolt.context.get_thread_context import GetThreadContext
66
from slack_sdk import WebClient
77
from slack_sdk.errors import SlackApiError
88

9-
from ..views.feedback_block import create_feedback_block
109
from ai.llm_caller import call_llm
1110

11+
from ..views.feedback_block import create_feedback_block
1212

1313
# Refer to https://tools.slack.dev/bolt-python/concepts/assistant/ for more details
1414
assistant = Assistant()
1515

1616

17-
# This listener is invoked when a human user opened an assistant thread
1817
@assistant.thread_started
1918
def start_assistant_thread(
2019
say: Say,
2120
get_thread_context: GetThreadContext,
2221
set_suggested_prompts: SetSuggestedPrompts,
23-
logger: logging.Logger,
22+
logger: Logger,
2423
):
24+
"""
25+
Handle the assistant thread start event by greeting the user and setting suggested prompts.
26+
27+
Args:
28+
say: Function to send messages to the thread from the app
29+
get_thread_context: Function to retrieve thread context information
30+
set_suggested_prompts: Function to configure suggested prompt options
31+
logger: Logger instance for error tracking
32+
"""
2533
try:
2634
say("How can I help you?")
2735

@@ -60,11 +68,23 @@ def respond_in_assistant_thread(
6068
client: WebClient,
6169
context: BoltContext,
6270
get_thread_context: GetThreadContext,
63-
logger: logging.Logger,
71+
logger: Logger,
6472
payload: dict,
6573
say: Say,
6674
set_status: SetStatus,
6775
):
76+
"""
77+
Handles when users send messages or select a prompt in an assistant thread and generate AI responses:
78+
79+
Args:
80+
client: Slack WebClient for making API calls
81+
context: Bolt context containing channel and thread information
82+
get_thread_context: Function to retrieve thread context (e.g., referred channel)
83+
logger: Logger instance for error tracking
84+
payload: Event payload with message details (channel, user, text, etc.)
85+
say: Function to send messages to the thread
86+
set_status: Function to update the assistant's status
87+
"""
6888
try:
6989
channel_id = payload["channel"]
7090
team_id = context.team_id
@@ -84,8 +104,6 @@ def respond_in_assistant_thread(
84104
)
85105

86106
if user_message == "Can you generate a brief summary of the referred channel?":
87-
# the logic here requires the additional bot scopes:
88-
# channels:join, channels:history, groups:history
89107
thread_context = get_thread_context()
90108
referred_channel_id = thread_context.get("channel_id")
91109
try:

listeners/events/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from slack_bolt import App
2+
23
from .app_mentioned import app_mentioned_callback
34

45

listeners/events/app_mentioned.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
from logging import Logger
2-
from slack_sdk import WebClient
2+
33
from slack_bolt import Say
4+
from slack_sdk import WebClient
45

56
from ai.llm_caller import call_llm
67
from ..views.feedback_block import create_feedback_block
78

8-
"""
9-
Handles the event when the app is mentioned in a Slack conversation
10-
and generates an AI response.
11-
"""
12-
139

1410
def app_mentioned_callback(client: WebClient, event: dict, logger: Logger, say: Say):
11+
"""
12+
Handles the event when the app is mentioned in a Slack conversation
13+
and generates an AI response.
14+
15+
Args:
16+
client: Slack WebClient for making API calls
17+
event: Event payload containing mention details (channel, user, text, etc.)
18+
logger: Logger instance for error tracking
19+
say: Function to send messages to the thread from the app
20+
"""
1521
try:
1622
channel_id = event.get("channel")
1723
team_id = event.get("team")

0 commit comments

Comments
 (0)