PowerShell hack to reformat webhook payload from GitHub to MS Teams. I run it as an Azure Function. This same concept will also work for other applications that send complex payloads via webhook.
Github Webhook -> Azure Function -> MS Teams Webhook.
# Accept the data from the incoming webhook.
param (
[object]$WebhookData
)
# MS Teams Webhook URL
$WebHookURL = "https://outlook.office.com/webhook/1234567890/"
# Make sure we have a payload before processing
if ($WebhookData -ne $null) {
# Split part of the WebhookData
$WebhookHeaders = $WebhookData.RequestHeader
$WebhookBody = $WebhookData.RequestBody | Convertfrom-Json
switch ($WebhookHeaders.'X-GitHub-Event') {
# https://developer.github.com/v3/activity/events/types/#commitcommentevent
'commit_comment' {
$text = '{0} commented on a commit in the {1} repository.<BR><BR>
Comment:{2}<br><br>
More information <a href="{3}">here</a>'
-f $WebhookBody.sender.login, $WebhookBody.repository.name, $WebhookBody.comment.body, $WebhookBody.comment.html_url
}
Default {
$text = "An event was triggered but we don't know how to handle it."
}
}
$body = '{"text" : "' + $text +'."}'
Invoke-WebRequest -Headers @{"ContentType" = "application/json"} -Body $body -uri $WebHookURL -Method Post -UseBasicParsing
}
else {
Write-Error "Runbook mean to be started only from webhook."
}