Skip to content

Instantly share code, notes, and snippets.

@Amar1729
Created February 23, 2019 00:29
Show Gist options
  • Save Amar1729/81833ec69c9030e8faa014a01d05862c to your computer and use it in GitHub Desktop.
Save Amar1729/81833ec69c9030e8faa014a01d05862c to your computer and use it in GitHub Desktop.
Batch upload icon links as emojis to RocketChat.
#!/usr/bin/env bash
# Author: Amar Paul
# Add custom emojis to a rocketchat server.
# Usage:
# Expects a yaml/ dir with .yaml files, formatted as such:
# - name: <name_of_emoji>
# src: <src_url>
#
# Script will read yaml files, download each src from URL
# into imgs/ directory, then upload to rocketchat using the
# rocketchat emoji-custom.create endpoint.
#
# Full usage:
# <script> --user USER --url ROCKETCHAT_URL [action]
# actions:
# --download-yamls <file>
# dl all the .yaml files listed in <file> into yamls/
# (expects file to be at least one line of YAML URLs)
# --logout
# --list
# --upload (default)
# expects yaml files in the yamls/ dir
#
# Note:
# This script isn't smart about what's already on the server,
# so uploading an emoji with a name that already exists will
# return a {success: false} json from the server and continue.
#
# It also doesn't know if an image has already been downloaded.
#
# Depends on `jq' for some JSON parsing.
#
# Some yaml-formatted emoji packs:
# https://github.com/amtypaldos/rocketchat-emoji-upload
ESC="$(printf '\033')"
GRN="${ESC}[31;1m"
RED="${ESC}[32;1m"
NOC="${ESC}[30;0m"
USER=""
url=""
retfile=login-return.json
get_uid () { cat $retfile | jq -r '.data | .userId' ;}
get_auth () { cat $retfile | jq -r '.data | .authToken' ;}
_login () {
echo -n "Password for <$USER>: "
read -s password
# bit of a hack to get around logging in w/ ldap enabled
curl -H "Content-type:application/json" ${url}/login \
-d "{\"ldap\": true, \"username\": \"${USER}\", \"ldapPass\": \"${password}\", \"ldapOptions\":{} }" > $retfile
#-d "{\"ldap\": true, \"username\": \"${USER}\"}" > $retfile
}
_logout () {
curl "${url}/logout" \
-H "X-Auth-Token: $(get_auth)" \
-H "X-User-Id: $(get_uid)"
[[ $? -eq 0 ]] && rm -f $retfile
}
list_emojis () {
curl "${url}/emoji-custom" \
-H "X-Auth-Token: $(get_auth)" \
-H "X-User-Id: $(get_uid)"
}
get_file () {
(pushd imgs/ && wget $1 && popd) &>/dev/null
echo "imgs/${1##*/}"
}
upload_emoji () {
printf "name=$1, "
printf "emoji=@$2"
printf "\n"
curl "${url}/emoji-custom.create" \
-H "Content-Type: multipart/form-data" \
-H "X-Auth-Token: $(get_auth)" \
-H "X-User-Id: $(get_uid)" \
-F "emoji=@$2" -F "name=$1"
printf "\n\n"
}
grab_files () {
local name=""
local src=""
if [[ $(uname) == "Darwin" ]]; then
alias grep=ggrep
fi
for ym in yamls/*yaml; do
while read -r line || [[ -n "$line" ]]; do
if echo $line | grep -o "^[^\W]*- name: " > /dev/null; then
name="${line#*- name: }"
fi
if echo $line | grep -o "^[^\W]*src: " > /dev/null; then
src="${line#*src: }"
echo "${GRN}Downloading: ${NOC}$src"
src="$(get_file $src)"
fi
if [[ -n "$name" ]] && [[ -n "$src" ]]; then
printf "${RED}Uploading : ${NOC}"
upload_emoji $name $src
name=""
src=""
fi
done < $ym
done
}
dl_yamls () {
[[ ! -e $1 ]] && echo "File: $1 doesn't exist." && exit 1
[[ ! -d yamls/ ]] && mkdir -p yamls
pushd yamls/ >/dev/null
while read -r line || [[ -n "$line" ]]; do
wget $line
done < ../$1
popd >/dev/null
}
#
#
#
# default action
action=grab_files
while [[ $# -gt 0 ]]; do
case $1 in
--user)
USER=$2
shift; shift
;;
--url)
url=${2%/}
shift; shift
;;
--logout)
action=_logout
shift
;;
--list)
action=list_emojis
shift
;;
--download-yamls)
dl_yamls $2
exit 0
;;
*)
shift
;;
esac
done
[[ -z $USER ]] && echo "Required: --user" && exit 1
[[ -z $url ]] && echo "Required: --url" && exit 1
[[ $action != "_logout" ]] && [[ ! -e $retfile ]] && _login
[[ ! -d imgs/ ]] && mkdir -p imgs
[[ ! -d yamls/ ]] && echo "Required directory: yamls/" && exit 1
$action
@Amar1729
Copy link
Author

Amar1729 commented Sep 9, 2020

Also, see the update version for mattermost: https://gist.github.com/Amar1729/e0d1018a88d2a7a2fe1f6e9fd998ceaf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment