Skip to content

Instantly share code, notes, and snippets.

@JimHume
Last active April 16, 2019 17:30
Show Gist options
  • Save JimHume/36022e23f5744395e5baa2f0f3f94922 to your computer and use it in GitHub Desktop.
Save JimHume/36022e23f5744395e5baa2f0f3f94922 to your computer and use it in GitHub Desktop.
motionEye OS Slack Upload script.
#!/bin/bash
################################################################################
# Constants
# TOKEN: Taken from an API bot
TOKEN='letters-numbers-numbers-numbersandletters'
# CHANNEL: When viewing the desired channel in a browser, this will be the last
# segment of the URL
CHANNEL='numbersandletters'
# UPLOAD_URL: Should remain as-is
UPLOAD_URL='https://slack.com/api/files.upload'
################################################################################
# Functions
# Upload the given file name (param) to slack
upload_file() {
# Find the newest file with the given extension
FILE_EXT=$1
FILE_NAME=$(find /data/output -iname $FILE_EXT -type f -print | sort -n | tail -1 | cut -f2- -d" ")
# Set the title to be the raw file name (not the full path)
TITLE=$(awk -F "/" '{print $NF}' <<< "$FILE_NAME")
# Set up the command before hand so that we can echo it if desired
COMMAND_TEXT="-F file=@$FILE_NAME -F channels=$CHANNEL -F token=$TOKEN -F title=$TITLE $UPLOAD_URL"
# Get the JSON response from the curl command.
# Slack will provide a field named 'ok' that contains a true/false variable indicating success
RESPONSE=$(curl $COMMAND_TEXT | jq '.ok')
# This should be done as a true bool instead of string comparison
if [ "$RESPONSE" = "true" ]; then
echo "Success"
else
echo "Failed to upload"
fi
}
################################################################################
# Main
# Upload the snapshot pic first
upload_file "*.jpg"
# Upload the video after waiting a bit for it to complete recording
sleep 90
upload_file "*.mp4"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment