Created
January 21, 2020 01:31
-
-
Save shamsway/49e2a7f32a18cc9563c50cd1ba59f2ae to your computer and use it in GitHub Desktop.
YNAB Alert via AWS Lambda
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
from botocore.vendored import requests | |
import json | |
import boto3 | |
def lambda_handler(event, context): | |
api_key = "[YOUR API KEY]" | |
category1 = "[YOUR BUDGET CATEGORY ID]" | |
url = "https://api.youneedabudget.com/v1/budgets/last-used/months/current/categories/{}".format(category1) | |
# Create an SNS client | |
sns = boto3.client('sns') | |
payload = "" | |
headers = { | |
'Accept': "application/json", | |
'Authorization': "Bearer {}".format(api_key), | |
'cache-control': "no-cache", | |
} | |
response = requests.request("GET", url, data=payload, headers=headers) | |
data = response.json() | |
name = data['data']['category']['name'] | |
balance = data['data']['category']['balance'] / 1000 | |
print("{}: ${:,.2f}\nTime to categorize your transactions!".format(name, balance)) | |
# Publish a simple message to the specified SNS topic | |
response = sns.publish( | |
TopicArn='[YOUR TOPIC ARN]', | |
Message="{}: ${:,.2f}\nTime to categorize your transactions!".format(name, balance), | |
) | |
return { | |
'statusCode': 200, | |
'body': json.dumps("MessageId: {}\n{}: ${:,.2f}\nTime to categorize your transactions!".format(response['MessageId'], name, balance)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment