Skip to content

Instantly share code, notes, and snippets.

@oogali
Created July 26, 2016 19:57
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 oogali/26bc621d0a83fe1f4c2adb7e4b738922 to your computer and use it in GitHub Desktop.
Save oogali/26bc621d0a83fe1f4c2adb7e4b738922 to your computer and use it in GitHub Desktop.
exchange rate hipchat bot
import json
import requests
def lambda_handler(event, context):
message_id = event.get('message_id')
if not message_id:
return None
ccypair = event.get('ccypair')
if not ccypair:
return None
if not ccypair.startswith('/exchange '):
return None
currencies = ccypair.replace('/exchange ', '').strip().split('/', 2)
base_currency = currencies[0].upper()
quote_currency = currencies[1].upper()
r = requests.get('https://currency-api.appspot.com/api/{base}/{quote}.json'.format(base=base_currency,
quote=quote_currency))
response = r.json()
if 'success' not in response or response['success'] is not True:
return None
room_response = {
'attach_to': message_id,
'color': 'green',
'message': '<b>{base} to {quote}</b>: {rate}'.format(base=base_currency,
quote=quote_currency,
rate=response['rate']),
'notify': False,
'message_format': 'html'
}
print {'event': event, 'fetch': response, 'response': room_response}
return json.dumps(room_response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment