Sipgate-CLI/fax.sh

43 lines
1.5 KiB
Bash
Raw Normal View History

2021-03-04 17:29:32 +00:00
#! /bin/bash
#Check if I have enough parameters:
if [ -z $2 ]
then
echo "Usage:
$ ./fax.sh <letter.pdf> <recipient>
where <recipient> has to be a german faxline number"
exit 1
fi
2021-03-04 19:17:30 +00:00
BASEPATH=$(dirname $(realpath $0))
. $BASEPATH/authorization.sh
2021-03-04 17:29:32 +00:00
#Prepare fax credentials:
FILE=$1
echo -n "Sending file ${FILE} "
2021-03-04 17:29:32 +00:00
REC=$(echo $2 | sed -e 's:[^+0-9]::g' -e 's:^0\([1-9]\):+49\1:')
echo "to $REC"
2021-03-04 17:29:32 +00:00
#Needs to be base64 encoded:
CONTENT=$(base64 $FILE -w 0)
if [ -z "$CONTENT" ];
then
echo "Encoding failed"
2021-03-04 19:41:00 +00:00
exit 2
2021-03-04 17:29:32 +00:00
fi
2021-03-10 16:11:38 +00:00
#save payload to tempfile to prevent curl from throwing 'argument list too long' error.
echo '{"faxlineId":"f0", "recipient":"'${REC}'", "filename":"fax.pdf", "base64Content":"'${CONTENT}'"}' > /tmp/sipgatedata.txt
2021-03-04 17:29:32 +00:00
#Send fax
FAXID=$(curl --request POST --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $AUTH_TOKEN" --data @/tmp/sipgatedata.txt "https://api.sipgate.com/v2/sessions/fax" -s | jq '.sessionId' | sed -e 's:"::g' )
2021-03-10 16:11:38 +00:00
#remove tempfile
rm /tmp/sipgatedata.txt
if [ -z $FAXID ]
then
echo "Something went wrong, I didn't receive some fax id back."
exit 2
else
echo "Sipgate took your fax as sessionID $FAXID"
2021-03-04 19:17:30 +00:00
echo "$FAXID" >> $BASEPATH/.fax_history
fi
2021-03-04 17:29:32 +00:00
#Check if fax request was accepted by sipgate
curl --request GET --header 'Content-Type: application/json' --header 'Accept: application/json' --header "Authorization: Bearer $AUTH_TOKEN" "https://api.sipgate.com/v2/history/$FAXID" -s | jq '.faxStatusType'
2021-03-04 17:29:32 +00:00
#And show the account balance
2021-03-04 19:17:30 +00:00
. $BASEPATH/getbalance.sh