Initial commit
This commit is contained in:
parent
88be1d7f6c
commit
296ed2b148
101
bin/rocket-send
Executable file
101
bin/rocket-send
Executable file
@ -0,0 +1,101 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eo pipefail
|
||||
|
||||
RC_HOST_URL=https://rocket.cadoles.com
|
||||
RC_CONFIG_DIR="$HOME/.config/rocket-send"
|
||||
RC_EMOJI=":cadoles:"
|
||||
RC_LOGIN=
|
||||
RC_PASSWORD=
|
||||
RC_CLIENT_ID=
|
||||
RC_CLIENT_TOKEN=
|
||||
RC_MESSAGE=
|
||||
RC_CHANNEL=
|
||||
|
||||
function help {
|
||||
cat<<EOF 1>&2
|
||||
Envoie un message sur un canal Rocket.Chat
|
||||
|
||||
Usage: rocket-send -c [CHANNEL] -m [MESSAGE]
|
||||
|
||||
Options:
|
||||
-c, --channel [Obligatoire] Nom du canal sur lequel envoyer le message
|
||||
-m, --message [Obligatoire] Message à envoyer
|
||||
-s, --server URL racine du serveur Rocket.Chat
|
||||
-e, --emoji Emoji à utiliser comme avatar pour le message
|
||||
-n, --nickname Pseudonyme à utiliser pour le message
|
||||
--config-dir Chemin vers le répertoire de configuration, par défaut $HOME/.config/rocket-send
|
||||
-h, --help Afficher l'aide
|
||||
EOF
|
||||
}
|
||||
|
||||
function get_options {
|
||||
while [[ "$#" -gt 0 ]]
|
||||
do
|
||||
case $1 in
|
||||
-s|--server)
|
||||
RC_HOST_URL="$2"
|
||||
;;
|
||||
-e|--emoji)
|
||||
RC_EMOJI="$2"
|
||||
;;
|
||||
--config-dir)
|
||||
RC_CONFIG_DIR="$2"
|
||||
;;
|
||||
-m|--message)
|
||||
RC_MESSAGE="$2"
|
||||
;;
|
||||
-c|--channel)
|
||||
RC_CHANNEL="$2"
|
||||
;;
|
||||
-h|--help)
|
||||
help
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
shift
|
||||
done
|
||||
}
|
||||
|
||||
function get_config {
|
||||
mkdir -p "$RC_CONFIG_DIR"
|
||||
if [ -e "$RC_CONFIG_DIR/config" ]; then
|
||||
source "$RC_CONFIG_DIR/config"
|
||||
fi
|
||||
}
|
||||
|
||||
function login {
|
||||
local login=$1
|
||||
local password=$2
|
||||
|
||||
local res=$(curl --silent \
|
||||
-H "Content-Type:application/json" \
|
||||
"$RC_HOST_URL/api/v1/login" \
|
||||
-d "{ \"user\": \"$login\", \"password\": \"$password\" }" \
|
||||
)
|
||||
|
||||
RC_CLIENT_ID=$(echo $res | jq -r '.data.userId')
|
||||
RC_CLIENT_TOKEN=$(echo $res | jq -r '.data.authToken')
|
||||
}
|
||||
|
||||
function post_message {
|
||||
local channel=$1
|
||||
local message=$2
|
||||
|
||||
curl --silent \
|
||||
-H "X-Auth-Token: $RC_CLIENT_TOKEN" \
|
||||
-H "X-User-Id: $RC_CLIENT_ID" \
|
||||
-H "Content-Type:application/json" \
|
||||
"$RC_HOST_URL/api/v1/chat.postMessage" \
|
||||
-d "{ \"channel\": \"$channel\", \"text\": \"$message\" }"
|
||||
}
|
||||
|
||||
function main {
|
||||
get_config
|
||||
get_options $@
|
||||
|
||||
login "$RC_LOGIN" "$RC_PASSWORD"
|
||||
post_message "$RC_CHANNEL" "$RC_MESSAGE"
|
||||
}
|
||||
|
||||
main $@
|
Loading…
Reference in New Issue
Block a user