Building a Slack Chat Bot

Handling a Shortcut

Jason Lengstorf

Jason Lengstorf

Learn With Jason
Building a Slack Chat Bot

Check out a free preview of the full Building a Slack Chat Bot course

The "Handling a Shortcut" Lesson is part of the full, Building a Slack Chat Bot course featured in this preview video. Here's what you'd learn in this lesson:

Jason creates a shortcut in the Slack UI to ask a user to use the slash command. This is encourages team members to adhere to a process in a less intrusive way and reduces friction on the team.


Transcript from the "Handling a Shortcut" Lesson

>> So now that we've got the bot responding to the slash command and kind of putting the details from the payload from the user input back into chat, we wanna do one more thing before we start dealing with saving that input. Which is, if somebody's not following our process, which is pretty common on teams that I've been on, it's pretty standard for somebody to kind of fly in from a meeting and say, hey, I don't really like chocolate.

If I can type. And in this case, they're starting a food fight but in a work context, they would be saying, hey, we need x or y. We need your help with this. We were working on x launch. And the message flies into Slack, and then who's responsible for keeping up on that, right?

Is it the person who posted it is supposed to be tracking that task? Is it the team? It's hard to say. So what we really want is we want a way to nudge that person to follow the process, which we could do by going in here and being like, hey, please use the process.

But doing it this way it's a tiny conflict even if it's not actually that tense or that big of a deal and it's still just a little moment of you having to kind of steal yourself to go tell a co worker that they're wrong and that they need to do something a different way.

And that drains energy, it uses up some of your spoons and leaves you with less in the tank to do your job. So a better way to do this is to let a robot do it for us. So we are going to add a context menu shortcut, so that we can ask people without having to type anything to them.

We just wanna be able, instead of having to do it this way. We wanna open up this menu here, and then ask them to do a thing. So, to handle that, we're going to go back to the browser. And we're gonna get into our interactivity and shortcuts menu again, and we're gonna create a new one.

Now to create a new one, we want this to work on messages. So there's an option globally to just hit that little lightning bolt icon and it'll give you options of things you wanna do. But in our case, we're responding directly to a message, so we want this to be on Messages.

So we're gonna hit that hit next and then we are going to give it some details. So the details that we want to include our request to food fight and that's gonna be what shows up in the menu. And then we wanna have a short description and this is going to be something that just kind of gives some details on why you would use this context menu.

So, did your coworker just offend your food sensibilities? Ask them to start a food fight so you can lay into them for eating incorrectly. And then as a callback ID, this can be anything you want as long as it's unique, and I'm gonna call this one start food fight nudge.

So I'm gonna hit Create and then this is a little unintuitive. You also have to hit Save or else it's not going to do what you wanted to do. So, I have now saved and I'm gonna come out here and reload and then we can see that we've got the request a food fight.

I hit the wrong button. Request a food fight, and it'll say it didn't work and that's because we're not handling it yet. So then we can open this up and we're going to add a new case in here. So we're gonna say case, and it's going to be start food-fight-nudge, and then that'll have a break down below.

Then we can collapse these so that we can focus on just the piece that we're worried about. Great, the first thing we need is we need to figure out what channel the nudge was sent from. And that comes from our payload. And the channel, and then we'll grab the id.

Next, we wanna know who was the person that needs to be nudged because we're gonna tag them. So user id, and that's gonna come out of the payload user id. Then we need to know what the thread is that we're in because what we're doing is we're going to send this as a response to their message in a thread as opposed to a public channel, which it's just a little bit more subtle than like publicly blasting somebody for not following the process.

We do want it to be public so that people can see that there is a process and it needs to be followed. But we don't need to do the main channel, at user put them on blast kind of thing. So we're gonna grab the thread timestamp and there are two places that can come from.

If you are already in a thread, there's gonna be the parent thread timestamp, and if you are replying to a message with its first reply, you're just gonna use the message timestamp itself. So we're gonna check if it's in a thread first, that's payload message thread timestamp.

And if that doesn't exist, we're gonna get the payload message timestamp. With those details, we can send our message, so we're gonna await Slack API. We're gonna use the chat.postMessage, and what we wanna send is the channel, so that's where it's going, the thread ts, so that it goes into the right reply, and then we need to set up our text.

And so this is going to be just a nudge to remind somebody that like, hey, you should use the process. So we'll say, hey and tag them and that's their user id. And again, these pointy boys are a way of Slack letting you link in a not markdown, but they call it markdown way.

And we can say an opinion like this one deserves a heated public debate. Run the and then we wanna use some backticks here so we're gonna escape them and we'll say slash food fight. Wanna make this wrap. Food fight slash command in a main channel to start one.

And I call out main channel because slash commands don't work in threads, which is a little bit of a pain. And it's a bit confusing because like the "giphy" command does work in threads, but that's a special case that Slack. They just decided that that was the way things were gonna be.

There's no way to make your app work in a thread so you just have to nudge people to go back to the main thread. If they do try to use it in a thread, it will tell them that. So it's not a big deal, it's just a thing to be aware of.

So I'm gonna save this, And now if I go back out to the Slack instance, I'm gonna refresh by hitting Cmd+R, because sometimes it takes a second for this to load. I have two of these for reasons I don't understand. But the one that we just added is the top one here, and that is going to show our message.

So it created a thread, it tagged the person who made the request, and it gives them a next step. So this is a nice little process nudge that gives people a clue of what they should be doing next, right? So again, you're just trying to take that mental burden, that emotional burden off of somebody of having to go in and be the process cop.

You want the bot to just gently and very uniformly, same treatment for everybody, remind them that processes are processes. And this is the way that work gets done here.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now