Active IQ Unified Manager Discussions

Slack Integration from WFA

FRAZERBRADBURN
2,443 Views

Hi, I am looking at sending notifications from WFA into slack channels. So rather than using the current mail code I'll be looking at trying to write something to hit a slack channel. 

 

I'm wondering before I begin if anyone has something written already? I have an easy enough powershell script which does that but I'm learning how to invoke these in WFA atm. 

 

 

1 REPLY 1

mbeattie
2,412 Views

Hi Frazer,

 

It's possible in PowerShell (like this example) so it should be possible in WFA.

I would "guess" it's something like this (though I've not tested it):

 

Param(
   [Parameter(Mandatory=$True, HelpMessage="The slack URI")] 
   [String]$Uri,
   [Parameter(Mandatory=$True, HelpMessage="The slack hostname, FQDN or IP address for authentication")] 
   [String]$HostName,
   [Parameter(Mandatory=$True, HelpMessage="The slack channel name")] 
   [String]$ChannelName,
   [Parameter(Mandatory=$True, HelpMessage="The slack channel message")] 
   [String]$Message,
   [Parameter(Mandatory=$True, HelpMessage="The slack channel icon")] 
   [String]$Icon
)
#'------------------------------------------------------------------------------
#'Enumerate the slack credentials from the WFA credential cache.
#'------------------------------------------------------------------------------
$Credentials = Get-WFACredentials $HostName
[String]$userName = $Credentials.GetNetworkCredential().Username
$BodyTemplate = @"
   {
      "channel": "$ChannelName",
      "username": "$userName",
      "text": "$Message. \nTime: DATETIME.",
      "icon_emoji":":$Icon:"
   }
"@
#'------------------------------------------------------------------------------
#'Send the message to the slack channel.
#'------------------------------------------------------------------------------
Try{
   Invoke-RestMethod -uri $Uri -Method Post -body $body -ContentType "application/json" -Credential $Credentials -ErrorAction Stop
   Get-WFALogger -Info -Message "Sent message ""$Message"" to slack channel ""$ChannelName"" using URI ""$Uri"""
}Catch{
   Get-WFALogger -Error -Message $("Failed invoking URI ""$Uri"". Error " + $_.Exception.Message)
   Throw "Failed sending message ""$Message"" to slack channel ""$ChannelName"" using URI ""$Uri"""
}
#'------------------------------------------------------------------------------

In addition to the PowerShell code link, I would assume the REST POST method would require authentication hence i added the $HostName parameter which is used to retrieve a WFA credential object for invoking the REST API.

EG add a WFA credential using 'hooks.slack.com' and the username (EG #wfaslackbot)

 

Hope that helps

 

/Matt

If this post resolved your issue, help others by selecting ACCEPT AS SOLUTION or adding a KUDO.
Public