API Hack: Slack + Office 365

My team at Microsoft is sprinkled all around the world, from Tel-Aviv to London, Boston and San Francisco (where I happen to work). This can be awesome but chatting on slack and the occasional video conference isn’t enough and so a couple times a year we all get together as a team for an offsite.

Our team is all about passionate technologist adept at learning new technologies and to put it short — making awesome stuff. This year’s offsite was a casual hackathon in Seattle involving Office 365, the Microsoft Office SaaS service which has become pretty popular the last couple years.

Ever Go Out of Office?

Here’s the scenario – your team uses Slack and Office 365 (Outlook Mail in particular). You go on vacation, so you block your calendar saying you’re ‘Out of Office’ but you still get tons of @mentions on Slack. Your team has no idea that you’re out of office because they aren’t emailing you, they’re Slack-ing you (is that a word yet?).

Any who, wouldn’t it be nice to automagically tell folks who @mention you that you’re out of office, similar to an out of office reply?

The Hack

So the problem is simple – tell people that you’re out of office on Slack without having to anything special. If we had a smart, calendar-aware Slack Bot to tell our coworker’s we’re out this would totally solve the problem.

All we need to do is integrate the Office 365 API with the Slack Bot API. Then all you need to do is mark your vacation time on their calendar — like you would normally do — and the slackbot will let any folks who @mention you that you’re out.

OOO-Bot

Obviously we aren’t gonna start from scratch. Usually I do a quick search on github to see if anyone wrote anything similar to what I want. And what a coincidence! Someone has!… well kinda.

Thanks to this awesome slackbot repository, slack-ooo, we pretty much get all the Slackbot functionality of listening for @mentions and replying out of the box. If you launched this bot with your own Slackbot client id and key, you would quickly see it plugin to your slack account, but also notice that it isn’t very smart and just holds OOF status in-memory:

slacko3654

To get this to work, you’ll have to make your way over to the Slack Bot page in order to get an API key and secret.

Eventually we forked this bot and made it smart. Keep reading to find out how!

Authenticating to Office 365

The second part of this hack is to use Office 365. Which means we need to authenticate with Azure Active Directory (AAD) to Office which can be a bit tricky.

We wrote our app in Node.js using the passport and the passport-azure-oauth extension. Passport is a library that works with connect-style middleware like what can be found in Express. There’s a couple things to do to get things setup.

First, you need to initialize passport on startup:

function setupPassport () {
    var settings = {
    callbackURL: nconf.get('returnURL'),
    realm: nconf.get('realm'),
    clientID: nconf.get('clientID'),
    clientSecret: nconf.get('clientSecret'),
    //resource: 'http://outlook.office.com/',
    //oidcIssuer: nconf.get('issuer'),
    identityMetadata: nconf.get('identityMetadata'),
    responseType: nconf.get('responseType'),
    responseMode: nconf.get('responseMode'),
    skipUserProfile: nconf.get('skipUserProfile'),
    scope: ['https://outlook.office.com/calendars.read']
    };

    return new RSVP.Promise(function (resolve){
        passport.use(new OIDCStrategy(settings,
        function(iss, sub, profile, accessToken, refreshToken, done) {
            return resolve({
                iss: iss,
                sub: sub,
                profile: profile,
                accessToken: accessToken,
                refreshToken: refreshToken,
                done: done
            });
        }
        ));
    });
}

Checkout the full configuration file sample which has the correct configuration values for Office 365.

Then register the OAuth callback URLs. If you’re using express, depending what router your authentication routes are set on it should look similar to this:

app.get('/auth/azureOAuth',
  passport.authenticate('azureOAuth', { 
    failureRedirect: '/login'
  }),
  function(req, res){
    // The request will be redirected to SharePoint for authentication, so
    // this function will not be called.
  });

app.get('/auth/azureOAuth/callback', 
  passport.authenticate('azureOAuth', { 
    failureRedirect: '/login'
    // refreshToken: azureOAuth_RefreshToken 
  }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/');
  });

Finally in your Express initialization code make sure to initialize the passport middleware:

// Place this after you initialize your session middleware
app.use(passport.initialize());
app.use(passport.session());
// Place this before your cookieParser initialization

Pay attention to the order you place this initialization as it should come after your session manager initialization.

You’ll also need to get an API key and secret from the Office Dev Center with Calendar.Read access. The callback URL is specified by your application registration and in the example above would be registered as: http://<hostname>/auth/azureOAuth/callback.

The Slack Office 365 Experience!

Finally, after a couple days of work (and a few beers in between) our team of pointy haired geeks modified the existing OOO-bot by making it completely asynchronous to allow for checking of the calendar api.

All you have to do is:

One-Time Login

A one-time log in to our app to authenticate to your calendar:

slacko3651

Block Your Calendar

Create a new Calendar event with ‘OOO or OOF’ somewhere in the event title, and create a nice Out of Office message:

slacko3652

Voila

Magic happens:

slacko3653

Give Me Codez!

Full disclosure – this is a rough hack made in 2 days – it totally works but keep an eye out as this will see a bit more love as I get more time.

For now, you can checkout the source code and create your own slack out of office bot!

2 responses on “API Hack: Slack + Office 365

  1. Bryant Avey

    That’s cool Steve! I never used Slack or any Zapier products but heard about it through Sharegate. looked it up and saw what you were doing with it. So I signed up for a free account after reading your blog. I have several clients that might like this and had no idea it even existed.

    Thanks for the post.

  2. Jon Roberts

    Steven,

    I’d like to try this out for our office, but I’m having trouble finding your code. I didn’t see a link to it in your post. I searched and found the slack-ooo project on github. There were two public forks I found and neither appeared to be your changes. Can you help me out?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>