bml-tg-notify/bml-tg-notify.sh

110 lines
3.9 KiB
Bash
Raw Normal View History

2021-05-13 22:40:24 +05:00
#!/bin/bash
2021-05-14 02:44:12 +05:00
source .env # import credentials, tg api, cookie path, bml api
init(){
2021-05-14 02:27:01 +05:00
if [ ! -f delay ] # if delay file missing
2021-05-14 01:47:41 +05:00
then
2021-05-14 02:27:01 +05:00
echo 160 > delay # make delay file with 160 sec
2021-05-14 01:47:41 +05:00
fi
}
login(){
2022-04-15 23:57:54 +05:00
$curl -s -c $COOKIE $BML_URL/login --data-raw username=$BML_USERNAME --data-raw password=${BML_PASSWORD} # attempt to login and generate cookie
PROFILE=$($curl -s -b $COOKIE $BML_URL/profile | jq -r '.payload | .profile | .[] | .profile' | head -n 1) ; echo $PROFILE # get Personal Profile
$curl -s -b $COOKIE $BML_URL/profile --data-raw profile=$PROFILE # select Personal Profile
}
getaccountdetails(){
2022-04-15 23:57:54 +05:00
REQACCOUNTDETAILS=$($curl -s -b $COOKIE $BML_URL/account/$BML_ACCOUNTID | jq -r .payload)
ACCOUNTTYPE=$(echo $REQACCOUNTDETAILS | jq -r .product)
ACCOUNTNUMBER=$(echo $REQACCOUNTDETAILS | jq -r .accountNumber)
CURRENCY=$(echo $REQACCOUNTDETAILS | jq -r .currency)
}
send_tg(){
TGTEXT=$(echo $DESCRIPTION%0A$FROMTOAT: $ENTITY%0A$CURRENCY: $AMOUNT | sed "s/ /%20/g") ; echo $TGTEXT # format text for telegram
2022-04-15 23:57:54 +05:00
$curl -s $TG_BOTAPI$TG_BOT_TOKEN/sendMessage?chat_id=$TG_CHATID'&'text=$TGTEXT #send to telegram
echo "Next check in $DELAY seconds"
}
req_history(){
2022-04-15 23:57:54 +05:00
REQ_HISTORY=$($curl -s -b $COOKIE $BML_URL/account/$BML_ACCOUNTID/history/today) ; echo $REQ_HISTORY
LOGIN_STATUS=$(echo $REQ_HISTORY | jq -r .success) ; echo $LOGIN_STATUS
}
check_diff(){
2021-05-15 01:08:54 +05:00
CHECKDIFF1=$(echo $HISTORY | wc -c) ; echo $CHECKDIFF1 # check intial and previous history
2022-04-15 23:57:54 +05:00
#HISTORY=$($curl -s -b $COOKIE $BML_URL/account/$BML_ACCOUNTID/history/today | jq -r '.payload | .history | .[]') ; echo $HISTORY # request history
HISTORY=$(echo $REQ_HISTORY | jq -r '.payload | .history | .[]') ; echo $HISTORY
2021-05-15 01:08:54 +05:00
CHECKDIFF2=$(echo $HISTORY | wc -c) ; echo $CHECKDIFF2 # check new history
}
read_delay(){
2021-05-15 01:08:54 +05:00
DELAY=$(cat delay) ; echo $DELAY # read delay file and get value
}
echo_delay(){
echo "Nothing new....Next check in $DELAY seconds"
}
init
login
getaccountdetails
loop(){
while true; do
req_history
if [ "$LOGIN_STATUS" != "true" ]
then
2021-09-19 20:20:51 +05:00
echo "Looks like app or web was opened... checking again in $APP_OPEN_DELAY"
sleep $APP_OPEN_DELAY
login
break & loop
fi
check_diff
read_delay
2021-05-14 02:27:01 +05:00
if [ "$CHECKDIFF1" != "$CHECKDIFF2" ] # if previous history do not match with new history
2021-05-13 22:40:24 +05:00
then
if [ "$CHECKDIFF2" = "1" ]
2021-05-13 22:40:24 +05:00
then
echo "=============" ; echo NEW DAY ; echo "============="
2022-04-15 23:57:54 +05:00
# $curl -s $TG_BOTAPI$TG_BOT_TOKEN/sendMessage?chat_id=$TG_CHATID'&'text=GO%20TO%20SLEEP%0AITS%0000
else
echo $HISTORY | jq
DESCRIPTION=$(echo $HISTORY | jq -r .description | head -n1) ; echo $DESCRIPTION # get last trascation description
AMOUNT=$(echo $HISTORY | jq -r .amount | head -n1) ; echo $AMOUNT # get last trascation amount
if [ "$DESCRIPTION" = "Transfer Credit" ] # if last trascation is description is Transfer Credit
then
FROMTOAT=From
ENTITY=$(echo $HISTORY | jq -r .narrative3 | head -n1) ; echo $ENTITY # get last persona or place name
elif [ "$DESCRIPTION" = "Transfer Debit" ] # if last trascation descripton is Transfer Debit
then
FROMTOAT=To
ENTITY=$(echo $HISTORY | jq -r .narrative3 | head -n1) ; echo $ENTITY # get last person or place name
2021-08-27 17:28:03 +05:00
elif [ "$DESCRIPTION" = "Cash Deposit-ATM" ] || [ "$DESCRIPTION" = "ATM Withdrawal" ] || [ "$DESCRIPTION" = "Purchase" ] # if last trascation descripton is ATM Withdrawal
then
FROMTOAT=At
ENTITY=$(echo $HISTORY | jq -r .narrative3 | head -n1) ; echo $ENTITY #get last ATM name
elif [ "$DESCRIPTION" = "Salary" ] # if last trascation descripton is Salary
then
FROMTOAT=From
ENTITY=$(echo $HISTORY | jq -r .narrative2 | head -n1) ; echo $ENTITY # get last trascation company name
2022-01-18 18:38:54 +00:00
elif [ "$DESCRIPTION" = "Prepaid Top-Up" ] # if last trascation descripton is Prepaid Top-Up
then
FROMTOAT=To
ENTITY=$(echo $HISTORY | jq -r .narrative3 | head -n1) ; echo $ENTITY # get name..idk
fi
send_tg
2021-05-13 22:40:24 +05:00
fi
else
echo_delay
2021-05-13 22:40:24 +05:00
fi
2021-05-14 02:27:01 +05:00
sleep $DELAY # initiate delay read from delay file
2021-05-13 22:40:24 +05:00
done
}
loop