-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.sh
executable file
·98 lines (80 loc) · 2.54 KB
/
webhook.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env sh
# * Variables
WEBHOOK_FOLDER_NAME="webhook"
[ -z "$WEBHOOK_FOLDER" ] && export WEBHOOK_FOLDER=$HOME/.config/$WEBHOOK_FOLDER_NAME
CONFIG_FILE=$WEBHOOK_FOLDER/config
# * Functions
generateDelayRequest() { # Return the request for a delayed call, input parameters are message, mins, key
REQUEST="https://script.google.com/macros/s/AKfycbx0hWSQlfMuJ47xgYqHO5O2fAfRXb5p8e66in8A7mmFmHpEATU/exec?action=$1&mins=$2&key=$3"
}
generateRequest() {
REQUEST="https://maker.ifttt.com/trigger/$1/with/key/$2"
}
generateMessage() {
MESSAGE="turn_$1_$2"
}
checkKey() {
if [ ! -f "$CONFIG_FILE" ]; then
brecho "Config file doesn't exist!"
echo "Create the file $CONFIG_FILE with the KEY variable"
exit 3
fi
# shellcheck disable=SC1090
. "$CONFIG_FILE" # Sourcing the key file
if [ -z "$KEY" ]; then # Check if the KEY parameter exist
brecho "No KEY configured in the configuration file"
echo "Write your KEY as a bash variable inside $CONFIG_FILE"
exit 4
fi
}
brecho() { # Bold Red echo
# shellcheck disable=SC2059
printf "${F_BOLD}${C_RED}$1${C_NO_COLOR}\n"
}
gecho() { # Green echo
# shellcheck disable=SC2059
printf "${C_GREEN}$1${C_NO_COLOR}\n"
}
makeWebRequest() { # Make the web request
FILENAME="$WEBHOOK_FOLDER/requestResult"
wget -O "$FILENAME" "$REQUEST" >/dev/null 2>&1 # Make web request and suppress output
requestResult=$(cat "$FILENAME") # Print request result on a variable
rm "$FILENAME" # Remove request result
if [ -z "$requestResult" ]; then # if request result it's empty
brecho "ERROR firing the event: Invalid KEY"
exit 5
fi
echo "$requestResult"
exit 0
}
# * Script
if [ -z "$1" ]; then # Check if the DEVICE parameter exist
brecho "No DEVICE provided"
echo "Usage: webhook.sh [DEVICE] [0/1] optional[DELAY]"
exit 1
elif [ -z "$2" ]; then # Check turn status
brecho "No action provided"
echo "Usage: webhook.sh [DEVICE] [0/1] optional[DELAY]"
exit 2
fi
checkKey
# MESSAGE GENERATION
COMMAND=$2
DEVICE=$1
if [ "$COMMAND" = "1" ]; then
COMMAND="on"
elif [ "$COMMAND" = "0" ]; then
COMMAND="off"
else
brecho "Wrong command provided"
echo "Valid values are: 0 ; 1"
exit 1
fi
generateMessage $COMMAND "$DEVICE"
if [ -z "$3" ]; then # Check if delay parameter exist
generateRequest "$MESSAGE" "$KEY"
else
generateDelayRequest "$MESSAGE" "$3" "$KEY"
fi
gecho "Turning $COMMAND $DEVICE with key $KEY"
makeWebRequest