Skip to content

Instantly share code, notes, and snippets.

@glenrobertson
Last active September 10, 2016 06:49
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 glenrobertson/03597fadf0eb20c2c4d45c1130393d66 to your computer and use it in GitHub Desktop.
Save glenrobertson/03597fadf0eb20c2c4d45c1130393d66 to your computer and use it in GitHub Desktop.
Ball game today AWS lambda slack webhook
import boto3
from base64 import b64decode
from urlparse import parse_qs
import logging
import urllib2
import json
logger = logging.getLogger()
logger.setLevel(logging.INFO)
CITIES = [
'atlanta',
'baltimore',
'bayarea',
'boston',
'chicago',
'cincinnati',
'dallas',
'denver',
'losangeles',
'minneapolis',
'neworleans',
'newyork',
'oakland',
'philadelphia',
'phoenix',
'pittsburgh',
'sanfrancisco',
'sanjose',
'seattle',
'washington',
]
def lambda_handler(event, context):
req_body = event['body']
params = parse_qs(req_body)
token = params['token'][0]
user = params['user_name'][0]
command = params['command'][0]
channel = params['channel_name'][0]
if 'text' in params:
pretty_location = params['text'][0].title()
else:
pretty_location = 'San Francisco'
location = pretty_location.replace(' ', '').lower()
cities_str = 'Valid locations are:\n {}'.format('\n'.join(CITIES))
if location == 'help':
return {
'text': cities_str + '\n\nData provided by http://gametonight.in'
}
elif location not in CITIES:
return {
'text': 'Invalid location "{}". {}'.format(params['text'][0], cities_str)
}
try:
response = urllib2.urlopen('http://gametonight.in/' + location + '/json').read()
except:
return {
'text': 'Not sure what you mean'
}
data = json.loads(response)
if data['game_tonight']:
result = ':warning: Yes'
today = data.get('today', {})
if len(today) > 0:
titles = ', '.join(event['title'] + ': ' + event['date'].split(' ')[1] for date, event in today.items())
result += ' ({})'.format(titles)
else:
result = ':beers: No ballgame in {}'.format(pretty_location)
return {
'response_type': 'in_channel',
'text': result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment