Skip to content

Instantly share code, notes, and snippets.

@cr5315
Created March 24, 2016 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cr5315/809c42ca3aaf1192affc to your computer and use it in GitHub Desktop.
Save cr5315/809c42ca3aaf1192affc to your computer and use it in GitHub Desktop.
Tasker script to calculate bank balance from SMS messages
console.log("US Bank Balance Tasker Task");
var TYPE_DEPOSIT = 0;
var TYPE_NEITHER = 1;
var TYPE_PURCHASE = 2;
var US_BANK_PHONE_NUMBER = 872265;
// Whether the script is being called by Tasker on an Android device, or it's being called on a PC
var onAndroid = (global("SDK") > 0);
if (!onAndroid) {
console.log("Not an Android device");
// We're on a PC, so we need to set a few things
// Like the phone number that sent us an SMS
var sender = 872265; // This is the US Bank number, just so that PC testing always goes through
var balance = 156.75; // Dummy balance
// And we need the text of the SMS
var message = "U.S. Bank Alerts\nAccount: 1177\nTran Date: 3/23\nTran Criteria: CR>$10.00\nTran Amnt: $28.60\nOpt-out txt STOP";
} else {
var sender = global("SMSRF");
var balance = global("Balance");
var message = global("SMSRB");
}
// If the message is from US Bank, we want to math it so good
if (sender == US_BANK_PHONE_NUMBER) {
console.log("SMS is from US Bank.");
console.log("Starting balance: $" + balance);
// We need to get the type of transaction so we can do the right maths
lines = message.match(/[^\r\n]+/g);
// The lines we actually need are 3 and 4
var type = getType(lines[3]);
var amount = getAmount(lines[4]);
if (type == TYPE_DEPOSIT) {
balance = +balance + +amount;
} else if (type == TYPE_PURCHASE) {
balance = balance - amount;
}
setGlobal("Balance", balance);
console.log("New balance: $" + balance);
}
function getAmount(line) {
// Alright so US Bank is dumb and this is the format of the arg line
// Tran Amnt: $28.60
// We just want the 28.60
words = line.split(" ");
return words[2].replace("$", "");
}
function getType(line) {
// As previously mentioned, US Bank is dumb
// Tran Criteria: DR>$10.00
// If I got money, it's CR, if I paid money, it's DR
words = line.split(" ");
if (words[2].startsWith("CR")) return TYPE_DEPOSIT;
else if (words[2].startsWith("DR")) return TYPE_PURCHASE;
else return TYPE_NEITHER;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment