-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (94 loc) · 3.65 KB
/
main.py
File metadata and controls
118 lines (94 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from flask import Flask, request, jsonify
import requests
import os
import rag
import threading
import json
import logging
import re
from dotenv import load_dotenv
load_dotenv(dotenv_path='secrets.env')
# Initialize Flask app
app = Flask(__name__)
# Slack configuration (replace with your actual token and signing secret)
SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
SLACK_SIGNING_SECRET = os.environ.get("SLACK_SIGNING_SECRET")
processed_message_ids = set()
logging.basicConfig(
filename="slack_events.log", # Save logs to a file
level=logging.INFO,
format="%(asctime)s - %(message)s",
)
@app.route("/slack/events", methods=["POST"])
def slack_events():
"""Handles Slack events like incoming messages."""
data = request.json
logging.info("Incoming Slack Event: %s", json.dumps(data, indent=2))
# Slack URL verification
if "challenge" in data:
return jsonify({"challenge": data["challenge"]})
#____________________________________________________
if "event" in data:
event = data["event"]
event_type = event.get("type")
user = event.get("user")
text = event.get("text")
channel = event.get("channel")
ts = event.get("ts")
# Log specific event details
logging.info("Event Type: %s", event_type)
logging.info("User: %s", user)
logging.info("Text: %s", text)
logging.info("Channel: %s", channel)
logging.info("Timestamp: %s", ts)
#________________________________________________________
# Process message events
if "event" in data:
event = data["event"]
if event.get("type") == "message" and (not event.get("subtype") or event.get("subtype") == "file_share"):
message_id = event.get("client_msg_id")
# Skip if message has already been processed
if message_id in processed_message_ids or event.get("bot_id") is not None:
return jsonify({"status": "duplicate_message_skipped"})
# Add message ID to the processed set
processed_message_ids.add(message_id)
# Start background thread to process the message
thread = threading.Thread(target=process_message_async, args=(event,))
thread.start()
# Acknowledge Slack immediately
return jsonify({"status": "ok"})
return jsonify({"status": "ok"})
def process_message_async(event):
"""Processes the Slack message asynchronously."""
user_message = event.get("text")
channel_id = event.get("channel")
thread_ts = event.get("ts")
attachments = event.get("files", [])
if not user_message.startswith("!bot"):
return
rag_response = ""
if contains_link(user_message) or len(attachments)>0:
#RAG can't process externals links or attachments
rag_response = "I cannot process links or attachments with my current capability"
else:
rag_response = rag.init(user_message)
# Send the generated response back to Slack
send_message_to_slack(channel_id, rag_response, thread_ts)
def send_message_to_slack(channel_id, message, thread_ts):
"""Sends a message to Slack."""
url = "https://slack.com/api/chat.postMessage"
headers = {"Authorization": f"Bearer {SLACK_BOT_TOKEN}"}
payload = {
"channel": channel_id,
"text": message,
}
#Reply in thread
if thread_ts:
payload["thread_ts"] = thread_ts
requests.post(url, headers=headers, json=payload)
def contains_link(s):
# Regular expression to match URLs
url_regex = r'https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
return bool(re.search(url_regex, s))
if __name__ == "__main__":
app.run(port=2323)