Created
February 2, 2021 15:13
-
-
Save shamsway/11ff6f1e2e77880ebc4f1c62bf132c6a to your computer and use it in GitHub Desktop.
Modified Lambda/YNAB SMS alerter
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): | |
budget_id = "[snip]" | |
api_key = "[snip]" | |
category1 = { | |
"id": "[snip]", | |
"name": "Fun Money" | |
} | |
category2 = { | |
"id": "[snip]", | |
"name": "Clothing" | |
} | |
category3 = { | |
"id": "[snip]", | |
"name": "Household" | |
} | |
category4 = { | |
"id": "[snip]", | |
"name": "Groceries" | |
} | |
url1 = "https://api.youneedabudget.com/v1/budgets/last-used/months/current/categories/{}".format(category1["id"]) | |
url2 = "https://api.youneedabudget.com/v1/budgets/last-used/months/current/categories/{}".format(category2["id"]) | |
url3 = "https://api.youneedabudget.com/v1/budgets/last-used/months/current/categories/{}".format(category3["id"]) | |
url4 = "https://api.youneedabudget.com/v1/budgets/last-used/months/current/categories/{}".format(category4["id"]) | |
# Create an SNS client | |
sns = boto3.client('sns') | |
payload = "" | |
headers = { | |
'Accept': "application/json", | |
'Authorization': "Bearer {}".format(api_key), | |
'cache-control': "no-cache", | |
} | |
response1 = requests.request("GET", url1, data=payload, headers=headers) | |
data1 = response1.json() | |
balance1 = data1['data']['category']['balance'] / 1000 | |
response2 = requests.request("GET", url2, data=payload, headers=headers) | |
data2 = response2.json() | |
balance2 = data2['data']['category']['balance'] / 1000 | |
response3 = requests.request("GET", url3, data=payload, headers=headers) | |
data3 = response3.json() | |
balance3 = data3['data']['category']['balance'] / 1000 | |
response4 = requests.request("GET", url4, data=payload, headers=headers) | |
data4 = response4.json() | |
balance4 = data4['data']['category']['balance'] / 1000 | |
print("{}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n".format(category1["name"], balance1, category2["name"], balance2, category3["name"], balance3, category4["name"], balance4)) | |
# Publish a simple message to the specified SNS topic | |
response = sns.publish( | |
TopicArn='arn:aws:sns:us-east-1:083364829142:ynab-alerts', | |
Message="\n{}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n".format(category1["name"], balance1, category2["name"], balance2, category3["name"], balance3, category4["name"], balance4), | |
) | |
return { | |
'statusCode': 200, | |
'body': json.dumps("MessageId: {}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n{}: ${:,.2f}\n".format(category1["name"], balance1, category2["name"], balance2, category3["name"], balance3, category4["name"], balance4)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment