|
# Assumes curl and jq are installed |
|
|
|
################################## |
|
# REQUIRED INPUT: API key/secret # |
|
################################## |
|
|
|
# Retrieve from https://web.vestaboard.com; API docs at https://docs.vestaboard.com |
|
VESTABOARD_API_KEY="" |
|
VESTABOARD_API_SECRET="" |
|
|
|
########################### |
|
# Input/message detection # |
|
########################### |
|
|
|
# Detect input string; if empty, assign a generic message with the current date |
|
VESTABOARD_TEXT_TO_SEND=$1 |
|
if [ -z "$VESTABOARD_TEXT_TO_SEND" ] |
|
then |
|
VESTABOARD_TEXT_TO_SEND="Hello. It is currently $(date)." |
|
echo "== Note about script invocation ==" |
|
echo "You did not provide an argument to this script so we will assign the following default input value:" |
|
echo " $VESTABOARD_TEXT_TO_SEND" |
|
echo |
|
echo "Next time, define your own message by invoking the script like this:" |
|
echo " bash $0 \"Hello from the command line!\"" |
|
echo |
|
fi |
|
|
|
############# |
|
# Functions # |
|
############# |
|
|
|
# Helper function to call an arbitrary HTTP GET endpoint of Vestaboard API |
|
function vestboard_api_call_get { |
|
local ENDPOINT=$1 |
|
local URL="https://platform.vestaboard.com$ENDPOINT" |
|
|
|
local API_CALL_RESPONSE=$(curl -s \ |
|
-H "X-Vestaboard-Api-Key: $VESTABOARD_API_KEY" \ |
|
-H "X-Vestaboard-Api-Secret: $VESTABOARD_API_SECRET" \ |
|
"$URL") |
|
echo $API_CALL_RESPONSE |
|
} |
|
|
|
# Helper function to call an arbitrary HTTP POST endpoint of Vestaboard API |
|
function vestboard_api_call_post { |
|
local ENDPOINT=$1 |
|
local BODY=$2 |
|
local URL="https://platform.vestaboard.com$ENDPOINT" |
|
|
|
local API_CALL_RESPONSE=$(curl -s \ |
|
-X POST \ |
|
-H "X-Vestaboard-Api-Key: $VESTABOARD_API_KEY" \ |
|
-H "X-Vestaboard-Api-Secret: $VESTABOARD_API_SECRET" \ |
|
-d "$BODY" \ |
|
$URL) |
|
echo $API_CALL_RESPONSE |
|
} |
|
|
|
# Get the API key/secret's viewer info and echo it out |
|
function viewer_info { |
|
echo "== Vestaboard Viewer == " |
|
VESTABOARD_VIEWER=$(vestboard_api_call_get "/viewer") |
|
echo $VESTABOARD_VIEWER | jq |
|
echo |
|
} |
|
|
|
# Publish a message to a subscription and report the API output |
|
function publish_message { |
|
echo "== Publish message to Subscription ==" |
|
local SUBSCRIPTION_ID=$1 |
|
local MESSAGE=$2 |
|
|
|
VESTABOARD_PUBISH_BODY="{\"text\":\"$MESSAGE\"}" # properly escape JSON |
|
echo "Sending the following message to publish:" |
|
echo $VESTABOARD_PUBISH_BODY | jq |
|
echo |
|
|
|
VESTABOARD_PUBLISH_RESPONSE=$(vestboard_api_call_post "/subscriptions/$SUBSCRIPTION_ID/message" "$VESTABOARD_PUBISH_BODY") |
|
|
|
echo "Vestaboard API response was:" |
|
echo $VESTABOARD_PUBLISH_RESPONSE | jq |
|
echo |
|
} |
|
|
|
#################### |
|
# Script execution # |
|
#################### |
|
|
|
viewer_info |
|
|
|
# Get a list of subscriptions |
|
echo "== Vestboard Subscription ID == " |
|
VESTABOARD_SUBSCRIPTIONS=$(vestboard_api_call_get "/subscriptions") |
|
|
|
# Pick out the first subscription and echo it out |
|
echo "(While the API supports multiple subscriptions, this script simply selects the first)" |
|
VESTABOARD_FIRST_SUBSCRIPTION_ID=$(echo $VESTABOARD_SUBSCRIPTIONS | jq -r '.subscriptions[0]._id') |
|
echo \"$VESTABOARD_FIRST_SUBSCRIPTION_ID\" | jq |
|
echo |
|
|
|
publish_message "$VESTABOARD_FIRST_SUBSCRIPTION_ID" "$VESTABOARD_TEXT_TO_SEND" |