Python Slack Bot Tutorial #4 - Markdown Messages & Emojis

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

Key Takeaways

This video tutorial covers creating a Python Slack bot that sends markdown messages and emojis, handles user reactions, and updates messages accordingly, utilizing tools like Slack, Python, and Ngrok.

Full Transcript

[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'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'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 gonna 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'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'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'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'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'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'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'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

Original Description

In this python slack bot tutorial I will be covering markdown messages and sending emojis in the context of a slack bot. We will be learning how to handle users reactions and update messages based on user reactions to our slack messages. 💻 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/twt 📝 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 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

This video tutorial teaches how to create a Python Slack bot that sends markdown messages and emojis, handles user reactions, and updates messages accordingly, utilizing tools like Slack, Python, and Ngrok. The tutorial covers various concepts like retrieval augmented generation, fine-tuning, and natural language processing. By following this tutorial, viewers can learn how to craft effective prompts, handle user reactions, and update messages in a Slack bot.

Key Takeaways
  1. Create a class to handle welcome messages
  2. Define an init method to take channel and user
  3. Add an emoji as the icon beside the bot
  4. Define a few other variables to use
  5. Send a welcome message as a direct message (DM) in Slack
  6. Use markdown for formatted text
  7. Define a section type for a message
  8. Create a divider in a message
  9. Check if a task is completed and display a reaction
  10. Use f-strings for string formatting
💡 To handle caching issues and prevent duplicate DMs, it's essential to update message handling to include reactions and message updates.

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →