Last active
November 22, 2023 14:02
-
-
Save nurandi/29522eee330921414078ae0d03b8303f to your computer and use it in GitHub Desktop.
Send job notification in bash to Teams channel using incoming webhook
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 | |
# (c) @nurandi | |
# Make sure to add teams incoming webhook URL to ~/.bashrc : | |
# export WEBHOOK_URL={your webhook URL} | |
source ~/.bashrc | |
# Help. | |
if [[ "$1" == "-h" || "$1" == "--help" ]]; then | |
echo 'Usage: sh send_teams.sh --title= --description= --status={OK|WARNING|NOK} --updated= --period= --records= --elapsed= --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=* | --updated=*) | |
updated="${1#*=}" | |
;; | |
-p=* | --period=*) | |
period="${1#*=}" | |
;; | |
-r=* | --records=*) | |
records="${1#*=}" | |
;; | |
-et=* | --elapsed=*) | |
elapsed="${1#*=}" | |
;; | |
-pic=* | --pic=*) | |
pic="${1#*=}" | |
;; | |
-e=* | --email=*) | |
email="${1#*=}" | |
;; | |
*) | |
printf "Error: unknown option: $1\n" | |
exit 1 | |
esac | |
shift | |
done | |
[ -z "$email" ] && email="noemail" | |
if [ "$status" = "OK" ]; then | |
style="good" | |
elif [ "$status" = "NOK" ]; then | |
style="attention" | |
elif [ "$status" = "WARNING" ]; then | |
style="warning" | |
fi | |
# 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": "Updated date", | |
"value": "$updated" | |
}, | |
{ | |
"title": "Period", | |
"value": "$period" | |
}, | |
{ | |
"title": "Total records", | |
"value": "$records" | |
}, | |
{ | |
"title": "Elapsed time", | |
"value": "$elapsed" | |
}, | |
{ | |
"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 | |
# 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