Skip to content

Instantly share code, notes, and snippets.

@nasamuffin
Last active November 23, 2015 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasamuffin/8f4e3aa8f25b2d8797c9 to your computer and use it in GitHub Desktop.
Save nasamuffin/8f4e3aa8f25b2d8797c9 to your computer and use it in GitHub Desktop.
AWS lambda implementation of a slackbot stockbroker app
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