FactoryFour Learn Center Index

Last updated March 24, 2019


Slack Updates from FactoryFour

See updates on orders and tasks in Slack channels for easy collaboration.


Description:

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:

  • triage production issues
  • see order updates
  • know when new work is available

How to Use:

  1. Creat a new custom rule for any event.
  2. Configure the scope of the rule to match when you would like to receive Slack notifications.
  3. Copy the following code into the package.json file.
     {
       "engines": {
         "node": "> 8"
       },
       "dependencies": {
           "slack-notify": "0.1.7"
       }
     }
    
  4. Copy the following code into the index.js file.
     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: {},
         	})
           });
     };
    
  5. 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.

    Add to Slack

  6. Copy the url from the success screen and paste it on the first line for SLACK_WEBHOOK_URL 'https://hooks.slack.com/services/sampleUrl'
  7. Save the code in the editor, and turn the rule on

Message Configuration

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.


Search the FactoryFour Learn Center


FactoryFour Learn Center