Skip to content

Commit 8fb1ea9

Browse files
Fix: [AEA-6308] - Fix Command Endpoint (#397)
## Summary Fix Slack Command endpoint for `/test` --------- Co-authored-by: bencegadanyi1-nhs <bence.gadanyi1@nhs.net>
1 parent 73bea4a commit 8fb1ea9

3 files changed

Lines changed: 45 additions & 44 deletions

File tree

packages/slackBotFunction/app/slack/slack_events.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,16 @@ def process_async_slack_event(event: Dict[str, Any], event_id: str, client: WebC
433433
process_slack_message(event=event, event_id=event_id, client=client)
434434

435435

436-
def process_async_slack_command(command: Dict[str, Any], client: WebClient) -> None:
437-
logger.debug("Processing async Slack command", extra={"command": command})
436+
def process_async_slack_command(body: Dict[str, Any], client: WebClient) -> None:
437+
logger.debug("Processing async Slack command", extra={"body": body})
438438

439439
try:
440-
command_arg = command.get("command", "").strip()
440+
command_arg = body.get("command", "").strip()
441441
if command_arg == "/test":
442-
process_command_test_request(command=command, client=client)
442+
process_command_test_request(body=body, client=client)
443443
except Exception as e:
444444
logger.error(f"Error processing test command: {e}", extra={"error": traceback.format_exc()})
445-
post_error_message(channel=command["channel_id"], thread_ts=None, client=client)
445+
post_error_message(channel=body["channel_id"], thread_ts=None, client=client)
446446

447447

448448
# ================================================================
@@ -474,7 +474,7 @@ def process_pull_request_slack_command(slack_command_data: Dict[str, Any]) -> No
474474
command = slack_command_data["event"]
475475
token = get_bot_token()
476476
client = WebClient(token=token)
477-
process_async_slack_command(command=command, client=client)
477+
process_async_slack_command(body=command, client=client)
478478
except Exception:
479479
# we cant post a reply to slack for this error as we may not have details about where to post it
480480
logger.error(processing_error_message, extra={"error": traceback.format_exc()})
@@ -804,22 +804,23 @@ def _toggle_button_style(element: dict) -> bool:
804804
# ================================================================
805805

806806

807-
def process_command_test_request(command: Dict[str, Any], client: WebClient) -> None:
808-
if "help" in command.get("text"):
809-
process_command_test_help(command=command, client=client)
807+
def process_command_test_request(body: Dict[str, Any], client: WebClient) -> None:
808+
logger.info("Processing Command Test Request", extra={"body": body})
809+
if "help" in body.get("text"):
810+
process_command_test_help(body=body, client=client)
810811
else:
811-
process_command_test_questions(command=command, client=client)
812+
process_command_test_questions(body=body, client=client)
812813

813814

814-
def process_command_test_questions(command: Dict[str, Any], client: WebClient) -> None:
815+
def process_command_test_questions(body: Dict[str, Any], client: WebClient) -> None:
815816
# Prepare response
816817

817818
try:
818-
acknowledgement_msg = f"<@{command.get("user_id")}> has initiated testing."
819-
logger.info(acknowledgement_msg, extra={"command": command})
819+
acknowledgement_msg = f"<@{body.get('user_id')}> has initiated testing."
820+
logger.info(acknowledgement_msg, extra={"body": body})
820821

821822
# Extract parameters
822-
params = extract_test_command_params(command.get("text"))
823+
params = extract_test_command_params(body.get("text"))
823824

824825
# Is the command targeting a PR
825826
pr = params.get("pr", "").strip()
@@ -829,7 +830,7 @@ def process_command_test_questions(command: Dict[str, Any], client: WebClient) -
829830

830831
# Initial acknowledgment
831832
post_params = {
832-
"channel": command["channel_id"],
833+
"channel": body["channel_id"],
833834
"text": acknowledgement_msg,
834835
}
835836
client.chat_postMessage(**post_params)
@@ -846,10 +847,10 @@ def process_command_test_questions(command: Dict[str, Any], client: WebClient) -
846847

847848
# Post query information (for reflection in future)
848849
post_params = {
849-
"channel": command["channel_id"],
850+
"channel": body["channel_id"],
850851
"text": acknowledgement_msg,
851852
}
852-
client.chat_postEphemeral(**post_params, user=command.get("user_id"))
853+
client.chat_postEphemeral(**post_params, user=body.get("user_id"))
853854

854855
# Retrieve sample questions
855856
test_questions = SampleQuestionBank().get_questions(start=start - 1, end=end - 1)
@@ -876,7 +877,7 @@ def process_command_test_questions(command: Dict[str, Any], client: WebClient) -
876877
futures.append(future)
877878

878879
post_params["text"] = "Testing complete, generating file..."
879-
client.chat_postEphemeral(**post_params, user=command.get("user_id"))
880+
client.chat_postEphemeral(**post_params, user=body.get("user_id"))
880881

881882
aggregated_results = []
882883
for i, future in enumerate(futures):
@@ -897,7 +898,7 @@ def process_command_test_questions(command: Dict[str, Any], client: WebClient) -
897898

898899
# Upload the file to Slack
899900
client.files_upload_v2(
900-
channel=command["channel_id"],
901+
channel=body["channel_id"],
901902
content=final_file_content,
902903
title=filename,
903904
filename=filename,
@@ -907,7 +908,7 @@ def process_command_test_questions(command: Dict[str, Any], client: WebClient) -
907908
except Exception as e:
908909
logger.error(
909910
f"Failed to attach feedback buttons: {e}",
910-
extra={"channel": command["channel_id"], "error": traceback.format_exc()},
911+
extra={"channel": body["channel_id"], "error": traceback.format_exc()},
911912
)
912913

913914

@@ -939,8 +940,8 @@ def process_command_test_ai_request(question, response, output: bool, client: We
939940
return {"index": f"{question[0]}", "text": question[1], "response": response_text}
940941

941942

942-
def process_command_test_help(command: Dict[str, Any], client: WebClient) -> None:
943-
logger.info("Processing Command Test Help Message", extra={"command": command})
943+
def process_command_test_help(body: Dict[str, Any], client: WebClient) -> None:
944+
logger.info("Processing Command Test Help Message", extra={"body": body})
944945
length = SampleQuestionBank().length() + 1
945946
help_text = f"""
946947
Certainly! Here is some help testing me!
@@ -962,7 +963,7 @@ def process_command_test_help(command: Dict[str, Any], client: WebClient) -> Non
962963
- /test q10-16 --> Sends questions 10 to 16
963964
- /test .output -> Sends questions 1 to {length} and posts them to Slack
964965
""" # noqa: E501
965-
client.chat_postEphemeral(channel=command["channel_id"], user=command["user_id"], text=help_text)
966+
client.chat_postEphemeral(channel=body["channel_id"], user=body["user_id"], text=help_text)
966967

967968

968969
# ================================================================

packages/slackBotFunction/app/slack/slack_handlers.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from functools import lru_cache
1313
import traceback
1414
from typing import Any, Dict
15-
from slack_bolt import Ack, App, Say
15+
from slack_bolt import Ack, App
1616
from slack_sdk import WebClient
1717
from app.core.config import (
1818
get_logger,
@@ -67,9 +67,9 @@ def respond_to_action(ack: Ack):
6767

6868

6969
# ack function for commands where we just send an ack response back
70-
def respond_to_command(ack: Ack, say: Say):
71-
logger.debug("Sending ack response for command")
72-
ack()
70+
def respond_to_command(body, ack):
71+
logger.debug("Sending ack response for help command", extra={"body": body, "ack": ack})
72+
ack("Testing initiated")
7373

7474

7575
def feedback_handler(body: Dict[str, Any], client: WebClient) -> None:
@@ -149,20 +149,20 @@ def unified_message_handler(client: WebClient, event: Dict[str, Any], body: Dict
149149
logger.error("Error triggering async processing for event", extra={"error": traceback.format_exc()})
150150

151151

152-
def command_handler(body: Dict[str, Any], command: Dict[str, Any], client: WebClient) -> None:
152+
def command_handler(respond, body, client: WebClient) -> None:
153153
"""Handle /test command to prompt the bot to respond."""
154-
logger.info("Received command from user", extra={"body": body, "command": command, "client": client})
155-
if not command:
154+
logger.info("Received command from user", extra={"body": body})
155+
if not body:
156156
logger.error("Invalid command payload")
157157
return
158158

159-
user_id = command.get("user_id")
160-
session_pull_request_id = extract_test_command_params(command.get("text")).get("pr")
159+
user_id = body.get("user_id")
160+
session_pull_request_id = extract_test_command_params(body.get("text")).get("pr")
161161
if session_pull_request_id:
162162
logger.info(f"Command in pull request session {session_pull_request_id} from user {user_id}")
163163
forward_to_pull_request_lambda(
164164
body=body,
165-
event={**command, "channel": command.get("channel_id")},
165+
event={**body, "channel": body.get("channel_id")},
166166
pull_request_id=session_pull_request_id,
167167
event_id=f"/command-{time.time()}",
168168
store_pull_request_id=False,
@@ -171,6 +171,6 @@ def command_handler(body: Dict[str, Any], command: Dict[str, Any], client: WebCl
171171
return
172172

173173
try:
174-
process_async_slack_command(command, client)
174+
process_async_slack_command(body=body, client=client)
175175
except Exception:
176176
logger.error("Error triggering async processing for command", extra={"error": traceback.format_exc()})

packages/slackBotFunction/tests/test_slack_commands.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_process_slack_command_test_questions_ai_request_to_file(
127127
mock_client.chat_postMessage.return_value = mock_response
128128

129129
# perform operation
130-
process_async_slack_command(command=slack_command_data, client=mock_client)
130+
process_async_slack_command(body=slack_command_data, client=mock_client)
131131

132132
# assertions
133133
assert mock_query_bedrock.call_count == 21
@@ -163,7 +163,7 @@ def test_process_slack_command_test_questions_default_to_file(
163163
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
164164

165165
# perform operation
166-
process_async_slack_command(command=slack_command_data, client=mock_client)
166+
process_async_slack_command(body=slack_command_data, client=mock_client)
167167

168168
# assertions
169169
mock_client.chat_postMessage.assert_called_once()
@@ -202,7 +202,7 @@ def test_process_slack_command_test_questions_single_question_to_file(
202202
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
203203

204204
# perform operation
205-
process_async_slack_command(command=slack_command_data, client=mock_client)
205+
process_async_slack_command(body=slack_command_data, client=mock_client)
206206

207207
# assertions
208208
mock_client.chat_postMessage.asset_called_once()
@@ -237,7 +237,7 @@ def test_process_slack_command_test_questions_two_questions_to_file(
237237
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
238238

239239
# perform operation
240-
process_async_slack_command(command=slack_command_data, client=mock_client)
240+
process_async_slack_command(body=slack_command_data, client=mock_client)
241241

242242
# assertions
243243
mock_client.chat_postMessage.assert_called_once()
@@ -272,7 +272,7 @@ def test_process_slack_command_test_questions_ai_request_to_slack(
272272
mock_client.chat_postMessage.return_value = mock_response
273273

274274
# perform operation
275-
process_async_slack_command(command=slack_command_data, client=mock_client)
275+
process_async_slack_command(body=slack_command_data, client=mock_client)
276276

277277
# assertions
278278
assert mock_query_bedrock.call_count == 21
@@ -308,7 +308,7 @@ def test_process_slack_command_test_questions_default_to_slack(
308308
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
309309

310310
# perform operation
311-
process_async_slack_command(command=slack_command_data, client=mock_client)
311+
process_async_slack_command(body=slack_command_data, client=mock_client)
312312

313313
# assertions
314314
mock_client.chat_postMessage.assert_called()
@@ -347,7 +347,7 @@ def test_process_slack_command_test_questions_single_question_to_slack(
347347
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
348348

349349
# perform operation
350-
process_async_slack_command(command=slack_command_data, client=mock_client)
350+
process_async_slack_command(body=slack_command_data, client=mock_client)
351351

352352
# assertions
353353
mock_client.chat_postMessage.assert_called()
@@ -381,7 +381,7 @@ def test_process_slack_command_test_questions_two_questions_to_slack(
381381
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
382382

383383
# perform operation
384-
process_async_slack_command(command=slack_command_data, client=mock_client)
384+
process_async_slack_command(body=slack_command_data, client=mock_client)
385385

386386
# assertions
387387
mock_client.chat_postMessage.assert_called()
@@ -423,7 +423,7 @@ def test_process_slack_command_test_questions_too_many_questions_error(
423423

424424
# with pytest.raises(ValueError, match="'end' must be less than 21"):
425425
# perform operation
426-
process_async_slack_command(command=slack_command_data, client=mock_client)
426+
process_async_slack_command(body=slack_command_data, client=mock_client)
427427

428428
# assertions
429429
mock_client.chat_postMessage.asset_not_called()
@@ -460,7 +460,7 @@ def test_process_slack_command_test_help(
460460
mock_process_ai_query.return_value = {"text": "ai response", "session_id": None, "citations": [], "kb_response": {}}
461461

462462
# perform operation
463-
process_async_slack_command(command=slack_command_data, client=mock_client)
463+
process_async_slack_command(body=slack_command_data, client=mock_client)
464464

465465
# assertions
466466
mock_client.chat_postMessage.assert_not_called()

0 commit comments

Comments
 (0)