Skip to content

Instantly share code, notes, and snippets.

@msciotti
Last active December 9, 2019 22:37
Show Gist options
  • Save msciotti/aa448cc30b8092498883635d9e8b3e76 to your computer and use it in GitHub Desktop.
Save msciotti/aa448cc30b8092498883635d9e8b3e76 to your computer and use it in GitHub Desktop.

Getting Slab Notifications in Discord

We have lots of things to keep track of and lots of places that we get pings. Also, no one checks emails. Here's a small integration to get Slab notifications to ping you on Discord.

Steps

In Discord

  1. Create some sort of test server for yourself (I assume a lot of us already have one)
  2. Create a channel for the pings to go in
  3. Create a webhook in the channel: channel settings -> Webhooks -> Create Webhook
  4. Name it what you want, give it an icon, and copy the Webhook URL at the bottom

In Gmail

  1. Go to https://script.google.com/u/1/home/my
  2. Switch to your Discord GSuite user if necessary
  3. Create a new project
  4. Copy the following code into your script
// Thanks to Joey for an updates script!

var WEBHOOK_URL = ''
// discord limits us to 2000 char, but that seems exessive
var MAX_BODY_LENGTH = 1000

// Slab notifications to Discord
function slabPing() {
  // Search for any notification emails from slab in your inbox
  var emails = GmailApp.search('from:notifications@slab.com in:inbox');
  
  // For each email we find
  for each (thread in emails) {  
    // Get the subject of the email, and create form data for our webhook
    var messages = thread.getMessages();
    var subject = thread.getFirstMessageSubject();
    var lastMessage = messages[messages.length - 1];
    var body = lastMessage.getPlainBody()
      .replace('[image: Slab Logo]', '')
      .replace(subject, '**' + subject + '**')
      .trim()
      .substring(0,MAX_BODY_LENGTH);
    if (body.length === MAX_BODY_LENGTH) {
      body = body.substring(0, MAX_BODY_LENGTH - 1) + '…'
    }
    var formData = {
      'content': body
    };
    
    // Send a POST request with the data to our Webhook URL
    var options = {
      'method' : 'post',
      'payload' : formData
    };
    var res = UrlFetchApp.fetch(WEBHOOK_URL, options);
    var resCode = res.getResponseCode();
    if (resCode >= 200 && resCode < 400) {
      // Mark as read so this doesn't come up again
      thread.moveToArchive();    
    }
  }
}
  1. Save as a script; I called mine Code.gs
  2. Go to https://script.google.com/u/1/home/triggers
  3. Create a new trigger, choosing the slabPing function with a Time-driven event source and set it to run how ever often you'd like

Voila. This script will give you notifications for:

  • Comments on your docs
  • People mentioning you
  • Reactions to your docs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment