Send Messages to Slack

In this article we will discuss how to send messages or updates from Tadabase to your selected Slack channel.

Sending updates from Tadabase to Slack requires a five step process:

  1. Create a Slack App and Webhook
  2. Create your fields in Tadabase
  3. Create a script
  4. Launch script as web app
  5. Send a test message

Let's explore each of these steps with a simple example below.

1. Create a Slack App and Webhook

The first step begins in your Slack dashboard where you will create an app in your Slack account by clicking on the "Create your Slack app" button.

Slack will then continue to guide you to enable incoming webhooks as well as create an incoming webhook that will provide you with a unique URL that you will use to send messages from your Tadabase app to Slack. This webhook URL will automatically direct your Tadabase messages/updates to your selected Slack channel.

To read more about creating a webhook in Slack, please refer to https://api.slack.com/messaging/webhooks.

2. Create your fields in Tadabase 

Once you have your Slack webhook URL, you can then go to your Tadabase app and create the fields that you want to include in the message that you send to Slack. 

Sending messages and updates to Slack opens up a myriad of automation opportunities, such as sending automated updates to your selected Slack channels whenever a new user signs up, a new job is created, a new customer is added, or any other update you would like to automatically send to Slack. While you can automate highly complex updates to Slack, for the purpose of this article we will use a simple example of sending a simple message to Slack.

To send any message to Slack from your Tadabase app, you must first create the fields that you would like to send in your message within a new or existing data table within your Tadabase app.

For our simple example, we will create a new data table called "Messages" and within this data table we will add four fields:

  1. Message One 
  2. Message Two 
  3. Slack webhook URL
  4. SendSlack radio option field

Message one and Message Two will be the actual messages that gets sent to Slack. 

"SendSlack" is the Radio field where we determine on a per-message basis if this message should indeed be sent to Slack. 

"SlackURL" is the Slack webhook URL that you retrieved from Slack that will direct your message to your selected Slack channel. 

Our goal is that whenever a record is added in Tadabase and "SendSlack" is "Yes", the record should be sent to the indicated Slack webhook URL with the included messages.

A sample record of these four fields used in this example will appear as such: 

    As notated in the image above, each field has a unique field ID that can be found at the top right of each field when you click on the field to edit. Simply click each field and locate its field ID on the top right corner of the Edit Field screen. You will need the field IDs of each field included in your Slack message within the following step.

    field-id.png

    Please Note: In the above example we manually entered the Slack webhook URL into an individual record. This was done for the purpose of a simple example, however generally the recommended approach is to save your Slack webhook URL within a record rule in order to automate this process for multiple records. For instance, in your Users data table you may have a Slack webhook URL saved for each user. When a user adds a message, that user's unique URL would be retrieved and that specific record would be dynamically updated- all in an automated process. 

    Now that our message fields have been created in Tadabase, we can move on to the following step which will include creating our script. 

    3. Create a Script

    This step can be completed with any language , API, or Webhook, however for the purpose of this article we will use Google Script for its simplicity and ease. 

    Create a new script and copy/paste the code below into the script. 

    Be sure to customize your script according to your field values. Wherever it says "field_X", replace it with your field IDs belonging to the fields you created within the previous step.

    function sendSlack(SLACK_WEBHOOK_POST_URL, MESSAGE_TEXT, MESSAGE_TEXT_2) {
      
       var fields = [
        {
          "title" : "Message",
          "value" : MESSAGE_TEXT,
          "short" : false
        },
        {
          "title" : "Message 2",
          "value" : MESSAGE_TEXT_2,
          "short" : false
        },
      ]
      
      var attachments = [
        {
          "fallback" : "The attachment must be viewed as plain text.",
          "pretext" : "New message from Tadabase",
          "mrkdwn_in" : ["pretext"],
          "color" : "#0000DD",
          "fields" : fields
        }
      ];
      
      var payload = {
        "attachments": attachments
      };
    
      // Build request
      var options = {
        method: "post",
        payload: JSON.stringify(payload)
      };
    
      // Send to Slack
      UrlFetchApp.fetch(SLACK_WEBHOOK_POST_URL, options);
    };
    
    
    function doPost(e) {
      
      //If you are using a webhook address that's saved in your database, change the field_47 to the webhook field here. 
      var SLACK_WEBHOOK_POST_URL = e.parameter['field_47']; 
      
      //If all your messages are being sent to the same URL, change the link to: 
      //var SLACK_WEBHOOK_POST_URL = "WEBHOOK_URL_FROM_SLACK"; 
      
      //Change the 'field_45' and field_48 to the field id's containing the message you want to send to slack
      var MESSAGE_TEXT = e.parameter['field_45'];
      var MESSAGE_TEXT_2 = e.parameter['field_48'];
      
      //If you have a field that checks if message should be sent, pass it here. 
      var SEND_SLACK = e.parameter['field_46'];
      
      //If you're not using conditions, remove the IF statement. 
      if(SEND_SLACK == "Yes"){
        sendSlack(SLACK_WEBHOOK_POST_URL, MESSAGE_TEXT, MESSAGE_TEXT_2)
      }
      
     }
    

     

    4. Launch script as web app

    Next, click on Publish and Deploy as Web App to launch your script.

     

    Be sure to make it visible by selecting "Anyone, even anonymous" under app visibility access.

     

    Next, copy the URL provided and add it as a webhook within your Tadabase app. 

     

    5. Send a Test message

    Upon a successful setup, when records are added to your Tadabase app a webhook will be triggered to automatically send your message(s) to your Slack channel.