Last active
September 15, 2021 20:30
-
-
Save jrwarwick/3b06f2ed3711c801e9d47a33334c31eb to your computer and use it in GitHub Desktop.
Send a notification Message Card to a MS Teams channel chat from a UNIX/LINUX script
This file contains hidden or 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
#!/usr/bin/bash | |
# Send a notification to a MS Teams channel's webhook interface. | |
# Note: originally crafted in a Solaris 11 env. | |
# usage: im_notify.sh --envcontext TEST --infocontext "ERP System Patching" --infotask "$0 - Apply vendor patch #1" --infostatus "FAILURE" --message "Patching script encountered error! Attention required." | |
# usage: im_notify.sh --envcontext DEV --infocontext "Long Process Progress" --infotask "$0 script run" --infostatus "SUCCESS" --message "OK, step complete, ready for next step." | |
# [email protected] 2021 | |
# TODO: parameter to indicate CC message to syslog as well (with local logger) | |
#Standard Config | |
SupportURL="https://support.contoso.com/newIssue" | |
#Parameter Defaults | |
envcontext=${envcontext:-DEV} | |
infocontext=${infocontext:-Unattended Modification Activity} | |
infotask=${infotask:-Batch Patching script run} | |
infostatus=${infostatus:-FAILURE} | |
message=${message:-Operator attention recommended} | |
#Process Parameter User Spec | |
while [ $# -gt 0 ]; do | |
if [[ $1 == *"--"* ]]; then | |
param="${1/--/}" | |
declare $param="$2" | |
# echo $1 $2 // Optional to see the parameter:value result | |
fi | |
shift | |
done | |
#TODO: read from stdin and append to $message | |
#Weirdly, solaris std egrep doesn't support -q, so ... | |
if ( which /usr/xpg4/bin/egrep >/dev/null 2>&1 ) ; then | |
GREPTOOL=/usr/xpg4/bin/egrep | |
else | |
GREPTOOL=$(which egrep | tail -1) | |
fi | |
#TODO: differentiate warning from BAD/FAIL in pseudo-"sentiment analysis" to change theme color? | |
#Default: uncertain-blue | |
SentimentColor="2233BB" | |
if ( echo $infostatus | $GREPTOOL -iq "FAIL|ERROR|CATASTROPH|ABEND|ABNORMAL|BAD|WARN" ) ; then | |
#warning-orange | |
SentimentColor="FFA500" | |
elif ( echo $infostatus | $GREPTOOL -iq "SUCCESS|SUCCEED|GOOD|NORMAL|NOMINAL" ) ; then | |
#good-green | |
SentimentColor="22AA33" | |
fi | |
#TODO: any filtration here or conditional env inspection/collection | |
MessageBody="${message}" | |
if [ $ORACLE_SID ] ; then | |
$MessageBody = "${ORACLE_SID}_${HOSTNAME} ${MessageBody}" | |
fi | |
MainTitle="${envcontext} ${infocontext} ${infostatus}" | |
SubTitle="${infotask}" | |
#TODO, turn this into a lookup: friendly name to webhook url | |
# 'TenantName.Team_Name.General_ChannelName.HookName' spaces to underscore, dot-delimited | |
WebhookURL="https://tenantname.webhook.office.com/webhookb2/123412341234/IncomingWebhook/456745674567" | |
#A payload defined by MS https://docs.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/what-are-cards | |
#Note that around 2020 or 2021 even though they recommend the revamped actioncards 2.0 or whatever, some prime products | |
#do NOT yet support the newer standards! | |
##For troubleshooting json payload, comment out curl command and uncomment the cat | |
##cat <<EOF | |
curl --insecure -k --location --request POST $WebhookURL --header 'Content-Type: application/json' -d @- <<EOF | |
{ | |
"@type": "MessageCard", | |
"@context": "http://schema.org/extensions", | |
"summary": "$MainTitle", | |
"themeColor": "$SentimentColor", | |
"sections":[{ | |
"startGroup": true, | |
"activityTitle": "$MainTitle", | |
"activitySubtitle": "$SubTitle", | |
"facts": [ | |
{ | |
"name": "Hostname", | |
"value": "$HOSTNAME" | |
},{ | |
"name": "Message", | |
"value": "$MessageBody" | |
},{ | |
"name": "Process ID", | |
"value": "$PPID" | |
} | |
], | |
"potentialAction": [{ | |
"@type": "OpenUri", | |
"name": "Get Support", | |
"targets": [ { "os": "default", "uri": "$SupportURL" } ] | |
}] | |
}] | |
} | |
EOF | |
#TODO: some kind of basic error handling for a failed curl call. Maybe possibly a retry or a simplified text version. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment