Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save opn/736f449b17f128979b9b5c05ebed41d8 to your computer and use it in GitHub Desktop.
Save opn/736f449b17f128979b9b5c05ebed41d8 to your computer and use it in GitHub Desktop.
This library interfaces with the Slack REST API.
script "lib_Slack"
--> MetaData
-
license: GPLv3
name: lib_Slack
type: library
version: 0.2
--> Slack
-
command slack_SendMessage someMessage, webHookUrl
put someMessage into tMessage ["text"]
put ArrayToJSON (tMessage) into tMessage
put slack_GetAuthToken() into slackAuthToken
set the httpHeaders to "Content-type: application/json" & CR & "Authorization: token " & slackAuthToken
post tMessage to url webHookUrl
-- should error check
return the result
end slack_SendMessage
--> Slack | Authenticate
-
command slack_Authenticate pSlackClientID, pSlackClientSecret
constant kAuthURL = "https://slack.com/oauth/authorize"
constant kTokenURL = "https://slack.com/api/oauth.access"
constant kScopes = "incoming-webhook"
constant kLocalHostPort = 54303 -- needs to be set in slack oAuth configuration page
if pSlackClientID is empty then
put slack_GetClientID() into pSlackClientID
end if
if pSlackClientSecret is empty then
put slack_GetClientSecret() into pSlackClientSecret
end if
OAuth2 kAuthURL, kTokenURL, pSlackClientID, pSlackClientSecret, kScopes, kLocalHostPort
if the result is not empty then
answer error "Not authorized!"
else
local tAuth
put it into tAuth
local tMessage
ask question "What do you want to send?"
if it is empty then
exit to top
end if
put it into tMessage["text"]
put ArrayToJSON(tMessage) into tMessage
set the httpHeaders to "Content-type: application/json" & CR & "Authorization: token " & sAuth ["access_token"]
post tMessage to url tAuth ["incoming_webhook"]["url"]
put it
end if
end slack_Authenticate
--> Slack | Tokens
-
function slack_GetAuthToken
if the environment = "server" then
put gConfig["slackAuthToken"] into dropBoxAuthToken
else
put pref_FetchValue ("slackAuthToken", "rest") into slackAuthToken
end if
return slackAuthToken
end slack_GetAuthToken
command slack_SetAuthToken slackAuthToken
if the environment = "server" then
-- this is only temporary
-- need to figure out how best to store configs (maybe a json file)
rigSetConfigItem "slackAuthToken", slackAuthToken
else
pref_SaveValue "slackAuthToken", slackAuthToken, "rest"
end if
end slack_SetAuthToken
function slack_GetClientID
if the environment = "server" then
put gConfig["slackClientID"] into dropBoxAuthToken
else
put pref_FetchValue ("slackClientID", "rest") into slackClientID
end if
return slackClientID
end slack_GetClientID
command slack_SetClientID slackClientID
if the environment = "server" then
-- this is only temporary
-- need to figure out how best to store configs (maybe a json file)
rigSetConfigItem "slackClientID", slackClientID
else
pref_SaveValue "slackClientID", slackClientID, "rest"
end if
end slack_SetClientID
function slack_GetClientSecret
if the environment = "server" then
put gConfig["slackClientID"] into dropBoxAuthToken
else
put pref_FetchValue ("slackClientSecret", "rest") into slackClientSecret
end if
return slackClientSecret
end slack_GetClientSecret
command slack_SetClientSecret slackClientSecret
if the environment = "server" then
-- this is only temporary
-- need to figure out how best to store configs (maybe a json file)
rigSetConfigItem "slackClientSecret", slackClientSecret
else
pref_SaveValue "slackClientSecret", slackClientSecret, "rest"
end if
end slack_SetClientSecret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment