[, Music, ], hello, everybody and welcome to another slack bot tutorial. So in this video, what we're going to be doing is actually filtering our messages to make sure there's, no inappropriate words inside of them, and then i'll, be showing you how we can first of all Delete those messages or how we could actually reply to them in a thread now.
This will also explain kind of what i did in the previous video, where i was updating the last message. I realized that i didn't, go into enough depth on that, and it might be slightly confusing as to why i was actually able to update that specific message, so that hopefully should explain that so anyways let's get started the First thing that i want to do, though, is just modify something quickly related to what we did in the previous video, so here when i say if text.
lower equals equals start, what this is saying essentially, is every single time. I type start into the chat i'm, going to send a new welcome message. Now i really don & # 39. T want to do that because i don & # 39.
T want to have the situation where people are just spamming. Start start start start all the time and then getting all these messages popping up. In fact, let's. Have a look at an example here i'll, bring up my uh slack thing.
Let me make sure my bot's, actually running uh yep. It is okay and let & # 39. S have a look here. So if i type start in the chat - and you can see what i was doing before - i was just messing around here - and we have a look at our dms - we get this message popping up right as it should now.
Let's. Go back to start again or to test again and type start one more time, and we should get another dm popping up and we do now look what happens when i react to this top message up here. So if i react to this, one notice that this message actually doesn't get updated with the check mark the bottom one does.
The reason for that is that this message at the bottom is now the current one that we're. Storing in our memory in that dictionary that we had this, one gets tossed away because we overwrote it right, which means when we react to any of the message really in this channel it's, going to update the most recent message that we sent.
So we might not necessarily want that to happen, so i'm just going to make it so that we can't type start again and have another message come unless this one is gone right. Unless we've, you know done it.
So you'll, see how this works. That's, a poor explanation, but it's, pretty straightforward, so right here where it says if text.lower equals equals start all. I'm going to do is inside of here. Actually i can do it inside of send welcome message that might make more sense is make sure that we haven't already sent a welcome message all right.
So what i'm going to do here is i'm going to start by writing this condition. So i'm going to check first of all to see if the channel is inside, of our welcome messages. We have that condition here right, but i want to check that beforehand, just to make sure that this is actually going to make sense and in fact i can actually move this condition all the way up.
I guess uh yeah, so let's. Do that let's say if channel is not in welcome messages, then go ahead and add the channel in now. The next thing i will say, is: if user not in welcome messages channel, then we can go ahead and do all of this now, in fact, what i'm actually going to do is reverse this condition.
So i'm, going to say: if user in welcome messages channel, then we will return. So what this is doing now is first checking. Okay, you know is the channel currently in our welcome messages. If it's, not, then we're, going to go ahead and add it like.
We did here same thing that we did before and then i'm going to say: okay, well is the user currently in the welcome messages channel now we won't run into a problem here, because if the channel didn't already exist, we would have added it and in that situation we won't return, obviously right because the user won't be in the welcome messages, but if the channel already existed, obviously this condition won't Happen and then we'll check if the user is already in the channel and if they are, that means we already sent them a welcome message.
So we'll, simply return and just escape from this function. So anyways let's test this out, i feel like i might have messed up yeah. I got to rerun the bot here, uh. Oh, what's wrong with this, so i've messed up some indentation just going to move that if statement forward one okay, let's run this and now let's, see what happens so remember that Since this is all running in memory, every time we reload the bot technically, we're clearing that entire dictionary of welcome messages, but let's type start! Let's, see our dm come in great okay and now let's.
Go back to test, let's type start again and let's, see if it sends us a dm it. Doesn't, look like it's, sending us one so awesome! So now let me react to this. There we go. It should update the message and we're good to go now.
We could make it that once you react to this message, we can send you another one, but really once we & # 39, ve sent the user one welcome message: we don't really want to send them another one right.
Okay, so perfect! That's. What i wanted to do for that now. The next thing i'm, going to show you how to do is how we can look in a user's text so whatever they're sending to us and make sure that they don't have Any like bad words or prohibited words.
Now i'm, not going to type any swear words out in this tutorial, just because youtube want to be careful with demonetization, but you get the point. You could obviously type a swear word in place of what i'm going to do here.
So i'm going to make a list at the top of my program here i'm, going to call it bad words. I'm, going to set it equal to a list and i'm just going to put a bunch of bad words in lowercase that i don't want to appear in my program.
So in this case, i'm, going to say bad words equals like hum. I don't, know just a random one. We'll, make no another bad word and let's. Make tim another one, and you could put a whole phrase in here if you wanted to anything, you put in this list, will make sure it's not contained in the message uh before we actually allow that message to go through or we delete The message, or whatever we're, going to do with that all right, so bad words hum no tim.
Now, what i'm going to do is set up a function that can look through any string and tell me if there's any of those bad words inside of it. So i'm, going to say, define check if bad words we're, going to take a message like that, and then we're going to do a few other things.
So the first thing i'm going to do is make this message all lowercase just so that if they were to type like in all capitals, no we would catch that and we would remove that. So just make sure all of the words inside of here are lowercase, because we are going to be converting the message to lowercase.
Of course you could reverse this, make the message uppercase and make the words uppercase as well all right, so message equals message.lower. Now the next thing i'm going to do is something weird that you probably haven't seen before, but i'm going to remove all of the punctuation and special characters from this message.
The reason for that is i don't want someone to be able to get away with doing like h, h, dot, m dot right, something like that. If they're gonna do like you know like an s in between that's. Fine like i, can live with an extra character.
We could try to look for that, but i'm just gonna try to avoid the fact that someone could do like you know like an f star star, star right. If we were going to look for that word. So we're, going to remove all of these special characters, or you know if they did something like this or if they had.
Like. All of the you know special charac, like letters in between there, we're just trying to avoid that. So to do this, what i'm going to do is say message: dot translate. This is a somewhat new function.
I believe in python, or at least there's, a different implementation now in python 3. and i'm going to say, str dot make trans. I'll, explain what this does in a second empty string: oops empty string, comma, empty string, comma string, which we're, going to import from above in one second and then dot punctuation.
Now, string.punctuation, i believe, is simply a list of all of the punctuation characters so rather than us having to type it out. This just gives us access to that. If you want to see what it's like just print string.
punctuation and you will see - but to use this - we need to import it. So i'm going to go up to the top of my program here. I'm, going to say import string like that, okay, so import string, okay. So what this line is saying right here is we're, going to translate the string using what's, known as a translation table? I believe that's, what it's called the table essentially will take a uh, a bunch of letters and map them to something that we want to replace them with.
So this is actually a cool thing. So if i had like string dot make trans, what make trans is doing is making a translation table for us. What this is actually going to be is a dictionary and the dictionary is going to look something like this.
Like the letter a goes to none, which would mean any time we see the letter a in the string, replace it with none. Now, if i had b and i & # 39, ve mapped that to c that would mean any time we see the letter b map that to c so what this does is construct this table for us or construct this dictionary for us make trans and keep In mind, str is a key word in the language right takes an in string and an out string.
So these strings must be the same length and what they mean is okay. If i have a as one argument then i have b is the other. Whenever i see a map it to b, if i had ac and bb, that would mean whenever i see a map it to b.
Whenever i see c map it to b. I picked bad letters for the pronunciation there, but you get the point that's, how that works now the last argument, which is the only one we're, actually really passing in here, simply says: take everything in this everything in this String and map this to none, so that means that all of the punctuation characters, if we see them, we will replace them with nothing which just means we will remove them.
Hopefully that makes sense again. You can look up the syntax if that's unclear this story is not a list. This is actually just going to be a string which makes sense considering this is a string and it will have all the punctuation characters, great, so pretty straightforward.
Now, what we're going to do is say return any now. What i'm going to do inside of here is check if this message contains any of our bad words, so this is pretty straightforward. I'm, going to say word uh, oops yeah word in msg for word in bad underscore words.
So what i'm doing is using a comprehension here and i'm going to say for every word. In the bad words list, let's, see if that word is contained inside of message and of course, message is going to be translated here, because we called message.
translate and now i actually need to make this equal to message so msg equals message.translate, because This is going to return to us a string, not change the string in place. Hopefully that's, making sense.
We translate the message we made a lowercase, then we're checking if any of our bad words are inside of the message now any. What this does is tell us if there is any element in this list. That is true, because what this will do is make a list that will be like true false, true false for all of the bad words.
If there is at least one true inside of this list, then any will return true. So there you go learning about some advanced python syntax. We can kind of kill two birds with one stone, slack tutorial and more advanced python, stuff anyways.
That makes sense. So now let's, go down to message and what we're. Going to do is just pass our message to check if bad words and if we see a bad word, i'm, going to show us how we can reply to that message and be like hey.
That was a bad word. So let's now go under under here. If text dot, lower equals start and let's, say l. If text dot wise yeah, we don't, do text all lower, let's, say l. If and then we're going to say check if bad words that function we just made, we're, going to pass text to it, and this will tell us hey.
Was there any bad words or was there not? If this is true, then what we will do is respond to this message and just tell them hey. You know bad word so to respond, i'm, not actually going to use the post message command.
I'm going well. I am going to use the post message command, but i'm, going to use it in a way that we reply in a thread. So if i open up my slack here and we bring it over to the side - and we have a look at our channel, i can press reply right.
If i go here and press the reply button, i can oh that's share my bad. Where's reply i feel like that would be. Oh there you go replying thread. I can reply underneath. So if i said hello, then notice that it goes in a thread underneath that message, so that's, what i wanted to do so, let's.
Do that let's. Go here. We say lift check if bad words and then what we will do is say: client, oops, dot, post underscore or chat post underscore message, and then we'll, do the same things we've done before so we're.
Going to say channel and that's going to be equal to the channel id which we have up here and then what we need to do is actually get the timestamp of this message. So we're, going to say, ts equals event, dot get ts.
This is going to give us the time stamp that this original message that was passed in was sent at and what we're going to do. Is we're, going to say thread underscore ts, so thread underscore timestamp equals ts? This is telling us what message we should reply to and then finally, the text is going to be equal to what we want to say.
So i'm going to say that is a bad word or something like that, so that they know you know we didn't approve of this message. So let's, save that and what this ts is doing just so. This is clear. The ts is the timestamp of the message and the timestamp is like extremely precise.
I think it's down to like nanoseconds or something like that. So we can pretty much be guaranteed that never are there going to be two messages that were sent at the exact identical same time. So that way we can use the timestamp of a message like the id of a message and that's.
How we're referencing, this thread, so we know which message to actually post into now. I'm, not sure if there is a situation where you could have two messages that have the same timestamp. I don't think that's possible or something that we need to think about.
So just understand that we can use the timestamp almost like an id for the message and that's. What we did previously and why we were updating the timestamp of our welcome message so that we know how we can reference it once again, all right! So let's go and make sure that this is still running and i didn't mess anything up it's.
Not so i made a mistake, but that's all right. Now let's, go to slack and let's, see if this works. So let's type, a bad word. Let's, go no and let's, see. If we get a reply there, you go bot replies, let's, have a look.
That is a bad word awesome and there we go now. I'll quickly. Show you how we can delete that message so say. Instead of just replying to it, you actually wanted to remove it all right. So, unfortunately, guys after doing some digging, i just realized.
We're, not going to be able to delete other users messages right now, with our current permission level, and i'll talk about why this is an issue in a second. We can only delete messages that we sent ourselves so here i'm trying to delete a message that's, not ours, and i was getting this pop-up saying that you know you can't delete this message.
That's, the problem blah blah blah. So i was looking and trying to figure out why that was happening, and it turns out that, with our current permission, level of having a bot token, we just simply can't delete.
Other people's messages. Now it makes sense if you think about it, because as a bot, we really shouldn't, be able to modify and delete users messages, because in theory i could you know, make a user sound like they're, saying something that they Didn't really say, which could be quite dangerous if you think about it, so what we would need to do to be able to do.
This is add what's, known as a user token scope or add a user token right now. We have a bot token, and you can see these are all the things that our bot is able to do right and that works perfectly fine.
But one of the things our bot cannot do is delete messages unless we have this token. So what i wanted to do is go inside of here and make a user token, and then we were just going to replace our slack token that we set up previously with this user token and we just re-add all the stuff that we needed.
So, in this case, i've, add channel history that's. What i need to actually delete a message that's, the scope, but now just watch what happens when i try to add something in here. So let's, say channel and let's go.
You know, admin conversations right. So i'm going to try to add that says. Okay, you know you change the scope, reinstall your app! So let's. Reinstall - and this is what we get - this app cannot be installed on this workspace.
App with this features are only available to enterprise level customers. So i'm, not an enterprise level customer and i don't plan on becoming one. So unfortunately we can't use anything other than this plain admin scope.
Inside of this user token now i know this is kind of confusing, but here you can see that now we have two tokens, the oauth access token and then the user oauth access token. So what we were going to do is just replace this token here with this one in our code and then in theory, we'd, be able to delete it, but again, because i don't have the paid version of slack.
I guess i can't do that. So that is the explanation on why that doesn't work. You can reference the api documentation if you want to learn more, but i think with that i'm just going to end the video here i can't delete that message, so i showed you how we replied to one.
I showed you how we filtered out some stuff, and then we modified something from the previous video in the next video. What i'm going to be doing is showing us how we can actually schedule messages. So that being said, i hope you guys enjoyed if you did make sure to leave a like subscribe to the channel, and i will see you in another youtube: video