+ # It's a bit cheesy, but we'll just use the content-type header to
+ # determine if we're being called from a Slack event or from a
+ # slash command or other interactivity. (The more typical way to
+ # do this would be to have different URLs for each Slack entry
+ # point, but it's simpler to have our Slack app implemented as a
+ # single AWS Lambda, (which can only have a single entry point).
+ content_type = headers['content-type']
+
+ if (content_type == "application/json"):
+ return turbot_event_handler(turb, event, context)
+ if (content_type == "application/x-www-form-urlencoded"):
+ return turbot_interactive_or_slash_command(turb, event, context)
+ return error("Unknown content-type: {}".format(content_type))
+
+def turbot_event_handler(turb, event, context):
+ """Handler for all subscribed Slack events"""
+
+ body = json.loads(event['body'])
+
+ type = body['type']
+
+ if type == 'url_verification':
+ return url_verification_handler(turb, body)
+ if type == 'event_callback':
+ return event_callback_handler(turb, body)
+ return error("Unknown event type: {}".format(type))
+
+def url_verification_handler(turb, body):
+
+ # First, we have to properly respond to url_verification
+ # challenges or else Slack won't let us configure our URL as an
+ # event handler.
+ challenge = body['challenge']
+
+ return {
+ 'statusCode': 200,
+ 'body': challenge
+ }
+
+def event_callback_handler(turb, body):
+ event = body['event']
+ type = event['type']
+
+ if type in turbot.events.events:
+ return turbot.events.events[type](turb, event)
+ return error("Unknown event type: {}".format(type))
+
+def turbot_interactive_or_slash_command(turb, event, context):
+ """Handler for Slack interactive things (buttons, shortcuts, etc.)
+ as well as slash commands.
+
+ This function simply makes a quick determination of what we're looking
+ at and then defers to either turbot_interactive or turbot_slash_command."""
+
+ # Both interactives and slash commands have a urlencoded body