-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
38 lines (26 loc) · 960 Bytes
/
app.py
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
import os
import json
from flask import Flask, request, jsonify
import requests
from jinja2 import Environment, FileSystemLoader, select_autoescape
app = Flask(__name__)
# Load the Slack webhook URL from an environment variable
SLACK_WEBHOOK_URL = os.getenv("SLACK_WEBHOOK_URL")
# Initialize Jinja2 environment
env = Environment(loader=FileSystemLoader("."))
@app.route("/webhook", methods=["POST"])
def webhook():
data = request.json
# Load the template
template = env.get_template("slack_message_template.j2")
# Render the template with the data
slack_message = json.loads(template.render(data))
response = requests.post(SLACK_WEBHOOK_URL, json=slack_message)
if response.status_code != 200:
return (
jsonify({"status": "failed", "reason": response.text}),
response.status_code,
)
return jsonify({"status": "success"}), 200
if __name__ == "__main__":
app.run(port=5000)