Livecoding: Slackbots and OAuth
Picking up where we left off last time, in this post we'll try and use Express.js and OAuth to create our Slackbot door answering application.
Join the DZone community and get the full member experience.
Join For Free
this is embarrassing. that slackbot that opens doors from last time… it still doesn’t work. i thought we almost had it. i thought i’d identified the hard part. i thought it was just a matter of sitting down and smashing it out.
i was blindsided by oauth and the exploding complexity that always comes in tutorials and api docs! they show you how simple their api is. how absolutely beautiful to use!
one line of code, two clicks of set up, and you’re sending messages to slack like a champ. two clicks, a bit of code, and you’re a bot! you can listen to people’s conversations.
champ! king of the world?
oh, you want to actually receive the promised post requests from those message buttons? boy, do i have a trip for you!
first, you have to set up a slack app. not an ad-hoc custom integration like you’ve had until now. oh no, you need a real app. it takes a few clicks on this screen:
then it’s going to say that this app is part of your team,
but it’s not
. you have to set up oauth, have a server that can do oauth, and then authorize your team with this new app. you need to add an
https
url on this screen:
on the server side, you’re in for a world of doubya-tee-eff, mate. if you’re on express.js, then you can use grant to set up slack oauth somewhat painlessly. in theory , this is all it takes:
|
this uses express’s in-memory session storage, which means that as soon as you restart the server, sessions will be forgotten. the good news is that you don’t have to hold on to them; you just need them long enough to do the oauth dance.
oh, and that
callback
parameter grant asks for? that is
not
the same as the
redirect url
that slack asks for. set them to the same value, and you’re going for an endless redirect ride. learned that the hard way.
go to
<server>/connect/slack
and do the oauth dance for your team. if all goes well, slack rewards you with this message:
if you go to slack’s app management panel, you should see something like this:
/partyhard
your app is officially set up for your team. the slash command you created while debugging and trying to figure out what’s up works. you say
/hai
and it says
pong
.
but when you click a button on a message that you sent, you still get the error. no post request hits your server. it’s an immediate error. it doesn’t even try to do the thing.
it’s an identity issue, i think. based on my last livecoding , we’re sending slack messages based on an ad-hoc integration api key. but we’re a real app, so we should send them as the app.
slack’s documentation doesn’t say how to do that. it talks only about api keys, but apps don’t get api keys. or i don’t understand the docs. or i’m blind.
i should send them an email.
Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments