-
Notifications
You must be signed in to change notification settings - Fork 1
Slack service changes #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shatakshiiii
wants to merge
1
commit into
main
Choose a base branch
from
slack_service_changes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,87 +1,84 @@ | ||
| from flask import Flask, request, jsonify | ||
| import os | ||
| import slack_sdk | ||
| # import slack_sdk | ||
| from slack_sdk.web import WebClient | ||
| from slack_sdk.signature import SignatureVerifier | ||
| # from rag_model import get_rag_response | ||
|
|
||
| import requests | ||
| import json | ||
| import threading | ||
| from dotenv import load_dotenv | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| app = Flask(__name__) | ||
|
|
||
| slack_token = "xoxb-8848313668450-8848330175890-NxONtME4RybfiHlAZfIDsJVf" | ||
| signing_secret = "165bb0d941425645b37d1ff7a6d77f30" | ||
| slack_token = os.getenv("SLACK_BOT_TOKEN") | ||
| signing_secret = os.getenv("SLACK_APP_SECRET") | ||
|
|
||
| client = WebClient(token=slack_token) | ||
| verifier = SignatureVerifier(signing_secret) | ||
|
|
||
| def get_rag_response(query: str) -> str: | ||
| """ | ||
| Query the agent service and get response | ||
| """ | ||
| import requests | ||
| import json | ||
|
|
||
| # Configure the endpoint URL - adjust host/port as needed | ||
| agent_service_url = f"{os.getenv("AGENT_SERVICE_URL")}/query" | ||
|
|
||
| agent_service_url = f"{os.getenv('AGENT_SERVICE_URL')}/query" | ||
| try: | ||
| # Make POST request to the agent service | ||
| response = requests.post( | ||
| agent_service_url, | ||
| json={"query": query}, | ||
| headers={"Content-Type": "application/json"} | ||
| ) | ||
|
|
||
| # Check if request was successful | ||
| response.raise_for_status() | ||
|
|
||
| # Parse JSON response | ||
| result = response.json() | ||
|
|
||
| # Return the response text | ||
| return result.get("response", "No response received from agent") | ||
|
|
||
| except requests.exceptions.RequestException as e: | ||
| # Handle connection errors | ||
| print(f"Error querying agent service: {e}") | ||
| return f"Sorry, I couldn't reach the knowledge base. Error: {str(e)}" | ||
|
|
||
| except json.JSONDecodeError: | ||
| # Handle invalid JSON response | ||
| print(f"Invalid response from agent service: {response.text}") | ||
| return "Sorry, I received an invalid response from the knowledge base." | ||
|
|
||
| @app.route("/slack/commands", methods=["POST"]) | ||
| def slack_commands(): | ||
| data = request.form | ||
| command = data.get("command") | ||
| if command == "/ping": | ||
| return jsonify({ | ||
| "response_type": "in_channel", | ||
| "text": "Pong! 🚀" | ||
| }) | ||
| return "Unknown command", 200 | ||
| def send_delayed_response(response_url: str, user_id: str, query: str): | ||
| """Process the query and send the response back to Slack.""" | ||
| response_text = get_rag_response(query) | ||
| payload = { | ||
| "response_type": "in_channel", # or "ephemeral" for private replies | ||
| "text": f"<@{user_id}> {response_text}" | ||
| } | ||
| try: | ||
| requests.post(response_url, json=payload) | ||
| except requests.exceptions.RequestException as e: | ||
| print(f"Error sending response to Slack: {e}") | ||
|
|
||
| @app.route("/slack/prompt", methods=["POST"]) | ||
| def slack_prompt(): | ||
| # Verify the request (optional, but recommended) | ||
| if not verifier.is_valid_request(request.get_data(), request.headers): | ||
| return jsonify({"text": "Invalid request signature"}), 403 | ||
|
|
||
| data = request.form | ||
| user_id = data.get("user_id") | ||
| query = data.get("text") # This is the user's prompt message | ||
| query = data.get("text") | ||
| response_url = data.get("response_url") # Slack provides this for delayed responses | ||
|
|
||
| # Change this to your RAG model's response function | ||
| # This is where you would call your RAG model | ||
| # For example, if you have a function `get_rag_response`: | ||
| response = get_rag_response(query) | ||
| # Start a background thread to process the query | ||
| thread = threading.Thread(target=send_delayed_response, args=(response_url, user_id, query)) | ||
| thread.start() | ||
|
|
||
| # Respond back publicly in the slack channel or privately to the user | ||
| # Immediately acknowledge the command | ||
| return jsonify({ | ||
| "response_type": "in_channel", # or "ephemeral" for private replies | ||
| "text": f"<@{user_id}> {response}" | ||
| "response_type": "in_channel", | ||
| "text": "Processing your request, please wait..." | ||
| }) | ||
|
|
||
| @app.route("/slack/commands", methods=["POST"]) | ||
| def slack_commands(): | ||
| data = request.form | ||
| command = data.get("command") | ||
| if command == "/ping": | ||
| return jsonify({ | ||
| "response_type": "in_channel", | ||
| "text": "Pong! 🚀" | ||
| }) | ||
| return jsonify({"text": "Unknown command"}), 200 | ||
|
|
||
| if __name__ == "__main__": | ||
| app.run(debug=True,port=3000) | ||
| app.run(debug=True, port=3000) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Get secrets like above to make sure default value is a string and not
Null. This is becauseSignatureVerifierdoes not acceptNullvalue