Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,535 Bytes
b7eca85 |
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 |
import os
from slack_sdk import WebClient
from slack_sdk.errors import SlackApiError
from flask import Flask, request, Response
app = Flask(__name__)
SLACK_TOKEN = 'YOUR_SLACK_BOT_TOKEN'
SLACK_SIGNING_SECRET = 'YOUR_SLACK_SIGNING_SECRET'
TARGET_CHANNEL_ID = 'YOUR_TARGET_SLACK_CHANNEL_ID'
TRIGGERS = {
"urgent": "<@U061W3NRFFA>", # Replace with actual Slack user IDs
"help": "<@U87654321>",
}
slack_client = WebClient(token=SLACK_TOKEN)
@app.route("/slack/events", methods=["POST"])
def slack_events():
if not verify_slack_request(request):
return Response(status=403)
data = request.json
if "event" in data:
event = data["event"]
if event.get("type") == "message" and not "subtype" in event:
handle_message_event(event)
return Response(status=200)
def verify_slack_request(request):
return True
def handle_message_event(event):
user = event.get("user")
text = event.get("text").lower()
channel = event.get("channel")
for trigger, slack_mention in TRIGGERS.items():
if trigger in text:
post_to_slack(channel, slack_mention, text)
break
def post_to_slack(channel, slack_mention, text):
try:
response = slack_client.chat_postMessage(
channel=TARGET_CHANNEL_ID,
text=f"{slack_mention} New message in <#{channel}>: {text}"
)
except SlackApiError as e:
print(f"Error posting to Slack: {e.response['error']}")
if __name__ == "__main__":
app.run(port=3000)
|