Skip to content

Instantly share code, notes, and snippets.

@sohjsolwin
Created May 9, 2019 18:52
Show Gist options
  • Save sohjsolwin/866fcff22749733d8b9ebd49b4b66c7a to your computer and use it in GitHub Desktop.
Save sohjsolwin/866fcff22749733d8b9ebd49b4b66c7a to your computer and use it in GitHub Desktop.
Send Private Messages through Slack API
$buildVCS = "whateverTheSHAofCommitYouWant"
import-module posh-git
git show - pretty $buildVCS > $pwd\test.txt
$a = get-content test.txt
($a -split "`r`n") | ForEach-Object {
# the pattern is modified to check for a First Last name, or a single username, depending on how people have their git setup. 
$email = Select-String -InputObject $_ -Pattern "Author: ([A-Za-z]{2,20} [A-Za-z]{2,20}|[A-Za-z]{2,20}) <[A-Za-z]{2,20}@YOUR_DOMAIN\.com>" | % { $_.Matches } | % { $_.Value }
if($email){
$who = $email -replace "Author: ([A-Za-z]{2,20} [A-Za-z]{2,20}|[A-Za-z]{2,20}) <", '' -replace ">", ''
}
}
Write-Output "determined email = $who for this specific build to be the originator."
Write-Output "Proceeding to lookup user in Slack and Send them a message."
$targetUser = Invoke-RestMethod -Method POST -Uri "https://slack.com/api/users.lookupByEmail?email=$who@YOUR_DOMAIN.com&token=xoxb-THE-OBNOXIOUSLY-LONG-TOKEN" -ContentType 'application/x-www-form-urlencoded' | ConvertTo-Json
($targetUser -split "`r`n") | ForEach-Object {
$userID = Select-string -InputObject $_ -Pattern '"id": '
if($userID){
Write-Output "FOUND"
$cleanID = $userID -Replace '"id":', "" -replace '"', "" -replace ',', ""
$cleanID = $cleanID.trim()
}
}
Write-Output "ID:$cleanID"
$stuff ='Whatever Stuff you want to tell your user.'
Invoke-RestMethod -Method POST -Uri "https://slack.com/api/chat.postMessage?token=xoxb-THE-OBNOXIOUSLY-LONG-TOKEN&channel=$who&text=$stuff" -ContentType "application/json"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment