Skip to content

Commit 7d2d256

Browse files
committed
Slack service changes
1 parent 8d90254 commit 7d2d256

File tree

2 files changed

+45
-46
lines changed

2 files changed

+45
-46
lines changed

sample.env

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ PGVECTOR_PORT="5432"
1414

1515

1616
OPENAI_API_KEY="sk-..."
17-
AGENT_SERVICE_URL="http://localhost:8001"
17+
AGENT_SERVICE_URL="http://localhost:8001"
18+
SLACK_BOT_TOKEN=""
19+
SLACK_APP_SECRET=""

services/slack_service.py

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,84 @@
11
from flask import Flask, request, jsonify
22
import os
3-
import slack_sdk
3+
# import slack_sdk
44
from slack_sdk.web import WebClient
55
from slack_sdk.signature import SignatureVerifier
6-
# from rag_model import get_rag_response
7-
6+
import requests
7+
import json
8+
import threading
89
from dotenv import load_dotenv
910

1011
load_dotenv()
1112

12-
1313
app = Flask(__name__)
1414

15-
slack_token = "xoxb-8848313668450-8848330175890-NxONtME4RybfiHlAZfIDsJVf"
16-
signing_secret = "165bb0d941425645b37d1ff7a6d77f30"
15+
slack_token = os.getenv("SLACK_BOT_TOKEN")
16+
signing_secret = os.getenv("SLACK_APP_SECRET")
1717

1818
client = WebClient(token=slack_token)
1919
verifier = SignatureVerifier(signing_secret)
2020

2121
def get_rag_response(query: str) -> str:
22-
"""
23-
Query the agent service and get response
24-
"""
25-
import requests
26-
import json
27-
28-
# Configure the endpoint URL - adjust host/port as needed
29-
agent_service_url = f"{os.getenv("AGENT_SERVICE_URL")}/query"
30-
22+
agent_service_url = f"{os.getenv('AGENT_SERVICE_URL')}/query"
3123
try:
32-
# Make POST request to the agent service
3324
response = requests.post(
3425
agent_service_url,
3526
json={"query": query},
3627
headers={"Content-Type": "application/json"}
3728
)
38-
39-
# Check if request was successful
4029
response.raise_for_status()
41-
42-
# Parse JSON response
4330
result = response.json()
44-
45-
# Return the response text
4631
return result.get("response", "No response received from agent")
47-
4832
except requests.exceptions.RequestException as e:
49-
# Handle connection errors
5033
print(f"Error querying agent service: {e}")
5134
return f"Sorry, I couldn't reach the knowledge base. Error: {str(e)}"
52-
5335
except json.JSONDecodeError:
54-
# Handle invalid JSON response
5536
print(f"Invalid response from agent service: {response.text}")
5637
return "Sorry, I received an invalid response from the knowledge base."
5738

58-
@app.route("/slack/commands", methods=["POST"])
59-
def slack_commands():
60-
data = request.form
61-
command = data.get("command")
62-
if command == "/ping":
63-
return jsonify({
64-
"response_type": "in_channel",
65-
"text": "Pong! 🚀"
66-
})
67-
return "Unknown command", 200
39+
def send_delayed_response(response_url: str, user_id: str, query: str):
40+
"""Process the query and send the response back to Slack."""
41+
response_text = get_rag_response(query)
42+
payload = {
43+
"response_type": "in_channel", # or "ephemeral" for private replies
44+
"text": f"<@{user_id}> {response_text}"
45+
}
46+
try:
47+
requests.post(response_url, json=payload)
48+
except requests.exceptions.RequestException as e:
49+
print(f"Error sending response to Slack: {e}")
6850

6951
@app.route("/slack/prompt", methods=["POST"])
7052
def slack_prompt():
53+
# Verify the request (optional, but recommended)
54+
if not verifier.is_valid_request(request.get_data(), request.headers):
55+
return jsonify({"text": "Invalid request signature"}), 403
56+
7157
data = request.form
7258
user_id = data.get("user_id")
73-
query = data.get("text") # This is the user's prompt message
59+
query = data.get("text")
60+
response_url = data.get("response_url") # Slack provides this for delayed responses
7461

75-
# Change this to your RAG model's response function
76-
# This is where you would call your RAG model
77-
# For example, if you have a function `get_rag_response`:
78-
response = get_rag_response(query)
62+
# Start a background thread to process the query
63+
thread = threading.Thread(target=send_delayed_response, args=(response_url, user_id, query))
64+
thread.start()
7965

80-
# Respond back publicly in the slack channel or privately to the user
66+
# Immediately acknowledge the command
8167
return jsonify({
82-
"response_type": "in_channel", # or "ephemeral" for private replies
83-
"text": f"<@{user_id}> {response}"
68+
"response_type": "in_channel",
69+
"text": "Processing your request, please wait..."
8470
})
8571

72+
@app.route("/slack/commands", methods=["POST"])
73+
def slack_commands():
74+
data = request.form
75+
command = data.get("command")
76+
if command == "/ping":
77+
return jsonify({
78+
"response_type": "in_channel",
79+
"text": "Pong! 🚀"
80+
})
81+
return jsonify({"text": "Unknown command"}), 200
82+
8683
if __name__ == "__main__":
87-
app.run(debug=True,port=3000)
84+
app.run(debug=True, port=3000)

0 commit comments

Comments
 (0)