Skip to content
This repository was archived by the owner on Jul 14, 2023. It is now read-only.

Commit 678f973

Browse files
committedJun 8, 2019
small adjustment to exceptions
1 parent adb13c9 commit 678f973

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed
 

‎README.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# slack-slash-lambda
22
This repo holds code and configuration for lambda based [slash commands](https://api.slack.com/slash-commands) for Slack.
33

4+
## Details for geeks
5+
Slack allows your API to respond in sync or async manner. For synchronus calls the
6+
timeout is 3 seconds. For any work that might take longer than that it is recommended to use callback url while responding with `200 OK` to the initial call.
7+
The configuration in thie repo configures API gateway to run the Lambdas asynchronusly.
8+
9+
410
# Pre-Requirements
511
To deploy this you will need the following
612

‎slack_slash/app.py

+17-14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
import hmac
33
from urllib.parse import parse_qs
44
from functools import partial
5-
import json
65

76
import requests
7+
from requests.exceptions import RequestException
88

99

1010
def lambda_handler(event, context):
@@ -51,9 +51,11 @@ def get_sessions(callback, secret):
5151
"""
5252
try:
5353
r = requests.get(f"https://sessionize.com/api/v2/{secret}/view/sessions")
54-
except Exception as e:
55-
print("sessionize didn't respond correctly")
56-
raise e
54+
r.raise_for_status()
55+
except RequestException as e:
56+
print("error: ", e)
57+
callback("error while attempting to reach sessionize.com")
58+
return
5759

5860
sessions = r.json()[0]["sessions"]
5961
content = f":star2: Number of talk submissions: *{len(sessions)}*\n"
@@ -79,12 +81,14 @@ def get_tickets(callback, secret, org, slug):
7981
"Authorization": f"Token token={secret}",
8082
},
8183
)
82-
except Exception as e:
83-
print("ti.to chocked")
84-
raise e
84+
r.raise_for_status()
85+
except RequestException as e:
86+
print("error: ", e)
87+
callback("error while attempting to reach ti.to")
88+
return
8589

8690
tickets = r.json()["tickets"]
87-
content = f"tickets sold: {len(tickets)}"
91+
content = f"tickets sold: *{len(tickets)}*"
8892

8993
callback(content)
9094

@@ -97,17 +101,16 @@ def send(url, content):
97101

98102
try:
99103
r = requests.post(url, json=payload)
100-
except Exception as e:
101-
print("slack didn't like that")
104+
r.raise_for_status()
105+
except RequestException as e:
106+
print("error while sending callback to slack")
102107
raise e
103108

104-
r.raise_for_status()
105-
106109

107110
def verify(*, key, version, timestamp, payload, signature):
108111
"""
109-
verify slack message.
110-
info: https://api.slack.com/docs/verifying-requests-from-slack#a_recipe_for_security
112+
verify slack message. see more:
113+
https://api.slack.com/docs/verifying-requests-from-slack#a_recipe_for_security
111114
"""
112115
msg = f"{version}:{timestamp}:{payload}".encode()
113116
h = hmac.new(key, msg, "sha256")

0 commit comments

Comments
 (0)
This repository has been archived.