Skip to content

Instantly share code, notes, and snippets.

@jmassardo
Last active February 26, 2019 18:19
Show Gist options
  • Save jmassardo/55efbbf58693fcdad9789f62ff55fd73 to your computer and use it in GitHub Desktop.
Save jmassardo/55efbbf58693fcdad9789f62ff55fd73 to your computer and use it in GitHub Desktop.
This is a simple PowerShell hack to translate an incoming webhook from Github to MS Teams.

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."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment