-
-
Save chusiang/895f6406fbf9285c58ad0a3ace13d025 to your computer and use it in GitHub Desktop.
#!/bin/bash | |
# ============================================================================= | |
# Author: Chu-Siang Lai / chusiang (at) drx.tw | |
# Filename: teams-chat-post-for-workflows.sh | |
# Modified: 2024-07-22 11:44 (UTC+08:00) | |
# Description: Post a message to Microsoft Teams via "Post to a chat when a webhook request is received" workflows. | |
# Reference: | |
# | |
# - https://gist.github.com/chusiang/895f6406fbf9285c58ad0a3ace13d025 | |
# - https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/ | |
# - https://adaptivecards.io/explorer/ | |
# - https://adaptivecards.io/designer/ | |
# | |
# ============================================================================= | |
# 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 | |
ATTACHMENTS=$* | |
if [[ "${TEXT}" == "" ]]; then | |
echo "No text specified." | |
exit 1 | |
fi | |
# Escape char: `'`, `"`, `\` . | |
MESSAGE=$(echo ${TEXT} | sed "s/'/\'/g" | sed 's/"/\"/g; s/\\/\\\\/g') | |
# Adaptive Cards of TextBlock - https://adaptivecards.io/explorer/TextBlock.html | |
JSON="{ | |
\"type\": \"message\", | |
\"attachments\": [ | |
{ | |
\"contentType\": \"application/vnd.microsoft.card.adaptive\", | |
\"contentUrl\": null, | |
\"content\": { | |
\"$schema\": \"http://adaptivecards.io/schemas/adaptive-card.json\", | |
\"type\": \"AdaptiveCard\", | |
\"version\": \"1.2\", | |
\"body\": [ | |
{ | |
\"type\": \"TextBlock\", | |
\"text\": \"${TITLE}\", | |
\"color\": \"${COLOR}\", | |
\"weight\": \"bolder\", | |
\"size\": \"large\", | |
\"wrap\": true | |
}, | |
{ | |
\"type\": \"TextBlock\", | |
\"text\": \"${MESSAGE}\", | |
\"color\": \"${COLOR}\", | |
\"size\": \"small\", | |
\"wrap\": true | |
} | |
] | |
} | |
} | |
] | |
}" | |
# Post to Microsoft Teams via curl. | |
curl \ | |
--header "Content-Type: application/json" \ | |
--request POST \ | |
--data "${JSON}" \ | |
"${WEBHOOK_URL}" |
#!/bin/bash | |
# ============================================================================= | |
# Author: Chu-Siang Lai / chusiang (at) drx.tw | |
# Filename: teams-chat-post.sh | |
# Modified: 2024-07-12 18:49 (UTC+08:00) | |
# Description: Post a message to Microsoft Teams via connectors, not support | |
# Power Automate workflows. | |
# Reference: | |
# | |
# - https://gist.github.com/chusiang/895f6406fbf9285c58ad0a3ace13d025 | |
# - https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams/ | |
# - Fixed for workflows edition: https://gist.github.com/chusiang/895f6406fbf9285c58ad0a3ace13d025?permalink_comment_id=5119162#gistcomment-5119162 | |
# | |
# ============================================================================= | |
# 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}" | |
@whanklee You can add \"color\": \"<options>\"
are:
Default
Dark
Light
Accent
Good
Warning
Attention
@q2uantum Thank you for your share, I can send message to Chat via Workflows now, but I have no permission to send to Channel, so I can't verify it now.
I have put the script of teams-chat-post-for-workflows.sh .
[ chusiang@ubuntu-22.04 ~]
$ ./teams-chat-post-for-workflows.sh "https://prod-XXX.weXXXX.logic.azure.com:443/workflows/1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXf/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=BXXXXXXXXXF-fXXXXXXXXXXXXXXXXXXXXXXXXXXXXX4" "Workflows" "Default" "Send from XXXXX-ubuntu."
@chusiang You have a typo in the script :
{ "type": "TextBlock", "text": "${MESSAGE}", "warp": true } should be
{ "type": "TextBlock", "text": "${MESSAGE}", "wrap": true }
@ronnyheymans Thank you for feedback, I have fixed it.
I have leaved a comment to share this new script on Retirement of Office 365 connectors within Microsoft Teams | Microsoft Developer Blogs post, maybe it can help someone.
@chusiang thanks for your post, any idea how to get rid of the footnote message "... used a Workflow template to send this card. Get template" and to change the Workflow logo ?
Good news, I can send notification with teams-chat-post-for-workflows.sh to channel via workflows !
I guess, it's working now, because I am the owner role in this Teams.
Thanks chusiang , is it possible remove the user name from message ?
Hi @wengzy , I think you need the title like "chusiang via Workflows", I can not see some options for it now.
I'm also successfully using your script to post, but my backend monitoring tool has checks against Windows drive pathing. When those checks pass their data to ${TITLE} or ${MESSAGE} it contains a \ in the drive path. This is breaking the JSON with a "Bad JSON escape sequence: \ . Path 'attachments[0].content.body[1].text" error. I've tried a couple of things to correct this issue, but haven't been able to get around it. Any thoughts?
Hi @lp-maximus,
I think we need add Escape Character for \
on L60 in teams-chat-post-for-workflows.sh.
I have also escape the \
char, maybe it can help you.
- MESSAGE=$(echo ${TEXT} | sed 's/"/\"/g' | sed "s/'/\'/g")
+ MESSAGE=$(echo ${TEXT} | sed "s/'/\'/g" | sed 's/"/\"/g; s/\\/\\\\/g')
Good luck.
Reference:
Yes, it works.
What about "themeColor": "${COLOR}"? I tried adding this parameter, however, nothing happened.