Skip to main content

Generate a Slack Message Dynamically Using Custom Script Engine

Slack is one of the most popular workspace communication tools that connect people to the information they require. That information could be text, images, videos, pdf, or personalized reports.

Boltic supports Slack as a destination allowing users to send data over Slack. This guide will teach you how to generate a slack message dynamically using a script engine.

What is a Dynamic Slack Message?

The dynamic slack message is a term used to describe a message that changes based on data. The message will change automatically when the data is changed. And this is achieved by writing scripts in the custom script engine. With this, you can always update the recipients with the latest data over Slack.

Define Custom Script Engine

The custom script engine has an advanced script editor that allows you to enrich the data by writing scripts.

Step by Step to Generate Slack Message Using Script Engine Dynamically

Everything in Boltic revolves around the workflow known as Bolt. It encompasses the steps- Integration, Transformation, and Destination. To get started, you have to create a Bolt. In this example, we will pull data from MongoDB, choose Slack as the Destination, and transform the data format using a script engine.

Step 1: Add Integration

Click the "Add Integration" dropdown to select the Integration. In our case, Integration is MongoDB

Add

Create Dataset

Dataset is a slice and dice output of the data. Use Query Mode or Visual Mode to create a dataset.

Create

Step 2: Apply Transformation

Once the dataset is created. You can accelerate the transformation using-

  • No-code Transformation: A No-code Transformation provides a visual, user-friendly capabilities to transform the data

  • Custom Script Engine: Custom Script Engine gives you to capability to enrich the data using script languages- JavaScript/Python

Apply

Our use case will get solved by using Custom Script Engine. We will write scripts to implement the following use case on the data-

  • Transform the data into a suitable-destination format. Our data is in tabular form and we want to make it as Slack format.
Sample
  • Generate slack message dynamically that notifies recipients Total Revenue by Units Sold.

The following script will transform the tabular data into an attention-grabbing slack message and fetch the latest data from a source to make the message dynamic.

/*
rowNo: row number of current row being processed
row: current row being processed
context: context information for the pipeline batch
returns: modified row
*/
function processRow(rowNo, row, context)
{
row={ "type": "section",
"fields" : [
{
"type": "mrkdwn",
"text": "*Total Revenue:* "+row['Total Revenue']
},
{
"type": "mrkdwn",
"text": "*Units Sold:* "+row['Units Sold']
}
]

}
return row
}


/*
rows: batch data being processed in the pipeline
context: context information for the pipeline batch
returns: modified rows of the pipeline batch data
*/
function process(rows, context) {
const processedRows = []
for (rowNo = 0; rowNo < rows.size(); rowNo++) {
try {
const row = rows[rowNo]
let processedRow = processRow(rowNo, row, context)
if (processRow !== null) {
processedRows.push(processedRow)
}
} catch (err) {
error_rows.capture(400, row, err.message)
}
}
return processedRows
}

Custom Script Engine gives you the ability to test your transformation function with the "Run" option as shown:

Run

Step 3: Send Data to Destination

When you end up with the Transformation. Click the "Send Data Here" button to send the transformed data to a desired Destination. We will select "Slack" as a Destination to send the data over slack.

Send

Step 4: Publish the Bolt

Click "Publish" and give a meaningful name and description to the bolt.

Publish

Dynamic Slack Message

This is how a recipient will receive a message over Slack. As you can see, there are 8 rows in this message. Now, imagine if someone adds more rows in this source every day, the written scripts will fetch the updated data from a source and send the updated records to a recipient in this same format over Slack. So, the significance of using Custom Script Engine with Slack to get the updated data in Slack.

Screen

Run the Bolt

Once your Bolt gets created successfully, go to the Bolt Overview Page, click the "Run" button to execute the Bolt.

Run

Schedule the Bolt

Once you create a Bolt, there's a feature that awaits you Scheduler. With this feature, you can schedule a Bolt and send the message over slack at a specific time interval.

Any Question? 🤓

support@boltic.io