-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
120 lines (101 loc) · 5.39 KB
/
app.py
File metadata and controls
120 lines (101 loc) · 5.39 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
119
120
from flask import Flask, request, jsonify
import json
import sys
import os
import time
import requests
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
with open("channels.json") as f:
channels = json.load(f)
slack = App(token=os.environ.get("SLACK_BOT_TOKEN"))
app = Flask(__name__)
def post_install(channel_id, install, name, unit, phone, email, location, ticket, description):
try:
response = slack.client.chat_postMessage(
channel=channel_id,
blocks=[
{
"type": "section",
"text": {"type": "mrkdwn", "text": f"*Install* <https://map.nycmesh.net/nodes/{install}|{install}> - {name}\n*Location:* {location}\n*Unit:* {unit}\n*Phone Number:* {phone}\n*E-Mail Address:* {email}\n*Ticket:* <https://support.nycmesh.net/scp/tickets.php?number={ticket}|#{ticket}>\n*Description:* {description}"}
}
],
text=f"*Install* {install} - {name} | *Location:* {location} | *Unit:* {unit} | *Phone Number:* {phone} | *E-Mail Address:* {email} | * Description:* {description}"
)
print(f"Message sent: {response['ts']}")
except Exception as e:
print(f"Error sending message: {e}")
@app.route("/webhook", methods=["POST"])
def webhook():
try:
data = request.get_json(force=True) # Parse JSON
# Pretty-print JSON to stdout
print(json.dumps(data, indent=4), file=sys.stdout, flush=True)
id = data["data"]["id"]
install = data["data"]["install_number"]
node = data["data"]["node"]["network_number"]
unit = data["data"]["unit"]
member_id = data["data"]["member"]["id"]
building_id = data["data"]["building"]["id"]
headers = {
"accept": "application/json",
"Authorization": "Token " + os.environ.get("MESHDB_API_KEY"),
}
members_url = f"https://db.nycmesh.net/api/v1/members/{member_id}/"
response = requests.get(members_url, headers=headers)
if response.status_code == 200:
member = response.json()
name = member.get("name")
email = member.get("primary_email_address")
phone = member.get("phone_number")
try:
channel_id = channels[str(node)]["channel_id"]
description = channels[str(node)]["description"]
except KeyError as e:
print("204, Error message:", e)
return jsonify({"status": "success"}), 204
else:
print("MeshDB Error (getting member):", response.status_code, response.text)
return jsonify({"status": "error", "message": str(response.text)}), response.status_code
buildings_url = f"https://db.nycmesh.net/api/v1/buildings/{building_id}/"
buildings_response = requests.get(buildings_url, headers=headers)
if buildings_response.status_code == 200:
new_building = buildings_response.json()
street_address = new_building.get("street_address")
city = new_building.get("city")
state = new_building.get("state")
zip_code = new_building.get("zip_code")
location = street_address + ", " + city + ", " + state + ", " + zip_code
else:
print("MeshDB Error (getting buildings):", buildings_response.status_code, buildings_response.text)
return jsonify({"status": "error", "message": str(buildings_response.text)}), buildings_response.status_code
installs_url = f"https://db.nycmesh.net/api/v1/installs/{id}/"
installs_response = requests.get(installs_url, headers=headers)
if installs_response.status_code == 200:
new_install = installs_response.json()
# Retry up to 3 additional times if ticket_number is None
ticket_number = new_install.get("ticket_number")
retries = 0
while ticket_number is None and retries < 3:
print(f"No ticket yet, retrying ({retries + 1}/3)...")
time.sleep(3)
installs_response = requests.get(installs_url, headers=headers)
if installs_response.status_code != 200:
print("MeshDB Error (retrying install):", installs_response.status_code, installs_response.text)
return jsonify({"status": "error", "message": str(installs_response.text)}), installs_response.status_code
new_install = installs_response.json()
ticket_number = new_install.get("ticket_number")
retries += 1
if ticket_number is None:
print("No ticket after retries!")
return jsonify({"status": "no ticket"}), 500
else:
print("MeshDB Error (getting install):", installs_response.status_code, installs_response.text)
return jsonify({"status": "error", "message": str(installs_response.text)}), installs_response.status_code
post_install(channel_id=channel_id, install=install, name=name, unit=unit, phone=phone, email=email, location=location, ticket=ticket_number, description=description)
return jsonify({"status": "success"}), 200
except Exception as e:
print(f"400, Error: {e}", file=sys.stderr, flush=True)
return jsonify({"status": "error", "message": str(e)}), 400
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000, debug=True)