Skip to content

Instantly share code, notes, and snippets.

@chusiang
Last active March 21, 2024 10:55
Show Gist options
  • Star 66 You must be signed in to star a gist
  • Fork 29 You must be signed in to fork a gist
  • Save chusiang/895f6406fbf9285c58ad0a3ace13d025 to your computer and use it in GitHub Desktop.
Save chusiang/895f6406fbf9285c58ad0a3ace13d025 to your computer and use it in GitHub Desktop.
Post a message to Microsoft Teams with bash script.
#!/bin/bash
# =============================================================================
# Author: Chu-Siang Lai / chusiang (at) drx.tw
# Filename: teams-chat-post.sh
# Modified: 2021-10-18 00:09
# 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}"
@shadabacc3934
Copy link

Hi,
Thanks for the script.
Could you please help me to understand the WEBHOOK URL, how we can create it? alos request you to please share sample way to run the script

@focbenz
Copy link

focbenz commented Dec 15, 2020

@shadabacc3934 Setting up a custom incoming webhook

  • In Microsoft Teams, choose More options (⋯) next to the channel name and then choose Connectors.
  • Scroll through the list of Connectors to Incoming Webhook, and choose Add.
  • Enter a name for the webhook, upload an image to associate with data from the webhook, and choose Create.
  • Copy the webhook to the clipboard and save it. You'll need the webhook URL for sending information to Microsoft Teams.
  • Choose Done.

Source: https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using

@zarruler
Copy link

Thank you sir.... This just made my life so much easier...

@digaoddc
Copy link

If you want to use a multiline message, you can use the pre tag, like this:

JSON="{\"text\": \"<pre>${MESSAGE}<\/pre>\" }"

@zarruler
Copy link

Thank You Thank You

@nmaupu
Copy link

nmaupu commented Aug 24, 2021

Thanks but this shell script contains bashism :/

@Carmelly212
Copy link

Hi, thanks for the script...
am i doing somthing wrong ? I get:

./teams.sh: 14: ./teams.sh: [[: not found
./teams.sh: 14: ./teams.sh: -h: not found
./teams.sh: 21: ./teams.sh: [[: not found
./teams.sh: 30: ./teams.sh: [[: not found
./teams.sh: 35: shift: can't shift that many

@nmaupu
Copy link

nmaupu commented Oct 17, 2021

Hi, thanks for the script... am i doing somthing wrong ? I get:

./teams.sh: 14: ./teams.sh: [[: not found
./teams.sh: 14: ./teams.sh: -h: not found
./teams.sh: 21: ./teams.sh: [[: not found
./teams.sh: 30: ./teams.sh: [[: not found
./teams.sh: 35: shift: can't shift that many

This script indicates /bin/sh but is full of bashism...
You probably should execute it using /bin/bash

@jacekgajek
Copy link

Is there a way to send a message to a group chat, not Teams?

@nfourniol
Copy link

nfourniol commented Jun 16, 2022

MESSAGE=$( echo ${TEXT} | sed 's/"/\"/g' | sed "s/'/\'/g" )
should be rewritten:
MESSAGE=$( echo ${TEXT} | sed 's/"/\\"/g')

No need to escape simple quote as you don't use it in your JSON fields representation.
And the most important thing: you should escape the baskslash before " otherwise after the $MESSAGE evaluation in your JSON variable you'll have something like below. Imagine your TEXT received is => The content of my "message".
After you've escaped the double quote (but not the backslash), your $TEXT variable will be evaluated as The content of my "message" causes a problem.
JSON="{\"title\": \"your title\", \"themeColor\": \"TheColorCode\", \"text\": \"The content of my \"message\" causes a problem.\" }"

And as you can see the value of your text field is cut at its middle.

And last thing: no need to declare a new variable MESSAGE, you can juste have:
TEXT=$(echo ${TEXT} | sed 's/"/\\"/g' | sed "s/'/\'/g")

@ciroiriarte
Copy link

Hi all, the Zabbix v5.0 has build-in the webhook of Microsoft Teams.

2020_6_17_zabbix_teams_alert

Enjoy it.

how do you achieve that formatting?, tabs/alignment for example

@atc0005
Copy link

atc0005 commented Oct 6, 2022

@ciroiriarte

The Event time, Severity and Operational data fields are "Facts", name/value pairs that are rendered in a table format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment