Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
About Slack's RTM API5:00 with Kenneth Love
Slack's Real-Time Messaging (RTM) API is just one of the APIs offered by Slack. This API lets you create bots that respond to messages and other events in the channels of your Slack group. In this video, we'll talk a bit about what all the API offers, what the bot we're going to create will do, and we'll get the bot user and its key ready for building the bot.
You'll need to get the slack-rtmbot project downloaded before you can follow along with this project on your own. You'll also need a Slack group you can add integrations to and then visit the
/apps/manage/custom-integrations URL for that group to create a new integration.
Hi everyone. 0:05 Slack has quickly become a popular tool for online communities and companies. 0:06 If you're like me though chat software is only as good as your ability to 0:10 bend it to your will. 0:13 Slack has a few different APIs that you can use for your tools. 0:15 But the most common one is the real time messaging or RTM API. 0:18 You might be thinking that an API that just deals with messages 0:23 wouldn't be the most powerful and you're right that wouldn't be, but 0:25 in slack almost all in app events are sent as messages. 0:29 So you can use the RTM API to trigger events when users create a channel, 0:33 change their away status or many other things. 0:37 In this workshop though we're going to focus on messages posted by users but 0:40 the tools and approaches we're going to use are applicable to all message types. 0:43 The RTM API is what most Slack bots will use because bots usually operate in 0:47 channels and have their own presence. 0:51 They show up as users for a lot of use cases bot users though don't have 0:54 all of the same privileges as regular users. 0:58 For instance bot users can't join channels on their own, 1:01 in this workshop we're going to give our bot two different abilities. 1:04 The first will be a trigger command to make the bot congratulate someone on doing 1:07 something awesome. 1:10 The second will randomly select a message and 1:12 change a random noun in that message to a word of my choosing. 1:14 Before we start you'll need a slack team that you can add bot users to. 1:18 So go sign up for one of those if you don't have one already. 1:21 Next I'll show you how to add a bot and we'll get started. 1:24 So before you can start on this whole thing you have to have a Slack channel 1:27 that you can add integrations to and then you'll have this little drop down here. 1:31 It's the same in the slack app and just on the slack website. 1:35 So, click that dropdown and then click Apps and Integrations and 1:40 you'll get this Generic App Directory page and there's some cool stuff in here. 1:43 I suggest looking through it but I want to build one of my own. 1:48 So, I'm going to hit Build and then I'm building something just for my team, so 1:51 I'm going to choose Make a Custom Integration and 1:55 then I'm going to choose Bots and I want to make new bot. 1:57 Now, all I have to do is give this bot a username, 2:00 our treehouse mascot is Mike the frogs. 2:03 So I'm going to call this one frog bot and I'm going to say add bot integration and 2:05 you'll see that slack is going to give me this API token and 2:10 you want to be sure and not share this API token with anybody. 2:14 So when I'm done with this workshop, I'm going to go and regenerate this one so 2:19 that this will not be the token that actually works. 2:23 So I'm gonna take this token and I'm gonna copy it. 2:26 Now, the next thing that you need to do I've already done this, but 2:31 the next thing you need to do is to go to this git hub repo, 2:34 there's a link in the teacher's notes. 2:38 And this is the python-rtmbot repo by slackhq, so this comes from slack. 2:40 This is their recommended way of building RTM or real time messaging bots in Python. 2:46 So you clone this library and then we need to configure it. 2:53 So just clone it and then inside of there, let's see. 2:58 I'm going to show you better. 3:02 This is it cloned and then inside of there, I have my virtual environment. 3:03 Which already have activated. 3:07 Now, I need to make one new file inside of here and 3:08 that file is called RTMbot.conf. 3:14 So I'm going to choose New File. 3:19 RTMbot.conf and so this is a COMF file I'm pretty sure this is in the YAML format. 3:21 That's what it seems to be it only needs two variables, 3:29 the first variable it needs is debug and I'm going to set this to true. 3:33 And the second one is slack token. 3:37 And this is the token that slack gave you for your bot. 3:40 Setting debug to true will make the bot crash. 3:44 If it encounters an error, if something goes wrong, it will crash and 3:47 give you you know output on what went wrong. 3:51 If debug set to false, it doesn't seem to crash and give you the output. 3:54 It just kind of ignores the thing that went wrong and and carries on. 3:57 And then your slack token of course is your slack token. 4:01 Okay so I've got that saved and now I need to install the requirements. 4:04 So there is this requirements .txt file 4:09 I'm gonna do pip3 install dash requirements.txt. 4:15 All right, so now I have several things installed here that the bot needs 4:22 in order to be able to run and if I run the bot it should be able to connect. 4:27 So let's try. 4:32 Let's see python rtmbot.py. 4:35 I need a colon right there. 4:42 And now if I come back over here to Slack, I should see the bot is connected. 4:49 There is a green dot. 4:54 All right, so the bots all set up and 4:55 in the next video we'll build our first plugin. 4:58
You need to sign up for Treehouse in order to download course files.Sign up