Microsoft Teams Cards with Webhooks

Helpful script writing tricks and HowTo's
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Microsoft Teams Cards with Webhooks

Post by jNizM » 02 Jun 2021, 07:18

Send Microsoft Teams Cards via Webhooks with AutoHotkey

1) Create an incoming webhook
2) Build cards and task modules


Working Code

Code: Select all

; GLOBAL SETTINGS ===============================================================================================================

#NoEnv
#SingleInstance Force
#NoTrayIcon

SetBatchLines -1


; SET GLOBALS ===================================================================================================================

global Webhook := "https://asdfasdf.webhook.office.com/webhookb2/asdfasdf-asdf-asdf-asdf-asdfasdfasdfasdf@asdfasdf-asdf-asdf-asdf-asdfasdfasdf/IncomingWebhook/asdfasdfasdfasdfasdfasdf/asdfadsf-asdf-asdf-asdf-asdfasdfasdf"


; GET INFOS =====================================================================================================================

ComputerName := A_ComputerName
Date         := A_YYYY "-" A_MM "-" A_DD " " A_Hour ":" A_Min


; CREATE JSON ===================================================================================================================

Content=
(
{
	"@type": "MessageCard",
	"@context": "http://schema.org/extensions",
	"themeColor": "0076D7",
	"summary": "AutoHotkey webhook with Teams",
	"sections": [{
		"activityTitle": "Show some informations",
		"facts": [{
			"name": "ComputerName",
			"value": "MyPC"
		}, {
			"name": "Date",
			"value": "%Date%"
		}],
		"markdown": true
	}]
}
)


; SEND DATA =====================================================================================================================

try
{
	hr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
	hr.Open("POST", Webhook)
	hr.SetRequestHeader("Content-Type", "application/json")
	hr.Send(Content)
	hr.WaitForResponse()
}


; EXIT ==========================================================================================================================

;MsgBox % "Status: " hr.Status "`nResult: " hr.ResponseText
ExitApp


; ===============================================================================================================================
See Examples on the next post
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: Microsoft Teams Cards with Webhooks

Post by jNizM » 02 Jun 2021, 08:42

Examples


Work with AutoHotkey variables

Code: Select all

Date := A_YYYY "-" A_MM "-" A_DD " " A_Hour ":" A_Min

Content=
(
{
	"@type": "MessageCard",
	"@context": "http://schema.org/extensions",
	"themeColor": "0076D7",
	"summary": "AutoHotkey webhook with Teams",
	"sections": [{
		"activityTitle": "Show some informations",
		"facts": [{
			"name": "ComputerName",
			"value": "%A_ComputerName%"
		}, {
			"name": "Date",
			"value": "%Date%"
		}],
		"markdown": true
	}]
}
)
Image



Buttons to open URLs

Code: Select all

Content=
(
{
	"@type": "MessageCard",
	"@context": "http://schema.org/extensions",
	"themeColor": "0076D7",
	"summary": "AutoHotkey webhook with Teams",
	"sections": [{
		"activityTitle": "Usefull AutoHotkey Links",
		"activitySubtitle": "",
	}],
	"potentialAction": [{
		"@type": "OpenUri",
		"name": "Homepage",
		"targets": [
			{ "os": "default", "uri": "https://www.autohotkey.com/" }
		]
	}, {
		"@type": "OpenUri",
		"name": "Forum",
		"targets": [
			{ "os": "default", "uri": "https://www.autohotkey.com/boards/" }
		]
	}, {
		"@type": "OpenUri",
		"name": "Documentation",
		"targets": [
			{ "os": "default", "uri": "https://www.autohotkey.com/docs/AutoHotkey.htm" }
		]
	}]
}
)
Image
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile

Post Reply

Return to “Tutorials (v1)”