Skip to content

Instantly share code, notes, and snippets.

@jzlima
Forked from chusiang/teams-chat-post.sh
Last active March 31, 2020 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jzlima/4d248cf0a7e5726ac125b32f8c165170 to your computer and use it in GitHub Desktop.
Save jzlima/4d248cf0a7e5726ac125b32f8c165170 to your computer and use it in GitHub Desktop.
Post a message to Microsoft Teams with bash script.
#!/bin/sh
# =============================================================================
# Author: Chu-Siang Lai / chusiang (at) drx.tw
# Filename: teams-chat-post.sh
# Modified: 2018-03-28 15:04
# Description: Post a message to Microsoft Teams.
# Reference:
#
# - https://gist.github.com/chusiang/895f6406fbf9285c58ad0a3ace13d025
#
# =============================================================================
# Help.
if [ "$1" = "-h" ] || [ "$1" = "--help" ]
then
echo 'Usage: teams-chat-post.sh "<webhook_url>" "<title>" "<color>" "<message>"'
exit 0
fi
# Webhook or Token.
WEBHOOK_URL=$1
if [ "${WEBHOOK_URL}" = "" ]
then
echo "No webhook_url specified."
exit 1
fi
shift
# Title .
TITLE=$1
if [ "${TITLE}" = "" ]
then
echo "No title specified."
exit 1
fi
shift
# Color.
COLOR=$1
if [ "${COLOR}" = "" ]
then
echo "No status specified."
exit 1
fi
shift
# Text.
TEXT=$*
if [ "${TEXT}" = "" ]
then
echo "No text specified."
exit 1
fi
# Convert formating.
#MESSAGE=$(echo ${TEXT} | sed 's/"/\"/g' | sed "s/'/\'/g")
MESSAGE=` echo $TEXT`
JSON="{\"title\": \"${TITLE}\", \"themeColor\": \"${COLOR}\", \"text\": \"${MESSAGE}\", \"ReplyTo\":\"MessageID\"}"
# Post to Microsoft Teams.
/opt/SUNWexplo/bin/solaris/curl.sparc -k -H "Content-Type: application/json" -d "${JSON}" "${WEBHOOK_URL}"
#!/bin/sh
# =============================================================================
# Author: Chu-Siang Lai / chusiang (at) drx.tw
# Filename: teams-chat-post.sh
# Modified: 2018-03-28 15:04
# Description: Post a message to Microsoft Teams.
# Reference:
#
# - https://gist.github.com/chusiang/895f6406fbf9285c58ad0a3ace13d025
#
# =============================================================================
# Help.
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
echo 'Usage: teams-chat-post.sh "<webhook_url>" "<title>" "<color>" "<message>"'
exit 0
fi
# Webhook or Token.
WEBHOOK_URL=$1
if [[ "${WEBHOOK_URL}" == "" ]]
then
echo "No webhook_url specified."
exit 1
fi
shift
# Title .
TITLE=$1
if [[ "${TITLE}" == "" ]]
then
echo "No title specified."
exit 1
fi
shift
# Color.
COLOR=$1
if [[ "${COLOR}" == "" ]]
then
echo "No status specified."
exit 1
fi
shift
# Text.
TEXT=$*
if [[ "${TEXT}" == "" ]]
then
echo "No text specified."
exit 1
fi
# Convert formating.
MESSAGE=$( echo ${TEXT} | sed 's/"/\"/g' | sed "s/'/\'/g" )
JSON="{\"title\": \"${TITLE}\", \"themeColor\": \"${COLOR}\", \"text\": \"${MESSAGE}\" }"
# 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