Send a Slack reminder message using Google Apps Script and Slack API

How to create a slack bot

In this blog, I will show you how to send an automated scheduled message to Slack channel using Google Apps Script and the Slack API.

Step 1: Create a New API

First, create a new app at https://api.slack.com/apps. This app will act as a bot to send automatic messages to Slack channels. After creating your app, navigate to the app page by clicking the link associated with your app's name.

Slack api create new app

Step 2: Generate Webhook URL

Navigate to the Incoming Webhooks page and activate incoming webhooks by toggling the button on

Activate incoming webhooks

Then, add a new webhook to your workspace. You can select any channel or direct messages from your workspace so that the bot will send a message to that channel or person.

Add new webhook

Copy the webhook url after creating hook. (Ex webhook url: https://hooks.slack.com/services/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)

Step 3: Send an automatic message through Google Apps Script

Create a new script file and project here. Replace your webhook URL in the provided code and paste this code into your project file.
function slackReminder() {
  let slackMsg = {
    "channel": "Reminders",
    "blocks": [
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": "Hey buddy \n Welcome to buggerspot",
        }
      }
    ]
  };
  sendSlackReminder(slackMsg);
}

function sendSlackReminder(payload) {
  const webhook = "https://hooks.slack.com/services/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"; 
  var options = {
    "method": "post", 
    "contentType": "application/json", 
    "muteHttpExceptions": true, 
    "payload": JSON.stringify(payload) 
  };
  
  try {
    UrlFetchApp.fetch(webhook, options);
  } catch(e) {
    Logger.log(e);
  }
}
Send slack messages through Google App Script
Make sure that the 'slackReminder' function is selected from the function options above. After saving the file, simply run the selected function. The first time, it will ask for permission to send a message. Please grant permission to the bot.
Now, you can send the messages to Slack channels or any direct messages with this APP.

Slack Bot Message

Step 4: Schedule automatic messages using triggers

Move to the "Triggers" tab in your project.

Triggers settings in Google App Script

Add a new trigger; it will display a popup box with schedule settings. Adjust the settings according to the provided image and save it. This will enable the script to send a message hourly.

Google app script trigger hourly

Thank You!