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)