Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from slack_sdk import WebClient
|
3 |
+
from slack_sdk.errors import SlackApiError
|
4 |
+
from flask import Flask, request, Response
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
SLACK_TOKEN = 'YOUR_SLACK_BOT_TOKEN'
|
9 |
+
SLACK_SIGNING_SECRET = 'YOUR_SLACK_SIGNING_SECRET'
|
10 |
+
TARGET_CHANNEL_ID = 'YOUR_TARGET_SLACK_CHANNEL_ID'
|
11 |
+
|
12 |
+
TRIGGERS = {
|
13 |
+
"urgent": "<@U061W3NRFFA>", # Replace with actual Slack user IDs
|
14 |
+
"help": "<@U87654321>",
|
15 |
+
}
|
16 |
+
|
17 |
+
slack_client = WebClient(token=SLACK_TOKEN)
|
18 |
+
|
19 |
+
@app.route("/slack/events", methods=["POST"])
|
20 |
+
def slack_events():
|
21 |
+
if not verify_slack_request(request):
|
22 |
+
return Response(status=403)
|
23 |
+
|
24 |
+
data = request.json
|
25 |
+
if "event" in data:
|
26 |
+
event = data["event"]
|
27 |
+
if event.get("type") == "message" and not "subtype" in event:
|
28 |
+
handle_message_event(event)
|
29 |
+
|
30 |
+
return Response(status=200)
|
31 |
+
|
32 |
+
def verify_slack_request(request):
|
33 |
+
return True
|
34 |
+
|
35 |
+
def handle_message_event(event):
|
36 |
+
user = event.get("user")
|
37 |
+
text = event.get("text").lower()
|
38 |
+
channel = event.get("channel")
|
39 |
+
|
40 |
+
for trigger, slack_mention in TRIGGERS.items():
|
41 |
+
if trigger in text:
|
42 |
+
post_to_slack(channel, slack_mention, text)
|
43 |
+
break
|
44 |
+
|
45 |
+
def post_to_slack(channel, slack_mention, text):
|
46 |
+
try:
|
47 |
+
response = slack_client.chat_postMessage(
|
48 |
+
channel=TARGET_CHANNEL_ID,
|
49 |
+
text=f"{slack_mention} New message in <#{channel}>: {text}"
|
50 |
+
)
|
51 |
+
except SlackApiError as e:
|
52 |
+
print(f"Error posting to Slack: {e.response['error']}")
|
53 |
+
|
54 |
+
if __name__ == "__main__":
|
55 |
+
app.run(port=3000)
|