With this tutorial you will be able to create an Integration with Slack. You can later use this integration to create specific workflows that follow your everyday process.
Permissions: Only Administrators can create Integrations.
To get started, open your menu and search for 'Integrations'. They are located under the Admin Settings Menu.
Then click the 'Create New Integration' button. Give your Integration a name, like Slack Integration, and optionally add a description that provides more information on its details.
For Authentication Type select, 'OAuth'.
Under Authentication Code add the following code:
(async function(inputs, context){ console.log("data", inputs.data) console.log("current access token", inputs.access_token) console.log("refresh token", inputs.refresh_token) const headers = { "Content-Type": "application/json", "Authorization": `Bearer ${inputs.access_token}`, }; const response = await context.libs.axios.post("https://slack.com/api/auth.test", null, {headers: headers}) console.log("CHECK ACCESS TOKEN", response.data) if (response.data.ok === false || (response.data.ok === true && response.data.expires_in < 20000)) { let urlParams = { client_id: context.integrationConfig.client_id, client_secret: context.integrationConfig.client_secret, grant_type: 'refresh_token', refresh_token: inputs.refresh_token, redirect_uri: inputs.redirect_uri } console.log(urlParams) const response = await context.libs.axios.post("https://slack.com/api/oauth.v2.access", null, {params: urlParams}) var status = response.status; var data = response.data; console.log("Refreshing Access Token") console.log("response data", data) console.log("new access token", data.access_token) const newTokens = { data: { access_token: data.access_token, refresh_token: data.refresh_token } } context.updateAccessTokens(newTokens); return { status: status, access_token: data.access_token, } } else { console.log("Using current Access Token") return { status: inputs.status, access_token: inputs.access_token }; } }(inputs, context));
Next on the Configuration, add your Client Id and your Client secret, in the code snippet below:
{
"client_id": "YOUR_CLIENT_ID",
"client_secret":"YOUR_CLIENT_SECRET"
}
For the OAuth URL use https://slack.com/oauth/v2/authorize
Next we have the Queryparams for OAuth URL.
{
"client_id": "YOUR_CLIENT_ID",
"scope": "channels:read,channels:manage,groups:write,chat:write,chat:write.public,
users:read,users:read.email",
"redirect_uri": "https://freeagent.network/integration-oauth/callback"
}
And last, but not least on the OAuth configuration, the Callback Code is needed:
(async function(inputs, context){
let urlParams = {
client_id: context.integrationConfig.client_id,
client_secret: context.integrationConfig.client_secret,
code: inputs.request.queryParams.code,
redirect_uri: context.integrationConfig.redirect_uri,
}
const respones = await
context.libs.axios.post(https://slack.com/api/oauth.v2.access",null, {params:
urlParams})
var status = response.status;
var data = response. data;
return {
data: data, status: status, access_token: data.access_token, refresh_token: data.refresh_token, redirect_uri: context.integrationConfig.redirect_uri }; }(inputs, context));
Now that we have the OAuth code in place, we need to work on the connectors. Click the 'Add New' button located at the bottom of the 'Create New Integration' window. For this integration, we will create 5 different connectors.
Connector 1. Create Slack Channel | ||
Name | createChannel | |
Description | Optional | |
Input Parameters | ||
Property = name | Type = Text | Options = |
Connector Custom Code | ||
(async function (inputs, context) { |
||
Output Parameters | ||
Property |
Add 2: id name |
![]() |
Connector 2. Get User Email from ID | ||
Name | getUserEmailFromId | |
Description | Optional | |
Input Parameters | ||
Property = userId | Type = Text | Options = |
Connector Custom Code | ||
(async function (inputs, context) { |
||
Output Parameters | ||
Property |
|
Connector 3. Invite Users | ||
Name | inviteUsers | |
Description | Optional | |
Input Parameters | ||
Property = channel | Type = Text | Options = |
Property = userEmails | Type = Text | Options = |
Connector Custom Code | ||
(async function (inputs, context) { |
||
Output Parameters | ||
Property |
|
Connector 4. Post Interactive Message | ||
Name | postInteractiveMessage | |
Description | Optional | |
Input Parameters | ||
Property = channel | Type = Text | Options = |
Property = text | Type = Text | Options = |
Property = block | Type = Text | Options = |
Connector Custom Code | ||
(async function (inputs, context) { |
||
Output Parameters | ||
Property |
|
Connector 5. Post Message | ||
Name | postMessage | |
Description | Optional | |
Input Parameters | ||
Property = channel | Type = Text | Options = |
Property = text | Type = Text | Options = |
Connector Custom Code | ||
(async function (inputs, context) { |
||
Output Parameters | ||
Property |
|