Skip to content

Commit 53e3db2

Browse files
chore: adds more comprehensive docstrings
1 parent df28ce2 commit 53e3db2

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

packages/slackBotFunction/app/handler.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""
2-
Main entry point for the Slack Bot Lambda Function
2+
Main Lambda handler - dual-purpose function for Slack bot operations
33
4-
This is the Lambda handler that coordinates all the components, it handles both regular Slack webhooks
5-
and async processing requests
4+
This Lambda function serves two purposes:
5+
1. Handles incoming Slack events (webhooks) via API Gateway
6+
2. Processes async operations when invoked by itself to avoid timeouts
67
"""
78

89
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
@@ -18,11 +19,12 @@
1819

1920
def handler(event: dict, context: LambdaContext) -> dict:
2021
"""
21-
Main Lambda handler - handles Slack webhooks and async processing
22+
Main Lambda entry point - routes between Slack webhook and async processing
2223
23-
Two modes:
24-
1. Slack webhook -> acknowledge quickly, trigger async processing
25-
2. Async processing -> handle the conversation (query Bedrock, respond)
24+
Flow:
25+
1. Slack sends webhook -> API Gateway -> Lambda (sync, 3s timeout)
26+
2. Lambda acknowledges immediately and triggers async self-invocation
27+
3. Async invocation processes Bedrock query and responds to Slack
2628
"""
2729
logger.info("Lambda invoked", extra={"is_async": event.get("async_processing", False)})
2830

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
def process_async_slack_event(slack_event_data):
2323
"""
24-
Process the actual Slack event
25-
Gets called asynchronously to avoid Lambda timeouts
24+
Process Slack events asynchronously after initial acknowledgment
25+
26+
This function handles the actual AI processing that takes longer than Slack's
27+
3-second timeout. It extracts the user query, calls Bedrock, and posts the response.
2628
"""
2729
event = slack_event_data["event"]
2830
event_id = slack_event_data["event_id"]
@@ -132,13 +134,16 @@ def store_conversation_session(conversation_key, session_id, user_id, channel_id
132134

133135
def query_bedrock(user_query, session_id=None):
134136
"""
135-
Query Bedrock knowledge base with optional conversation context
137+
Query Amazon Bedrock Knowledge Base using RAG (Retrieval-Augmented Generation)
138+
139+
This function retrieves relevant documents from the knowledge base and generates
140+
a response using the configured LLM model with guardrails for safety.
136141
"""
142+
137143
client = boto3.client(
138144
service_name="bedrock-agent-runtime",
139145
region_name=AWS_REGION,
140146
)
141-
142147
request_params = {
143148
"input": {"text": user_query},
144149
"retrieveAndGenerateConfiguration": {

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Slack event handlers - handles @mentions and DMs
2+
Slack event handlers - handles @mentions and direct messages to the bot
33
"""
44

55
import time
@@ -23,7 +23,8 @@ def log_request(slack_logger, body, next):
2323
@app.event("app_mention")
2424
def handle_app_mention(event, ack, body):
2525
"""
26-
Handle @mentions in channels
26+
Handle @mentions in channels - when users mention the bot in a channel
27+
Acknowledges the event immediately and triggers async processing to avoid timeouts
2728
"""
2829
ack()
2930

@@ -40,7 +41,8 @@ def handle_app_mention(event, ack, body):
4041
@app.event("message")
4142
def handle_direct_message(event, ack, body):
4243
"""
43-
Handle direct messages to the bot
44+
Handle direct messages to the bot - private 1:1 conversations
45+
Filters out channel messages and processes only direct messages
4446
"""
4547
ack()
4648

@@ -61,7 +63,10 @@ def handle_direct_message(event, ack, body):
6163

6264
def is_duplicate_event(event_id):
6365
"""
64-
Check if we've already processed this event
66+
Check if we've already processed this event using DynamoDB conditional writes
67+
68+
Slack may send duplicate events due to retries, so we use DynamoDB's
69+
conditional write to atomically check and record event processing.
6570
"""
6671
try:
6772
ttl = int(time.time()) + 3600 # 1 hour TTL
@@ -78,7 +83,13 @@ def is_duplicate_event(event_id):
7883

7984

8085
def trigger_async_processing(event_data):
81-
"""Fire off async processing to avoid timeout."""
86+
"""
87+
Trigger asynchronous Lambda invocation to process Slack events
88+
89+
Slack requires responses within 3 seconds, but Bedrock queries can take longer.
90+
This function invokes the same Lambda function asynchronously to handle the
91+
actual AI processing without blocking the initial Slack response.
92+
"""
8293
# incase we fail to re-invoke the lambda we should log an error
8394
try:
8495
lambda_client = boto3.client("lambda")

0 commit comments

Comments
 (0)