Python Slack Bot Tutorial #5 - Reaction Handling

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

Handles reactions with a Slack bot using Python and the Slack API

Full Transcript

[Music] hello everybody and welcome back another slack python bot tutorial so in today's video this one's going to be a lot shorter than the previous ones or fingers crossed we'll see how many mistakes i make what we're going to be doing is handling reactions so essentially when someone reacts to a message how can we handle that and then how can we update that welcome message that we sent previously so let's go ahead and do that the first thing we need to do is go over to our console here so our slack you know api whatever and just add the reactions read permission to our bot scope now i think i showed you how to do this in the last video we added the i am right and the reaction read just make sure you add that in here you may have to reinstall the app again once you add that all right so now that we have that what we're going to do is set up a very similar thing to what we did previously with this message so we're going to say at slack underscore event underscore adapter dot on now you can guess what this one is going to be i'm going to trick to you it's going to be reaction underscore added so this means that we added a reaction now we also need to go back sorry i forgot about this to the slack api and we need to now subscribe to that specific event which i think i actually already did but i'll show you how we change that so let's go subscribe to bot events and yes i already added that but you guys can just say add bot user event and then add reaction added like that we could also add reaction removed if we wanted to i'm not going to do that right now we'll just add reaction at it okay so let's go back now and let's go inside of here let's say define reaction you can call this whatever you want inside of here we're going to take a payload and we're literally just going to copy the first few things we have up here so the event the channel id and the user id all right so event channel id and user id now that we have these things what we're going to do is we're going to say if channel under score id not in and you can guess what this is going to be but it's going to be welcome messages we're going to return so just make sure that when we add this reaction we're actually uh checking to see if it is not if the reaction that we added was in the channel that wasn't where the welcome message was sent then we're not going to actually update the welcome message right and this actually reminds me the payload that's sent here is a little bit different than what was sent previously inside of onmessage so what we're going to have to do inside of here is just changes a tiny bit so rather than having channel id event.getch it seems a little bit weird but we're going to say event.get and inside of here we're going to say item comma empty dictionary and then dot get channel now the reason for this is just because again this payload that's sent back looks a little bit different than the one that's up here so this is just how we can handle that if you want to have a look at the payload you can just print it out and you'll see why i'm doing this but event.getitem the item is the item that we added the reaction on right and then we're getting the channel that that item was in so that's the only major change here but that still is equal to channel id then after this we're going to say welcome equals welcome underscore messages channel and then inside of here we're going to go user underscore id now we might need to change this actually but we'll look at it for right now and sorry this isn't channel my bad we do need to change this to channel id okay so now that we have the welcome message what we're going to do is we're going to say welcome dot completed equals true uh come on completed equals through give me the auto-completion there because once we reacted to the message well now our task is complete right and that will update the message automatically inside of the class if you have a look here where we have the check mark right that will change it now so it actually is a check mark and it's not the white large square all right anyways moving on what we'll do now is we will resend or update this message so to do that we're going to say message equals welcome dot and then get message like that this will give us the updated message that now has the check mark instead of that box what we will do after that is we'll say updated underscore message equals client dot chat underscore update and i don't think it's update message i think it's just update and then inside of here we're gonna go asterix asterix and message all right and then finally we will go welcome dot timestamp equals and we'll change that to be updated message ts all right so let's have a quick look here let's go to slack and oops not there let's go into our channel let's go into test let's send start and let's see if we get the thing popping up what is going on what's wrong well it would help if i ran the server i guess it crashed for some reason okay let's run the server let's go back to slack let's type start we see that we get this dm okay great now let's react to this message so let's give it a reaction boom and give it a second and something went wrong what ran wrong let's see here come on it's not working okay so i just messed around with this for a second and i realized what the problem is here so i've just been printing out and having a look at what the user id is and what the channel id is now the problem here is that what we are actually getting when we get the channel id is different from them what we've stored inside of these welcome messages right so again the reason for that is because what we stored inside the welcome message was simply this f string right which is the at and then the user id now that is different than the channel id now technically they represent the same thing and they'll point us to the same location which is the user's direct message inbox but since they are different i cannot simply check if the channel id is inside of welcome messages so what i can do here is kind of a hacky solution where i do a try catch so i can try to send this message and if it doesn't work just catch it and like cleanly return that would potentially work i also can just check if the at user id is inside of welcome messages so what i could do instead is do something like this if the f string of at and then user id is inside of welcome messages then we can go ahead and do that so let's see if this works first of all because i'm not actually 100 confident that's going to work just based on what's being returned to us here but let's try this so let's go and go do test let's type start let's see the dm come in and let's react to this message i just need to move this more on the screen so reaction let's add that and let's see if this changes no that does not change and let's have a look here what are we getting the problem here we get key error this uh so essentially what is this saying okay so i see the problem here i changed this but i didn't change this which is a big problem so let me just copy this string right here and just paste that inside of here and now this should work so this actually did work that's why it actually gave me that error because we advanced past the return statement so it did find that this user uh like the at user was stored inside of the uh what are we calling here welcome messages so now we can reference it from there and we should be good to go i will go over this after but i just want to make sure this is working before i mislead anyone anymore so let's go start all right let's go back here and let's give a reaction to this message and give it a second and of course it's not working all right so the error i just got there was telling me that the channel id was not found so i'm trying to send back into f at user id and this is not working for some reason there's something wrong with this slack api you know i hate to always blame the api but there's definitely something wrong here with this referencing so what i'm going to have to do is kind of implement some kind of hacky solution to fix this now what i'm going to do is just say welcome dot channel equals and i'm going to make this equal to the channel id before i get the new message what this will do is change the channel for a welcome message to be equal to the actual channel that this dm was sent in that's an id rather than this like at tag that we used before that should hopefully prevent the error that we had previously where we tried again to use this at user id to send the direct message or update the direct message again i don't know why that's not working but this should fix it now a lot of you might be asking well if we do that then we get to this point won't this always skip because we would have changed the channel to not be the at user id now it's equal to channel id well you're correct in that situation but it doesn't matter because we've already reacted and edited the message so it shouldn't matter if we skip over this in fact it's almost better to just not have that key in the welcome message anymore so that we continue to skip over it hopefully that's making sense i really apologize if i'm confusing you guys but there's not really much i can do when this just isn't working let's go to test let's go to start let's type that in hopefully we get our dm well it would be helpful if i ran the server of course i'm just running into all kinds of bugs today so let's go back let's type start see our dm come in now let's react to this message so go react add the cool smiley face give it a second and finally we get to the white check mark popping up so that was definitely very frustrating again i don't know why that was happening but now you can see we edited this message and the time stamp of the message would have been updated in fact if i try to re-react to it you'll notice that nothing's going to happen the time stamp won't change just because again what would happen here is we're going to skip over this because we'll hit this block but no longer will the f user id be stored inside of welcome messages so we won't bother doing the rest of this anyways that has been it for handling reactions now there's more that you can do here but this is how you set up the event and this is how we can modify a message again apologize about the confusion this stuff happens i am human just like all of you and let me know what you guys want to see for the next videos i do not yet have them all planned out as always if you enjoyed make sure to leave a like subscribe to the channel and i will see you in another youtube [Music] video you

Original Description

In this python slack bot tutorial I will be showing how to handle reactions with a slack bot. We will be sending a request to our python web server which will handle the slack reaction and respond with an appropriate action. 💻 Ngrok Download: https://ngrok.com/ 📖 Full Series Code: https://github.com/techwithtim/Slack-Bot/blob/main/bot.py 📕 Slack API Website: https://api.slack.com/ 📗 Slack Website: https://slack.com/intl/en-ca/ 📚 Playlist: https://www.youtube.com/playlist?list=PLzMcBGfZo4-kqyzTzJWCV6lyK-ZMYECDc ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/pr2k55t 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.to/3hpSprv 🎤 Main Microphone (Rode NT1): https://amzn.to/2HrZxXc 🎤 Secondary Microphone (Synco Wireless Lapel System): https://amzn.to/3e07Swl 🎤 Third Microphone (Rode NTG4+): https://amzn.to/3oi0v8Z ☀️ Lights: https://amzn.to/2ApeiXr ⌨ Keyboard (Daskeyboard 4Q): https://amzn.to/2YpN5vm 🖱 Mouse (Logitech MX Master): https://amzn.to/2HsmRDN 📸 Webcam (Logitech 1080p Pro): https://amzn.to/2B2IXcQ 📢 Speaker (Beats Pill): https://amzn.to/2XYc5ef 🎧 Headphones (Bose Quiet Comfort 35): https://amzn.to/2MWbl3e 🌞 Lamp (BenQ E-reading Lamp): https://amzn.to/3e0UCr8 🌞 Secondary Lamp (BenQ Screenbar Plus): https://amzn.to/30Dtafi
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →