![Building a Slack Chat Bot](https://static.frontendmasters.com/assets/courses/2023-08-25-chat-apis/thumb.webp)
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
[00:00:00]
>> So now that we've got the 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 it's pretty standard for somebody to kind of fly in from a meeting and say, hey, I don't really like chocolate.
[00:00:27]
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?
[00:00:50]
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.
[00:01:04]
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.
[00:01:23]
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.
[00:01:42]
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 in shortcuts menu again, and we're gonna create a new one.
[00:02:02]
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.
[00:02:15]
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 on why you would use this context menu.
[00:02:38]
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.
[00:03:06]
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.
[00:03:24]
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.
[00:03:45]
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.
[00:04:01]
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.
[00:04:27]
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 the main channel, at user put them on blast kind of thing. So we're gonna grab the thread time stamp and there are two places that can come from.
[00:04:43]
If you are already in a thread, there's gonna be the 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.
[00:04:59]
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.
[00:05:22]
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.
[00:05:45]
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.
[00:06:13]
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 Jiffy 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.
[00:06:28]
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.
[00:06:40]
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.
[00:07:05]
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.
[00:07:26]
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