-
-
Save digideskio/a9eabf75a8e6b6d95689d3cec7fdfd02 to your computer and use it in GitHub Desktop.
Slack, Uber Eats
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from ff.slack import LambdaHandler | |
EXPECTED_TOKENS = ("lol ya right :dogespin:") | |
def lambda_handler(event, context): | |
handler = LambdaHandler(event, context=context, expected_tokens=EXPECTED_TOKENS, method=_target) | |
return handler.call() | |
def _target(command, channel, user, command_text): | |
attachments = [] | |
command_text = command_text.lower() | |
if command_text in ('atx', 'aus', 'austin'): | |
attachments.extend(fetch('ATX')) | |
elif command_text in ('htx', 'hou', 'houston'): | |
attachments.extend(fetch('HOUSTON')) | |
else: | |
attachments.append({ | |
"text": "Failed to find city {}".format(command_text) | |
}) | |
return { | |
"response_type": "in_channel", | |
"attachments": attachments | |
} | |
def fetch(command_text): | |
attachments = [] | |
response = requests.post( | |
url="https://ubereats-city.appspot.com/_api/menus.get", | |
headers={ | |
"Accept": "application/json", | |
"Content-Type": "application/json", | |
}, | |
json={ | |
"city": command_text.upper() | |
} | |
) | |
if response.status_code == 200: | |
menus = response.json()['menu'] | |
todays_menu = menus[0] | |
for meal in todays_menu['meals']: | |
if meal['type'].lower() == "lunch": | |
order_url = meal['order_url'] | |
description = meal['description'] | |
dish = meal['dish'] | |
image_url = meal['image_url'] | |
price = meal['price'] | |
restaurant = meal['restaurant'] | |
attachments.append({ | |
"title": dish, | |
"title_link": order_url, | |
"thumb_url": image_url, | |
"text": description, | |
"fields": [ | |
{ | |
"title": "Restaurant", | |
"value": restaurant, | |
"short": True | |
}, | |
{ | |
"title": "Price", | |
"value": price, | |
"short": True | |
} | |
] | |
}) | |
return attachments | |
else: | |
return [{ | |
"text": "Failed to retrieve menu with HTTP status code {}".format(response.status_code) | |
}] | |
if __name__ == '__main__': | |
# For testing. | |
import json | |
print json.dumps(lambda_handler({ | |
"body-json": "token=%s&" | |
"team_id=T0001&" | |
"team_domain=example&" | |
"channel_id=C2147483705&" | |
"channel_name=test&" | |
"user_id=U2147483697&" | |
"user_name=Steve&" | |
"command=/food&" | |
"text=atx&" | |
"response_url=https://hooks.slack.com/commands/1234/5678" % EXPECTED_TOKENS[0] | |
}, None)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import logging | |
from urlparse import parse_qs | |
BODY_NAME = 'body-json' | |
class LambdaHandler: | |
def __init__(self, event, context, expected_tokens, method): | |
self.event = event | |
self.context = context | |
self.expected_tokens = expected_tokens | |
self.method = method | |
params = self._parse_event() | |
self._check_token(params['token'][0]) | |
self.user = params['user_name'][0] | |
self.command = params['command'][0] | |
self.channel = params['channel_name'][0] | |
self.command_text = params['text'][0] | |
def call(self): | |
return self.method( | |
command=self.command, | |
channel=self.channel, | |
user=self.user, | |
command_text=self.command_text | |
) | |
def _parse_event(self): | |
if BODY_NAME not in self.event: | |
raise Exception("Content body not found.") | |
req_body = self.event[BODY_NAME] | |
try: | |
params = parse_qs(req_body) | |
except Exception as e: | |
logging.error("Exception retrieving request token.") | |
raise Exception("Exception retrieving request token.", e) | |
return params | |
def _check_token(self, token): | |
if token not in self.expected_tokens: | |
logging.error("Invalid request token.") | |
raise Exception("Invalid request token.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment