[, Music, ], hello, everybody and welcome back to the slack python bot tutorial. So in today's. Video! What we're going to be doing is actually sending markdown text and some more nicely formatted messages into our slack channel.

Now the specif specific example story that we're going to be doing in this video is actually sending a welcome message that has a task that the user needs to complete. So, in this case, we're, going to ask the user to react to a message, so you can kind of think of this as, like.

You know like well welcoming the user, giving them information about slack and the things that they can do. So we'll, say: hey, you know, react to this message and then, as soon as they actually make a reaction to that message, what we'll do is we'll change, the message that shows a check mark next To that task they needed to complete.

Now, of course, you can extend this and make this. You need to complete more tasks, but this will show you how we can modify messages, how we can handle reactions and how we can send mark down text. So let's, go ahead and get started.

First thing i'm. Going to do here is set up a class that can actually kind of handle this for us. The idea behind this is that we may have multiple users coming into the channel right, so we need to make sure that these welcome messages are working for each of these users right and then we're, going to send that welcome message as a Dm so a direct message i'll, show you how we can do that again.

The first thing that i'm going to do is create class, and i'm going to call this welcome message pretty straightforward. Actually i don't need the brackets there. Now. The first thing i'll do is i'll, define an init method and what we're going to need to take here is we're, going to need to take the channel and the user? The reason for this is that we need to keep track of what channel this welcome message was sent into and what user it was sent to as well now inside of here, what i'm going to do is: go self.

channel equals channel. We'll, say self.user equals user and we & # 39. Ll define a few other things as well that we're going to use. So in this case, i'm, actually going to show us how we can add an emoji as the icon beside our bot.

So we'll, say the icon. Emoji is going to be equal to in this case, robot underscore face now. If you're, not familiar, the two colons surrounding a word is the uh. Like i, don't know denoting the fact that it's an emoji, so you could put like you know a smiley face or blush blush face, or whatever the other emojis are inside of these colons, and then that would show up As an emoji inside of slack, you can test it out in slack and you'll see what the different emojis are.

Next, we're, going to say: timestamp equals blank. We'll update this when we send the original message. The reason for this is that, since we are going to potentially be updating this message, which will be stored in this class, we want to know what time it was sent at, so that we can then update that time later on to the new time that that Message was edited at if that makes sense.

Next, we're, going to say, self.completed equals and we're, going to go ahead and make that false. Now this is just to keep track of whether or not the user has completed the task. That will tell us whether or not we're, going to show a check mark or whether we're, going to show like an empty box.

Beside the task now above this, what i'm going to do is to find two variables that are going to be the markdown text. That's, going to be a part of our message. So i'm, going to say, start underscore text equals and i'm, actually going to make sorry curly brackets.

Here i'm, going to define the type here. As a section you & # 39, ll see what that does in a second, but you can have a bunch of different types for your. I don't, know sections, i guess or or the different parts of the message that you're, going to be sending so i'll, say type equals section.

Next, i'm, going to say text, and then this is going to be equal again to another set of curly brackets here inside of here, we're, going to define the type of this text by default. I believe it's like utf-8 or just text right.

In this case, we're, actually going to make this markdown to make this markdown. We're, going to do mrkdwn if you're, not familiar with markdown that allows us it's like what you would use on github to make a dot md file.

So, like you know your welcome file on github uh, you can add, you know, i guess what is it like: the pound sign and like a bunch of other stuff to the markdown file, so it's like formatted text. Next, what i'm going to do is say text now.

This is going to seem strange, but i'm just going to open up some parentheses here. Just so i can put this text on multiple lines. Important here make sure you don't separate these two things by commas: it's, just important that you don't, do that, but you'll, see why so i'm, going to start By saying, welcome to this awesome channel and we'll do backslash n, backslash n, so a new line two times and then in bold.

I'm going to say so that's. Two asterisks for bold and markdown get started oops by completing the tasks all right, so that's, going to be our start text. Next, i'm, going to make a divider. The divider is simply going to be the curly brackets inside of here we're, just going to say type and then colon dividers.

This is another example, one of the types that we can use now. All of this will kind of be broken down when we actually make this message, which you'll, see later on, but just kind of follow with me. For now it's all going to come together next, what we're going to do is we're, going to say, define and then get underscore message.

What this is going to do is simply return to us. The message that's going to use this start text this divider and then something else as well inside of here for right now i'll just return, and i'm just going to return that for right.

Now we'll change that later on next, we're, going to make another method, which is an underscore method, just meaning. This is a private method and that this shouldn't be called from outside of the class.

That's. The convention in python, you just prefix it with an underscore. We'll, do define underscore get and in this case underscore get reaction underscore task. So if you can't already guess by the name, what this will do is give us the portion of the message that will say: hey.

You know react to this message before you can move on right so inside of here, what we're going to do is we're, going to say, check mark equals. Now this is going to be an emoji. The default emoji is going to be a not wa.

It's. Gon na be white, underscore large. Sorry white underscore check underscore mark so that's. What we'll do and then we'll, say if not self dot completed, we & # 39, ll, say check, mark equals and then again inside of colons.

In this case, we're, going to say, white underscore large, underscore box, uh, sorry, not box square, very close. So this is just going to be, like you know, a little box and then the check mark will be replacing that box.

If this task is complete next, what we'll do, is we'll, say text equals an f string inside of the f string? We're, going to say, check, mark that's, going to be the prefix and then we'll say in bold react to this message.

Exclamation point and finally, we're, going to return. What's going to seem kind of strange? It's, going to be a list inside of this list. We're simply going to have one element. This is going to be type colon, section, comma and text colon, and then we need the type.

In this case you can guess. This is going to be marked down again because we're using those asterisks for the bold. So we & # 39, ll, say markdown and then finally comma text, colon text, i understand the syntax - is a little bit confusing, but just kind of follow along with it.

This is the basic structure. I will leave a link down below someone remind me, because i'll, probably forget to how you actually find what this should look like all right. So now that we have that, let's, start building out the final message which is going to use all of these different things, and then we'll, send it in the channel and see how that works.

So the first thing we're going to do is we're, going to pass a timestamp? The timestamp is just ts right, so we're going to say self.timestamp. Next, we're going to pass the channel, so this would be the channel that we want to send this message in.

In this case, we're, going to say self.channel. We're, going to pass the username. Now we can set this username to whatever we want, so it doesn't need to be the same as our actual app name. We can make this whatever we want.

So i'll. Just say you know: welcome robot, because it's going to have that robot emoji and then next we will say: icon underscore emoji that will be equal to self dot, icon emoji. And finally, we are going to define the blocks that we want to send.

Now again, you'll, see how this works in a second, but all of these things inside of here are actually keyword, arguments to the chat.postmessage or chat underscore post message. So you see here we have channel, we have text.

Well, if you have a look here, we have channel. We have a few other things that we could actually use just as regular arguments in here. Right like i could go icon underscore emoji and then i could pass in an emoji, but you'll, see how we use that in a second just wanted to remind you of that.

So we'll, say blocks now inside of here. We're, going to say self dot, start text, comma self, dot, divider, comma, asterisk self dot underscore get reaction task now, technically, what we could do is just remove the list here.

In fact, let's. Do this to make a little bit simpler, then we can remove this asterisk and that's, going to prevent me from having to explain why we needed that asterisk there - okay, so hopefully you caught that sorry! I just removed the list from here and just removed the asterisks and had self.

reaction task now, of course, you can assume the blocks are going to happen in the order that we pass them. We're, going to start by doing the start text, then the divider and then the reaction task great now that we have that, let's actually send that into a channel.

Now we want to send that only when the user sends us a text that says start, so we could technically send this on like when the user joins the server. To do that, you would need to link up another event here.

So you'd. Have an event for like on team join or i think that's. What is team join? I believe it's, team hyphen join, and then you can send that message. But since i can't rejoin this, because i'm like the owner of this workspace, i can't, show you how that works.

So i'm just going to show you how we can send this. If they send text, that is a specific thing. So, first of all, this is going to seem weird, but what i need to do is make sure that the user underscore id does not equal.

None and bot id does not equal user id. So what this is going to do is just make sure that the user id is not none and then again that the bot didn't send this message before we go ahead and do whatever is inside of here.

The reason for this is when we update a message which we'll, do later on. There is technically no user for that updated message, because the bot's. Updating it. I don't know why it works like that. I would hope i would have assumed that the bot would be the user, but it breaks in that situation.

So we just need to add this in next. What i'm going to do is i'm going to say if text dot, lower equals equals, and then inside of here i'm going to put start, you can put whatever you want, but i'M just going to have start be the string that we put in there to do the start message.

Then what we will do is well. We can create a welcome message and we can send that now i don't want to just do that. I want to do a little bit more of a complex setup here. The reason for that is because i need to keep track of all these welcome messages, because we could potentially have a bunch of them and i need to update them later on when the user reacts to them.

So what i'm going to do is make a function. I'm, going to say, define, send underscore, welcome, underscore message like that, and this is going to take a channel, and this is going to take a user. What we'll do inside of here is we'll, say welcome equals and we & # 39.

Ll, make a welcome message. We'll pass. What was the order, the channel and the user great? So we'll, pass channel and then comma user, and then we can actually go ahead and send that message as well as add it to something else.

So we'll, say message: equals welcome dot, get underscore message. Remember that's, the method we wrote right here so that'll generate the message for us. Next, we'll, say response is equal to client dot, chat, underscore post message.

I guess i might as well just type it and we & # 39. Ll do astrix asterix message now. If you don't know what this is. This is um the unpack operator for dictionaries. What this will do, essentially, is take all of these values and it will pretty much rewrite them, so it says: ts equals self.

timestamp channel equals self.channel. So if we were to kind of you know decompose this, it would go like ts equals and then whatever that was equal to and then channel equals, so on so forth. So all of the keys are the left-hand side, keyword, arguments and the right-hand side is all the values.

Hopefully that makes sense. Don't worry if it doesn't, but we're just going to pass all those values in here. Next, we're, going to say: oops welcome, dot timestamp equals, and then we're. Going to say response, which is going to be the response from the server with information about our message and in this case we & # 39.

Ll do ts all right, so we should be good with that and then the next thing we're going to do. Is we're, going to set up a dictionary here outside of message counts? Now i realize i probably should have put this full screen.

It looks a little bit nicer, but let's, go welcome, underscore messages equals and then an empty uh dictionary, and what we will do is actually add into this dictionary, the channel and the user that we sent this welcome message to so we're going to say if the channel not in what is this? What did i call this welcome underscore message and let's.

Make this welcome messages, so a plural there. So, if channel is not in welcome messages, then we will do. Is we'll, say, welcome, underscore messages channel equals another dictionary, then we will say: welcome underscore messages, channel user equals and welcome all right.

So hopefully this makes sense, but essentially we're, going to store all of the different channels right. So all the different channels, we would have sent this welcome message into and then for each of those channels we will store each of the users that we potentially sent.

This welcome message to right. So we have each channel, which is kind of one main key here, and then all of the users inside of that channel will have their own welcome message stored here again. We need that for when we react to the message, so we can update it and um like rewrite the message like it's like editing the message right.

Okay, so we have all of that. So now we can use, send welcome message. So let's say: send underscore welcome message: let's, pass the channel and let's pass the user. Now i will show us how to send this as a dm for right.

Now it's, not going to go as a dm, but that's. What we can do next and sorry this needs to be channel id and user underscore id okay. So i'm, hoping this is going to work. Although i don't have too much confidence, i may have messed it up.

So let's run this here and come on. Okay. There we go, let's, go over to slack and you can see my youtube. Bot is here: let's type start and there we go. We say: welcome to this awesome channel the emoji for some reason: isn't working.

I'm, going to look into that in a second and then get started by completing this task. You see the little box and it says, react to this message all right. So let's, see why that emoji isn't working. I have a feeling it's, probably just a stupid spelling mistake all right so guys i'm gonna be honest with you.

I have no idea why that isn't working previously. That was working for me, so i'm gonna assume for some of you guys that that does work um if it doesn't, please let me know i'll, look into it and see if i can fix That, but essentially this icon should have changed, and this name should have changed again.

No idea why that's, not working, i've, looked at it for a while and can't get it. So i'm just going to move on because it's kind of a waste of time. If it's, just not going to work all right. So now what we're going to do is make it so that, rather than just sending this in the general chat, we & # 39.

Ll. Send this as a direct message. Actually, the first thing we need to do to do. That is add the permission to send a direct message, so we need to go to oauth and permissions from our api panel. Here we're, going to go to scopes.

You can see i've, been trying to add a few of them to get this to work. I just couldn't, get it to work. While we're here, we might as well add reactions read because we're going to need that and then we should add im and i think it's.

I am right, yes, start direct messages with people. I am right that's. What we want so add those two reactions read. I am right and then we need to reinstall our app. So let's. Do that we will allow our app to have those permissions.

Now we can go back here to our code and we can modify inside of message here so that, rather than sending in that default channel, we will send actually back as a direct message now we could send it to the main channel if we want - and i Already showed you how to do that, but to send a direct message is actually really easy.

All we need to do is simply change the channel to be at so the at symbol, and then the user's id. So what i can do here is instead of putting channel id and send welcome message. I can put an f string and put an at sign, and then i can just embed inside of here, the user id and what you'll notice now and just keep in mind.

I already tested this so that's. Why? You & # 39, ll, see this coming up in a second uh. What will happen? Is we'll just send a direct message to this user. So let's, go back to slack notice. This is the one i was sending before.

Let's, go back to test here. Let's, go start like that and let's. Give it a second go to youtube bot, and we can see that it sent this message now. I think it was sending it twice. I think i had some like caching issue so, like the request just came in uh, but let's.

Try it one more time and just make sure it only comes in once boom we get the dm and there we go. So this has been long enough for this video in the next video. I will show you how we can handle reactions, uh and then yeah how we can.

You know, update that message based on if the user reacts or if they don't

OUR SOCIALS: