Last active
November 23, 2015 15:29
-
-
Save nasamuffin/8f4e3aa8f25b2d8797c9 to your computer and use it in GitHub Desktop.
AWS lambda implementation of a slackbot stockbroker app
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 json | |
import urlparse | |
import urllib | |
import re | |
print('Loading function') | |
def lambda_handler(event, context): | |
if not 'text' in urlparse.parse_qs(event['postBody']): | |
return "Don't waste my time!" | |
symbols = urlparse.parse_qs(event['postBody'])['text'][0] | |
channel = urlparse.parse_qs(event['postBody'])['channel_name'][0] | |
urllib.urlopen('https://amazon-robotics.slack.com/services/hooks/slackbot?token=REDACTED&channel=%23' + channel, | |
'My accountant just called! Apparently ' + pretty_quotes(symbols)) | |
return "That's so cool, I let everyone know!" | |
def get_quote(symbol): | |
base_url = 'http://finance.google.com/finance?q=' | |
content = urllib.urlopen(base_url + symbol).read() | |
m = re.search(r'"price"\s+content="(.*?)"', content) | |
if m: | |
quote = m.group(1) | |
else: | |
quote = 'no quote available for: ' + symbol | |
return quote | |
def pretty_quotes(symbols): | |
return ", ".join([symbol.upper() + ": $" + get_quote(symbol) for symbol in symbols.split()]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment