user14623717
user14623717

Reputation:

Slack message based on date in Google Spreadsheet

I have a spreadsheet with several payment dates and want to create a slackbot to post a message to Slack when the date in one specific column match today's date. I'm very new to coding and I'm using a code I found to send an email, but can't find nothing about posting it to Slack. Could someone give me a hand, please?

This is the code I'm using to send email:

function emailAlert() {
  // today's date information
  var today = new Date();
  var todayMonth = today.getMonth() + 1;
  var todayDay = today.getDate();
  var todayYear = today.getFullYear();

  // getting data from spreadsheet
  var sheet = SpreadsheetApp.getActiveSheet();
  var startRow = 2; // First row of data to process
  var numRows = 100; // Number of rows to process

  var dataRange = sheet.getRange(startRow, 1, numRows, 999);
  var data = dataRange.getValues();

  //looping through all of the rows
  for (var i = 0; i < data.length; ++i) {
    var row = data[i];

    var expireDateFormat = Utilities.formatDate(
      new Date(row[6]),
      'ET',
      'yyyy/MM/dd'
    );

    // email information
    var subject = '';
    var message =
      'Hello' +
      '\n' +
      '\n' +
        'These are the due dates for today:' +
      '\n' +
      '\n' +
      ' ID number: ' +
      row[0] +
      '\n' +
      ' Name: ' +
      row[1] +
      '\n' +
      ' Due date: ' +
      expireDateFormat;

    //expiration date information
    var expireDateMonth = new Date(row[6]).getMonth() + 1;
    var expireDateDay = new Date(row[6]).getDate();
    var expireDateYear = new Date(row[6]).getFullYear();

    //checking for today
    if (
      expireDateYear === todayYear &&
      expireDateMonth === todayMonth &&
      expireDateDay === todayDay
      
    ) 
    {
      var subject =
        'Due date ' + row[1] + ' - ' + expireDateFormat;
      MailApp.sendEmail('email address here', subject, message);
      Logger.log('todayyyy!');
    }
  }
}

Upvotes: 0

Views: 809

Answers (1)

codelovesme
codelovesme

Reputation: 3267

Here is a http post request shows how to send a post to Slack

POST https://slack.com/api/chat.postMessage
Content-type: application/json
Authorization: Bearer xoxb-your-token
{
  "channel": "YOUR_CHANNEL_ID",
  "text": "Hello world :tada:"
}

Chackout more information from : https://api.slack.com/messaging/sending

Upvotes: 1

Related Questions