|
1 | 1 | from flask import Flask, request, jsonify |
2 | 2 | import os |
3 | | -import slack_sdk |
| 3 | +# import slack_sdk |
4 | 4 | from slack_sdk.web import WebClient |
5 | 5 | from slack_sdk.signature import SignatureVerifier |
6 | | -# from rag_model import get_rag_response |
7 | | - |
| 6 | +import requests |
| 7 | +import json |
| 8 | +import threading |
8 | 9 | from dotenv import load_dotenv |
9 | 10 |
|
10 | 11 | load_dotenv() |
11 | 12 |
|
12 | | - |
13 | 13 | app = Flask(__name__) |
14 | 14 |
|
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") |
17 | 17 |
|
18 | 18 | client = WebClient(token=slack_token) |
19 | 19 | verifier = SignatureVerifier(signing_secret) |
20 | 20 |
|
21 | 21 | 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" |
31 | 23 | try: |
32 | | - # Make POST request to the agent service |
33 | 24 | response = requests.post( |
34 | 25 | agent_service_url, |
35 | 26 | json={"query": query}, |
36 | 27 | headers={"Content-Type": "application/json"} |
37 | 28 | ) |
38 | | - |
39 | | - # Check if request was successful |
40 | 29 | response.raise_for_status() |
41 | | - |
42 | | - # Parse JSON response |
43 | 30 | result = response.json() |
44 | | - |
45 | | - # Return the response text |
46 | 31 | return result.get("response", "No response received from agent") |
47 | | - |
48 | 32 | except requests.exceptions.RequestException as e: |
49 | | - # Handle connection errors |
50 | 33 | print(f"Error querying agent service: {e}") |
51 | 34 | return f"Sorry, I couldn't reach the knowledge base. Error: {str(e)}" |
52 | | - |
53 | 35 | except json.JSONDecodeError: |
54 | | - # Handle invalid JSON response |
55 | 36 | print(f"Invalid response from agent service: {response.text}") |
56 | 37 | return "Sorry, I received an invalid response from the knowledge base." |
57 | 38 |
|
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}") |
68 | 50 |
|
69 | 51 | @app.route("/slack/prompt", methods=["POST"]) |
70 | 52 | 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 | + |
71 | 57 | data = request.form |
72 | 58 | 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 |
74 | 61 |
|
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() |
79 | 65 |
|
80 | | - # Respond back publicly in the slack channel or privately to the user |
| 66 | + # Immediately acknowledge the command |
81 | 67 | 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..." |
84 | 70 | }) |
85 | 71 |
|
| 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 | + |
86 | 83 | if __name__ == "__main__": |
87 | | - app.run(debug=True,port=3000) |
| 84 | + app.run(debug=True, port=3000) |
0 commit comments