Created
July 26, 2016 19:57
-
-
Save oogali/26bc621d0a83fe1f4c2adb7e4b738922 to your computer and use it in GitHub Desktop.
exchange rate hipchat bot
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 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