Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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}"
@brenoplucena

This comment has been minimized.

Copy link

@brenoplucena brenoplucena commented Oct 7, 2019

Hi @chusiang , thanks for your post.

@chusiang

This comment has been minimized.

Copy link
Owner Author

@chusiang chusiang commented Oct 7, 2019

Hi @brenoplucena , You're welcome . :-)

@kucerarichard

This comment has been minimized.

Copy link

@kucerarichard kucerarichard commented Oct 10, 2019

thanks!
#!/bin/bash
I don't know how to do a PR with a gist...

@danielporto

This comment has been minimized.

Copy link

@danielporto danielporto commented Oct 22, 2019

I tried now and worked.

@HassanSE

This comment has been minimized.

Copy link

@HassanSE HassanSE commented Oct 24, 2019

I'm getting:

'Object move to' html

Any clue?

@focbenz

This comment has been minimized.

Copy link

@focbenz focbenz commented Nov 26, 2019

Let's try this @tfellner

@cdivitotawela

This comment has been minimized.

Copy link

@cdivitotawela cdivitotawela commented Dec 9, 2019

Working for me. However no idea what should be the values for colours.

@focbenz

This comment has been minimized.

Copy link

@focbenz focbenz commented Dec 9, 2019

Use RGB hex color values like e.g. "ff0000" for red.

@jzlima

This comment has been minimized.

Copy link

@jzlima jzlima commented Mar 20, 2020

Is it possible to change background color??

@atc0005

This comment has been minimized.

Copy link

@atc0005 atc0005 commented Apr 7, 2020

@jzlima it possible to change background color??

Using the webhook API, as far as I know you can only change the left trim color, not the background of the message itself.

See https://docs.microsoft.com/en-us/outlook/actionable-messages/message-card-reference for more info.

@eviksantika

This comment has been minimized.

Copy link

@eviksantika eviksantika commented Apr 29, 2020

hi @chusiang , i got problem
curl: (3) malformed
any idea?

@MRinus

This comment has been minimized.

Copy link

@MRinus MRinus commented Jun 16, 2020

Thanks a lot! The format conversion part rescued me :)

@chusiang

This comment has been minimized.

Copy link
Owner Author

@chusiang chusiang commented Jun 17, 2020

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

2020_6_17_zabbix_teams_alert

Enjoy it.

@KeithBierman

This comment has been minimized.

Copy link

@KeithBierman KeithBierman commented Aug 11, 2020

Nice script. On ubuntu 18, I found I needed to change it to /bin/bash (sh is linked to dash) and dos2unix the file (although that might have been a side effect of how I got the files to the target system). dash doesn't recognize [[. Is there some compelling reason to specify sh (and then use a non-sh syntax?) "[[" is superior, but we should specify one of the shells that definitely supports it (ksh, bash, zsh ... in order I recall their being implemented ... but my memory could be wrong).

@rajnish6191

This comment has been minimized.

Copy link

@rajnish6191 rajnish6191 commented Dec 14, 2020

Hi @chu-siang Lai,
Could you please advise how to tag using curl command? I would like to send a message as well as wanted to tag a particular team.
Thanks

@nto4

This comment has been minimized.

Copy link

@nto4 nto4 commented Dec 14, 2020

@rajnish6191 i guess u cant, but u can create a team for this message and add users this team and every body open notifications for this team.
Example
U can create web hook in teams tab in any team click ... And create web hook
curl -H 'Content-Type: application/json' -d '{​​"text": "Hello World"}​​'

@shadabacc3934

This comment has been minimized.

Copy link

@shadabacc3934 shadabacc3934 commented Dec 15, 2020

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

This comment has been minimized.

Copy link

@focbenz 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

This comment has been minimized.

Copy link

@zarruler zarruler commented Jan 11, 2021

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

@digaoddc

This comment has been minimized.

Copy link

@digaoddc digaoddc commented Apr 28, 2021

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

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

This comment has been minimized.

Copy link

@zarruler zarruler commented Apr 28, 2021

Thank You Thank You

@nmaupu

This comment has been minimized.

Copy link

@nmaupu nmaupu commented Aug 24, 2021

Thanks but this shell script contains bashism :/

@Carmelly212

This comment has been minimized.

Copy link

@Carmelly212 Carmelly212 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
@nmaupu

This comment has been minimized.

Copy link

@nmaupu 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

This comment has been minimized.

Copy link

@jacekgajek jacekgajek commented Oct 20, 2021

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

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