├── README.md ├── MessageMe_nginx.conf ├── text └── text_server /README.md: -------------------------------------------------------------------------------- 1 | # MessageMe 2 | A wrapper for the TextBelt API that allows messages to be sent with delays from the command line. 3 | 4 | 5 | Dependencies: 6 | sudo apt-get install nginx nginx-extras curl 7 | -------------------------------------------------------------------------------- /MessageMe_nginx.conf: -------------------------------------------------------------------------------- 1 | server { 2 | satisfy any; 3 | 4 | allow 127.0.0.1; 5 | deny all; 6 | 7 | auth_basic "Restricted Access"; 8 | auth_basic_user_file /etc/nginx/.htpasswd_MessageMe; 9 | 10 | 11 | location /text { 12 | content_by_lua ' 13 | 14 | -- Read the request body so we can access the POST data. 15 | ngx.req.read_body() 16 | local args = ngx.req.get_post_args() 17 | 18 | local message 19 | local timestamp 20 | 21 | -- Set the `message` and `timestamp` variables. 22 | for key, val in pairs(args) do 23 | if key == "message" then 24 | message = " \'" .. val .. "\'" 25 | elseif key == "timestamp" then 26 | timestamp = " " .. val 27 | end 28 | end 29 | 30 | -- Pass the message info to the text_server script. 31 | local server_script = "/bin/text_server" 32 | local handle = io.popen(server_script..message..timestamp) 33 | local result = handle:read("*a") 34 | handle:close() 35 | 36 | ngx.say("Request sent!") 37 | '; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /text: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | MESSAGE=$1; 3 | SERVER_URL="REDACTED"; 4 | USER="REDACTED" 5 | CURRENT_TIME=$(date +"%s"); 6 | 7 | if [ -z "$1" ]; then 8 | echo "Error: No message!" 9 | exit 0 10 | fi 11 | 12 | 13 | # Set a delay-time if applicable. 14 | if [ ! -z "$2" ]; then 15 | # If a delay-time supplied, calculate the final timestamp of send-time. 16 | MINUTE=60; 17 | HOUR=$((MINUTE * 60)); 18 | DAY=$((HOUR * 24)); 19 | 20 | STRING_ARR=($2); 21 | QUANTITY=${STRING_ARR[0]} 22 | 23 | # Calculate the delay according to what `$1` is. 24 | if [[ $2 =~ ^[0-9]+$ ]] ; then # Default: Minutes 25 | DELAY=$((MINUTE * $2)); 26 | 27 | 28 | elif [[ $2 =~ ^[0-9]+\ ?seconds?$ ]] ; then 29 | DELAY=$QUANTITY; 30 | 31 | elif [[ $2 =~ ^[0-9]+\ ?minutes?$ ]] ; then 32 | DELAY=$(($QUANTITY * $MINUTE)); 33 | 34 | elif [[ $2 =~ ^[0-9]+\ ?hours?$ ]] ; then 35 | DELAY=$(($QUANTITY * $HOUR)); 36 | 37 | elif [[ $2 =~ ^[0-9]+\ ?days?$ ]] ; then 38 | DELAY=$(($QUANTITY * $DAY)); 39 | 40 | else 41 | { # Try Block 42 | 43 | DUE=$(date -d "$2" +"%s") && 44 | DELAY=$((DUE - CURRENT_TIME)); 45 | 46 | } || { # Catch Block 47 | 48 | echo "Error: Unknown delay '$2'"; 49 | exit 0; 50 | 51 | } 52 | 53 | fi 54 | 55 | TIME=$((CURRENT_TIME + DELAY)); 56 | 57 | else 58 | # If a delay-time is *not* supplied, then send the message now. 59 | TIME=$CURRENT_TIME; 60 | 61 | fi 62 | 63 | 64 | text-message() { 65 | 66 | RESPONSE=$(curl POST $SERVER_URL \ 67 | --silent \ 68 | --user $USER \ 69 | -d "message=$MESSAGE" \ 70 | -d "timestamp=$TIME"); 71 | 72 | echo -e $RESPONSE; 73 | 74 | } 75 | 76 | text-message; 77 | -------------------------------------------------------------------------------- /text_server: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | # Project Settings 4 | NUMBER=1234567890; 5 | LOG_PATH="/var/log/MessageMe/log.txt" 6 | NGINX_FILE="MessageMe_nginx.conf" 7 | WEB_USER="REDACTED"; 8 | 9 | 10 | # Run `sudo ./text_server` to install. 11 | if [ -z "$1" ]; then 12 | if [[ $(whoami) = "root" ]] ; then 13 | 14 | echo "Running server setup..." 15 | 16 | FULL_FILE_PATH="$(readlink -f $0)" 17 | FILE_NAME="$(basename $FULL_FILE_PATH)" 18 | FILE_DIR="$(dirname $FULL_FILE_PATH)" 19 | 20 | # Create the LOGFILE if it doesn't exist. 21 | echo "... Creating log-file" 22 | mkdir -p $(dirname $LOG_PATH) 23 | touch $LOG_PATH; 24 | chown $WEB_USER $LOG_PATH; 25 | 26 | 27 | # Based on: http://stackoverflow.com/a/630387/2456258 28 | echo "... Deleting old /bin/ script" 29 | SCRIPT_DEST="/bin/$FILE_NAME"; 30 | if [ -e $SCRIPT_DEST ]; then 31 | echo "... Deleting old /bin/ script"; 32 | rm $SCRIPT_DEST; 33 | fi 34 | echo "... Adding current script path to /bin/" 35 | ln -s $FULL_FILE_PATH $SCRIPT_DEST 36 | 37 | 38 | # Set up the nginx path. 39 | NGINX_DEST="/etc/nginx/sites-enabled/$NGINX_FILE"; 40 | if [ -e $NGINX_DEST ]; then 41 | echo "... Deleting old nginx configuration"; 42 | rm $NGINX_DEST; 43 | fi 44 | 45 | echo "... Enabling nginx configuration" 46 | ln -s $FILE_DIR/$NGINX_FILE $NGINX_DEST; 47 | service nginx restart 48 | 49 | 50 | echo "... Setup complete!" 51 | exit 0; 52 | 53 | fi 54 | fi 55 | 56 | 57 | 58 | # Argument Setup. 59 | MESSAGE=$1; 60 | TIME_DUE=$2; 61 | 62 | 63 | # Calculate when this datetime should be sent off. 64 | TIME_NOW=$(date +"%s"); 65 | WAIT=$((TIME_DUE-TIME_NOW)); 66 | 67 | send-text-from-textbelt () { 68 | # Argument 1: The delay-time before calling curl. 69 | if [ ! -z "$1" ]; then 70 | sleep $1; 71 | fi 72 | 73 | RESPONSE=$(curl -s POST http://textbelt.com/text \ 74 | -d number=$NUMBER \ 75 | -d "message=$MESSAGE"); 76 | 77 | echo "'$(date)' delayed $WAIT @ $NUMBER ::: $RESPONSE" >> $LOG_PATH; 78 | 79 | } 80 | 81 | 82 | # If the message was already due, then send it off without delay. 83 | if [[ ! $WAIT -gt 0 ]]; then 84 | send-text-from-textbelt & 85 | 86 | else 87 | send-text-from-textbelt $WAIT & 88 | fi 89 | 90 | 91 | --------------------------------------------------------------------------------