Last active
December 6, 2023 12:28
-
-
Save nurandi/baa5c18a006b26e58b3049d83b52d679 to your computer and use it in GitHub Desktop.
V2 - Send job notification in bash to Teams channel using incoming webhook. jq needed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# version 2.0 | |
# {jq} needed | |
# | |
# Make sure to add teams incoming webhook URL to global variable : | |
# export WEBHOOK_URL={your webhook URL} | |
# Help. | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
echo 'Usage: sh send_teams.sh --title= --description= --status={OK|WARNING|NOK} --summary= --pic= --email=' | |
exit 0 | |
fi | |
while [ $# -gt 0 ]; do | |
case "$1" in | |
-t=* | --title=*) | |
title="${1#*=}" | |
;; | |
-d=* | --description=*) | |
description="${1#*=}" | |
;; | |
-s=* | --status=*) | |
status="${1#*=}" | |
;; | |
-u=* | --summary=*) | |
summary="${1#*=}" | |
;; | |
-p=* | --pic=*) | |
pic="${1#*=}" | |
;; | |
-e=* | --email=*) | |
email="${1#*=}" | |
;; | |
*) | |
printf "Error: unknown option: $1\n" | |
exit 1 | |
esac | |
shift | |
done | |
if [ "$status" = "OK" ]; then | |
style="good" | |
elif [ "$status" = "NOK" ]; then | |
style="attention" | |
elif [ "$status" = "WARNING" ]; then | |
style="warning" | |
fi | |
[ -z "$email" ] && email="noemail" | |
# json template | |
read -r -d '' JSON <<- EOM | |
{ | |
"type": "message", | |
"attachments": [ | |
{ | |
"contentType": "application/vnd.microsoft.card.adaptive", | |
"content": { | |
"type": "AdaptiveCard", | |
"body": [ | |
{ | |
"type": "Container", | |
"items": [ | |
{ | |
"type": "TextBlock", | |
"text": "$title", | |
"wrap": true, | |
"size": "large", | |
"weight": "bolder" | |
}, | |
{ | |
"type": "TextBlock", | |
"text": "$description", | |
"wrap": true | |
}, | |
{ | |
"type": "FactSet", | |
"facts": [ | |
{ | |
"title": "Status", | |
"value": "$status" | |
}, | |
{ | |
"title": "PIC", | |
"value": "<at>$pic</at>" | |
} | |
], | |
"spacing": "Small" | |
} | |
], | |
"style": "$style" | |
} | |
], | |
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json", | |
"version": "1.0", | |
"msteams": { | |
"entities": [ | |
{ | |
"type": "mention", | |
"text": "<at>$pic</at>", | |
"mentioned": { | |
"id": "$email", | |
"name": "$pic" | |
} | |
} | |
], | |
"width": "Full" | |
} | |
} | |
}] | |
} | |
EOM | |
# modify JSON if summary is not blank | |
# summary should written using this format: "label1=value1|label2=value2| ... |labelN=valueN" | |
# ex: summary="Periode=202301|Updated=2023-01-01|Records=234" | |
if [ -n "$summary" ]; then | |
summary=$(echo "$summary" | tr '|' '\n' | tac | tr '\n' '|'); summary=${summary%|} | |
IFS='|' read -ra elements <<< "$summary" | |
for element in "${elements[@]}"; do | |
IFS== read -r key value <<< "$element" | |
JSON=$(echo "$JSON" | jq --arg key "$key" --arg value "$value" '.attachments[0].content.body[0].items[2].facts |= .[:1] + [{ "title": $key, "value": $value }] + .[1:]') | |
done | |
IFS=" " | |
fi | |
#echo "$JSON" | jq . | |
# Post to Microsoft Teams. | |
curl -H "Content-Type: application/json" -d "${JSON}" "${WEBHOOK_URL}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment