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 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 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 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 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.

Concepts used:

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:

 

 

Related pages