Introduction
This article introduces how to utilize Slack’s Incoming Webhook feature, to post messages into Slack channels when a workflow status of a record in a Kintone App is proceeded to "Completed".
For more details about Kintone, click here.
Scenario
Although Slack has many bots in the marketplace, marketplace bots tend to only function for one particular scenario, which may not always be flexible enough to meet the user's needs.
Different database instants within Kintone can cover many user scenarios depending on the user’s needs, such as CRMs, Project Management, Interview Notes, Task Lists, Expense Reports, PTO request forms and so on. Kintone Apps tend to result in higher productivity if their Process Management settings have been enabled, that defines a streamline workflow for every record within the database instance.
This article shows a quick way to spin up a bot that will send messages to a Slack channel from browser actions within Kintone. A JavaScript customization applied to a Kintone App will trigger when the workflow of a record is proceeded to a particular status. The script sends a request to Slack's Incoming Webhook feature to post messages into Slack channels. The contents of the scripts are simple, making it possible for the script to be adapted to the user's needs, so that the bot can flexibly function for many types of scenarios.
Setting up the connection
In this article, messages from Kintone to Slack will be posted with the below scenario:
- End users use a Shared to-do App inside Kintone, where they use the process management feature to keep track of the status of each task
- Tasks in the Kintone App that are set to "Completed" are notified to a Slack channel
Follow the steps below to set up the connection between Kintone and Slack:
1. Get a Slack channel ready
Create a Slack channel that will receive notifications from Kintone.
2. Set up an Incoming Webhook in Slack
In Slack, open the Incoming Webhook Integration page.
Choose the channel to which you want to send messages to and click the [Add Incoming WebHooks Integration] button. An incoming webhook will be set up.
Take note of the URL displayed in the Webhook URL settings, as it will be used in the next step. Input anything for the Descriptive Label, Customize Name and Icon.
3. Prepare a Kintone App
If you don’t have a Kintone environment, you can apply for a free 1 year Kintone developer license, by joining the Kintone Developer Program. For more details, check the following Kintone Developer Program article.
Log into your Kintone environment, and navigate to the Kintone App where the customization will be applied, or create a new App from the marketplace. Navigate to the App's settings, and to the "Process Management" settings. Check the "Enable process management" check box, if this setting has not been enabled yet. Make sure to include a status named "Completed" in the Status Settings.
Save the settings, and click on "Update App" to apply the changes to the Kintone App.
4. Upload a JavaScript file to Kintone
Copy the contents of the below code into a local JavaScript file.
post2slack.js
(function() {
'use strict';
kintone.events.on('app.record.detail.process.proceed', function(e) {
if (e.nextStatus.value == 'Completed') {
var thisUrl = 'https://<YourSubdomain>.kintone.com/k/' + kintone.app.getId() + '/show#record=' + kintone.app.record.getId();
var webhookUrl = '<IncomingWebhookURL>';
var payload = {
'text': '\"<' + thisUrl + '|' + e.record.<FieldcodeOfTask>.value + '>\" has been completed!'
};
return new kintone.Promise(function(resolve, reject) {
kintone.proxy(webhookUrl, 'POST', {}, payload, function(body, status, headers) {
console.log(status, body);
resolve(e);
});
});
}
});
})();
Refer to the following table for variables that need to be replaced:
Variables to replace | What to replace with |
<YourSubdomain> | The subdomain of your Kintone environment. |
<IncomingWebhookURL> | The Webhook URL created when setting up a new Incoming Webhook on Slack. |
<FieldcodeOfTask> | A field code of any field that exists in your Kintone App. The value of that field will be included in the Slack message. |
Save the local JavaScript file, navigate to the App's settings, then to "JavaScript and CSS Customization", and upload the file to the "Upload JavaScript for PC" settings. Save the settings, and click on "Update App" to apply the changes to the App.
In this code, an event is triggered when a user proceeds a task in a record. If the status is proceeded to "Completed", the kintone.proxy API is used to send a POST request to Slack's Incoming Webhook URL.
Test the connection
If the record list in the Kintone App is empty, add a new record into the App. Navigate to the details page of a record in the App, and proceed the process management status until it reaches "Completed".
This should trigger the event to fire data into the Slack Channel. Check the Slack Channel to see if a message has been posted.
If nothing is being posted into your Slack channel, try reading through the Debugging Tips for Kintone JS article to check for bugs.
Summary
In this example, a Kintone native event was used to catch the timing of when a status was proceeded within a record. Once the event was triggered, Kintone’s JavaScript API was used to post an HTTP request to Slack’s incoming webhook, which then proceeded to post a message into a Slack channel.
Although a scenario for using a Shared to-do App was used in this scenario, the code can be adjusted to fit many types of Apps that users build on Kintone. The status name written inside the code can be changed to a different status name set in the process management settings. A different event could also be set, such as an event that is triggered when a new record is added into the Kintone App, or when a record is updated within the Kintone App.
For more Kintone related integration tutorials, check the Kintone Developer Program Website.