-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
74 lines (62 loc) · 2.05 KB
/
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
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
import logging
import os
from signal import signal, SIGINT
from sys import exit
from Secret import Secret
from requests.exceptions import HTTPError
from weather import get_weather_for_zipcode
from zipcode import get_location_from_zipcode
from slack_bolt import App
def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
exit(0)
SECRET = Secret.get_instance()
app = App(
token=SECRET.secrets.get("SLACK_BOT_TOKEN"),
signing_secret=SECRET.secrets.get("SLACK_SIGNING_SECRET")
)
@app.command("/weather")
def weather(ack, say, command):
ack()
user_id = command.get("user_id")
prefix = f"Hey <@{user_id}>, "
zipcode = command.get("text")
if zipcode is None:
say(f"{prefix}provide a zipcode.")
return
#TODO: input validation
try:
weather = get_weather_for_zipcode(zipcode, SECRET.secrets["WEATHER_API_KEY"])
except HTTPError as e:
logging.exception("Failed to get weather for zipcode %s, %s", zipcode, e.response)
say(f"{prefix}\"Could not get weather for {zipcode}.\"")
return
feels_like_k = weather["main"]["feels_like"]
feels_like_f = (feels_like_k - 273.15) * 1.8000 + 32.00
try:
location = get_location_from_zipcode(zipcode, SECRET.secrets["ZIPCODE_API_KEY"])
except HTTPError as e:
logging.exception("Failed to get location from zipcode %s, %s", zipcode, e.response)
message = f"{prefix}It feels like {feels_like_f:.0f} in {zipcode}."
else:
city = location["city"]
state = location["state"]
message = f"{prefix}It feels like {feels_like_f:.0f} in {city}, {state}."
blocks = {
"response_type": "in_channel",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": message
}
}
]
}
say(blocks)
# Start your app
if __name__ == "__main__":
signal(SIGINT, handler)
app.start(port=int(os.environ.get("PORT", 3000)))