-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_app.py
29 lines (22 loc) · 989 Bytes
/
aws_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
from urllib import parse as urlparse
import base64
import json
import requests
from app import command_handler
def send_to_slack(json, url):
"""Send JSON to Slack response URL"""
requests.post(url, json=json, timeout=10)
def lambda_handler(event, _):
"""Handle SNS message, retrieve Slack message then run ranking logic"""
sns_message = json.loads(event['Records'][0]['Sns']['Message'])
slack_request = dict(urlparse.parse_qsl(base64.b64decode(str(sns_message['body'])).decode('ascii')))
# Build arguments for command_handler
body = {"text": (slack_request["command"] + " " + slack_request.get("text", "")).strip()}
def ack(text):
"""Send private text to user"""
respond({"text":text})
def respond(json):
"""Send JSON back to Slack, which may publish in the channel"""
send_to_slack(json, slack_request["response_url"])
# Call command handler
return command_handler(ack, body, respond)