Woodshop
Search…
Send Smart Action notifications to Slack
This example shows you how to integrate Slack incoming webhooks to receive notifications in your workspace when a Smart Action e.g "Reject application" is triggered.
Demo

Create your Forest Admin slack app

Follow Slack's guide to create a new app in your workspace and start sending messages using Incoming Webhooks.
At the end of this guide, make sure your Slack app has the following features activated:
    Incoming Webhooks
    Interactive Components
    Bots
    Permissions
Once your Slack app has its shiny Incoming Webhook URL, you will be able to send your message in JSON as the body of an application/json POST request.
1
https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Copied!

Connect your app from a Slack channel of your choice

From the channel, visi

Set up the webhook from your admin backend

Install the node.js Slack SDK

From your project's directory, simply run
1
$ npm install --save @slack/webhook
Copied!

Add the Incoming Webhook URL to your .env

1
SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX
Copied!
This Incoming Webhook URL contains a secret key, please make sure it does not appear in your code.

Create the Smart Action and initialize the Incoming Webhook

Smart Action declaration

forest/companies.js
1
const { collection } = require('forest-express-sequelize');
2
3
// This file allows you to add to your Forest UI:
4
// - Smart actions: https://docs.forestadmin.com/documentation/reference-guide/actions/create-and-manage-smart-actions
5
// - Smart fields: https://docs.forestadmin.com/documentation/reference-guide/fields/create-and-manage-smart-fields
6
// - Smart relationships: https://docs.forestadmin.com/documentation/reference-guide/relationships/create-a-smart-relationship
7
// - Smart segments: https://docs.forestadmin.com/documentation/reference-guide/segments/smart-segments
8
collection('companies', {
9
actions: [{
10
name: 'Reject application',
11
type: 'single',
12
fields: [
13
{
14
field: 'Reason(s) for rejection',
15
description: 'Please provide a reason for this decision',
16
type: ['Enum'],
17
enums: ['Certificate of Incorporation', 'Proof of Address ID', 'Bank Statement ID'],
18
required: true,
19
},
20
{
21
field: 'Comment',
22
description: 'This comment will only be displayed in your slack workspace message',
23
type: 'String',
24
widget: 'text area',
25
},
26
],
27
}],
28
// ...
29
});
30
Copied!

Smart Action logic

routes/companies.js
1
const express = require('express');
2
const { PermissionMiddlewareCreator, RecordsGetter } = require('forest-express-sequelize');
3
const { IncomingWebhook } = require('@slack/webhook');
4
const { companies } = require('../models');
5
6
const router = express.Router();
7
const permissionMiddlewareCreator = new PermissionMiddlewareCreator('companies');
8
const url = process.env.SLACK_WEBHOOK_URL;
9
10
// This file contains the logic of every route in Forest Admin for the collection companies:
11
// - Native routes are already generated but can be extended/overriden - Learn how to extend a route here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/extend-a-route
12
// - Smart action routes will need to be added as you create new Smart Actions - Learn how to create a Smart Action here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/actions/create-and-manage-smart-actions
13
14
// ...
15
16
// Initialize webhook
17
const webhook = new IncomingWebhook(url);
18
19
router.post('/actions/reject-application', permissionMiddlewareCreator.smartAction(), async (request, response) => {
20
// Get input form attributes
21
const attributes = await request.body.data.attributes.values;
22
const rejectionReason = attributes['Reason(s) for rejection'];
23
const comment = attributes['Comment'];
24
25
// Get selected company
26
const [selectedCompanyId] = await new RecordsGetter(companies, request.user, request.query).getIdsFromRequest(request);
27
const selectedCompany = await companies.findByPk(selectedCompanyId);
28
29
// Change company status to rejected
30
await companies.update({ status: 'rejected' }, { where: { id: selectedCompanyId } })
31
response.send({ success: 'Company\'s request to go live rejected!' });
32
33
// Trigger Slack webhook
34
await webhook.send({
35
text: 'An action has been triggered from Forest Admin',
36
channel: 'C01CFGCADGF', // replace by your Slack channel ID
37
blocks: [
38
{
39
type: 'header',
40
text: {
41
type: 'plain_text',
42
text: 'Action triggered - Company application rejected :x:',
43
emoji: true,
44
},
45
},
46
{
47
type: 'divider',
48
},
49
{
50
type: 'section',
51
text: {
52
type: 'mrkdwn',
53
text: `${request.user.firstName} ${request.user.lastName} just rejected <https://app.forestadmin.com/Live-demo/Production/Operations/data/companies/index/record/companies/${selectedCompanyId}/summary|${selectedCompany.name}>'s request to go live!\n\n • *Reason for rejection:* ${rejectionReason[0]}\n • *Comment:* ${comment}`,
54
},
55
accessory: {
56
type: 'image',
57
image_url: `${selectedCompany.logoUrl}`,
58
alt_text: 'company logo',
59
},
60
},
61
{
62
type: 'section',
63
text: {
64
type: 'mrkdwn',
65
text: 'For more details on the company',
66
},
67
accessory: {
68
type: 'button',
69
text: {
70
type: 'plain_text',
71
text: 'View Notes',
72
emoji: true,
73
},
74
value: 'null',
75
url: `https://app.forestadmin.com/Live-demo/Production/Operations/data/companies/index/record/companies/${selectedCompanyId}/collaboration`,
76
action_id: 'button-action',
77
},
78
},
79
],
80
});
81
});
82
83
module.exports = router;
84
Copied!
To learn more about composing messages using the Slack API, please visit the
To learn more about error handling of Slack Interactive Webhooks, please visit the Slack changelog.
Last modified 2mo ago