Hi guys, I've just finished creating two widgets, one which shows my live bank balance, and one that shows my stock portfolio balance. I can share snippets of code if anyone would like to recreate it!
Bank Balance
Connects to my bank using TrueLayer API
Zapier connects to TrueLayer via webhook POST to firstly, refresh the auth token
Zapier connects to TrueLayer via webhook GET to acquire the accounts balance
Zapier uses Utilities to convert line-item to text. This extracts the balance from the API response
Zapier uses the Google Drive zap to replace a txt file with a new one containing the balance
Google drive syncs that file to my laptop automatically, replacing the previous one
BTT uses applescript to return the balance from the txt file to the title
Thats it! The only code required for everything is step 7 (below). Zapier does the rest.
set theFile to (read POSIX file "Volumes/GoogleDrive/My Drive/BankConnect/Balance.txt")
set theFile to "£" & theFile
return theFile
Trading Portfolio Balance
Uses a list of trades which live update on google sheets and adds the total to a specific cell
Zapier extracts the cells value hourly
Zapier uses the Google Drive zap to replace a txt file with a new one containing the balance
Google drive syncs that file to my laptop automatically, replacing the previous one
BTT uses applescript to return the balance from the txt file to the title
My specific trading app does not have an API as of yet so its not fully live as I have to update the trades manually, but that isn't too common for me so it will be fine until they release an API.
Tried calling a webhook directly from BTT using Javascript, but it didn't work.
I want it to call the webhook, wait for the response then output to BTT to display in result in Menubar.
Run it every X hours, store the result in a Dynamic Variable, and put the dynamic variable in the menu bar next to an SF Symbol [Updates the text with the Dynamic variable].
But, it doesn't allow me to add an icon next to it.
And it doesn't update unless I click on it on the menu bar.
And sometimes when I click on it, it runs, displays the webhook response, and disappears after a couple of seconds...
Tried it, but It kept sending requests to the webhook URL, it doesn't wait for the webhook response,
So It runs the script over and over again.
The webhook takes a few seconds to return data, it shows the name I've set for the automation, and sometimes an error message from the script on the menu bar.
IDK What's wrong.
Code I used: (I used ChatGPT to generate it)
#!/bin/bash
# Set the webhook URL and payload
WEBHOOK_URL="Webhook link HERE"
PAYLOAD='{"foo": "bar"}'
# Send the initial request
send_request() {
RESPONSE=$(curl --header "Content-Type: application/json" \
--request POST \
--data "${PAYLOAD}" \
--write-out "%{http_code}:%{response_body}" \
--silent \
"${WEBHOOK_URL}")
RESPONSE_CODE=$(echo "${RESPONSE}" | awk -F':' '{print $1}')
RESPONSE_BODY=$(echo "${RESPONSE}" | sed 's/^[^:]*://')
}
# Send the first request
send_request
# Loop indefinitely and send a request every 3 hours
while true; do
echo "Waiting for 3 hours before sending next request..."
sleep 10800
send_request
if [[ "${RESPONSE_CODE}" == "200" ]]; then
echo "Webhook response received: ${RESPONSE_BODY}"
else
echo "Webhook request failed with status code ${RESPONSE_CODE}"
fi
done