Skip to main content

SLACK Library

The SLACK Library is an importable library you can use in your projects to help you connect and create Slack messages using the Slack API. It is used by the Slack Notifier Adapter.

You can get very creative, writing additional methods to add to the client - checkout the Slack API docs for all the available methods.

How it works:

Use the Translator's built in help to review how to use each function:

SLACKClient - creates the adapter framework using a metatable

SLACKclient creates the Slack adapter framework.

SLACKmessage and SLACKcustom modules are defined in a metatable as methods and set to the S object. The API key passed to SLACKclient is assigned to the new S table so that it can be used by the methods.

local MT={}   

MT.__index = {}
MT.__index.message = require ’SLACK.SLACKmessage’
MT.__index.custom = require ’SLACK.SLACKcustom’

help.map{dir=’SLACK/help’,methods=MT.__index}

function SLACKclient(T)
S= {}
setmetatable(S, MT)
S.key = T.key
return S
end

Concepts used:

SLACKcustom - takes in any API request parameters to make a request and return the response

SLACKcustom is a helper function designed to handle different API requests with Slack.

It prepares the header with the authorization details, and assigns the url endpoint and parameters prepared by SLACKmessage. net.http.get is used to send the request to Slack. The response is parsed and either the response or error response is returned.

local function SLACKcustom(T, C)   
local Headers={}
Headers.Authorization = "Bearer "..T.key
local Url = "https://www.slack.com/api/"..C.api
trace(Headers, Url, C.parameters)
local R, Code = net.http.get{url=Url, headers=Headers,parameters=C.parameters, live=C.live}
if (C.live ~= true) then
return "notlive"
end
R = json.parse{data=R}
if not R.ok then
return false, R.error
else
return true, R
end
end

return SLACKcustom

Concepts used:

There are two methods included in the library.

SLACKuserTagByEmail - used to get the associated UserTag

SLACKuserTagByEmail method is used to get the userTag associated with the email set in the custom fields.

function UserTag(S, Email)   
local UserTag = S:userTagByEmail{email=Email,live=true}
return UserTag
end

SLACKmessage - creates a message for the Slack chat.postMessage API call

SLACKmessage is passed the client object and the table of defined parameters. SLACKmessage formats the required endpoint and parameters to call SLACKcustom to make the 'chat.postMessage' API call.

local function SLACKmessage(T, C)   
local P = {}
P.channel = C.channel
P.text = C.text or "No text was supplied"
P.unfurl_links = "false"
P.unfurl_media = "false"
return T:custom{api=’chat.postMessage’, parameters=P, live=C.live}
end

return SLACKmessage

Concepts used:

SLACKcreateMessageBlocks - used to create a block for messages

SLACKcreateMessageBlocks creates different types of blocks that will be concatenated together in the final message. For example, if you wanted to create a text block followed by a button block:

local TextBlock = S:createMessageBlock\{   
block_type = 'section', text = Text}
local ButtonBlock = S:createMessageBlock\{
block_type = 'actions',
elements = \{
url = Url, text = 'Click Here', text_type = 'plain_text', type = 'button'}})

local Blocks = \{
[1] = TextBlock,
[2] = ButtonBlock
}

local R, Out = S:message\{channel=ChanId, blocks = json.serialize\{data=Blocks}}