Last updated March 24, 2019
See updates on orders and tasks in Slack channels for easy collaboration.
If your team uses Slack (like we do at FactoryFour), it’s likely the hub for all your workplace communication. This custom rule will show you how to send notifications to Slack from FactoryFour, making it easy to do things like the following:
{
"engines": {
"node": "> 8"
},
"dependencies": {
"slack-notify": "0.1.7"
}
}
const SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/sampleUrl';
const slack = require('slack-notify')(SLACK_WEBHOOK_URL);
const formatMessage = (url, orderId, taskName) => ({
// Documentation for formatting at https://api.slack.com/docs/message-attachments#link_buttons
text: `Order ${orderId} was updated, click to see more information`,
attachments: [
{
fallback: `See order at ${url}`,
color: 'danger', // or 'good' or 'warning'
fields: [
{
title: 'Order ID',
value: orderId,
short: true
}
],
actions: [
{
type: 'button',
text: 'See Order',
url
}
]
}
]
});
const getOrder = (ctx) => {
const order = Array.isArray(ctx.orders) ? ctx.orders[0] : ctx.order;
return {
id: order.id,
userDefinedIdentifier: order.userDefinedIdentifier,
};
};
const getTask = (ctx) => {
const task = Array.isArray(ctx.tasks) ? ctx.tasks[0] : ctx.entity;
return task;
};
const getUrl = (order, task) => {
let url = 'https://app.factoryfour.com';
let linkout;
if (task) {
if (task.type === 'task') {
if (task.workflowInitiator === task.workflowParent) {
linkout = `#/orders/${order.id}?task=${task.id}&workflow=${task.workflowInitiator}`;
} else {
linkout = `#/jobs/${task.workflowParent}?task=${task.id}`;
}
} else if (task.format === 'job') {
linkout = `#/jobs/${task.workflowParent}`;
} else {
linkout = `#/orders/${order.id}?workflow=${task.workflowParent}`;
}
} else {
linkout = `#/orders/${order.id}`;
}
return `${url}/${linkout}`;
};
module.exports = function (context, cb) {
const order = getOrder(context.body);
const task = getTask(context.body);
return slack.send(formatMessage(getUrl(order, task), order.userDefinedIdentifier, task.name), (err) => {
return cb(err, {
body: {},
})
});
};
Add the FactoryFour Slack application to your Slack Account using the following button. On the next screen, you’ll need to select a channel you’d like updates to be sent to. You can create multiple configurations for the same Slack and FactoryFour accounts if you would like to receive updates in different channels.
'https://hooks.slack.com/services/sampleUrl'
Messages to slack can be modified further with custom messages. See Slack Documentation for more information. Modify the formatMessage
function to include any custom formatting and link.