Basics of creating a Telegram bot (with code/Python)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and hello everyone welcome back to another tutorial so in the past i've done or i've made a video where we did create a basic bot and up until today or up until this day i should rather say to be better english my bot still runs so if i were to go here my bot here it still runs and it still has all the basic functionality that my bot used to have if we say slash we can say all of the things here the problem is it's just so basic and i really really want to just improve it to be better so today i'm going to show you how to create a telegram bot but this time with code we're going to be using python and a module called pi telegram bot api it may sound like a big and difficult name but actually it's rather simple i'd always say as easy if not easier than the scored pi if you have ever used this chord pi but let's get started so what do you need to know just a basic python would probably be fine but yeah that's actually all you need to know and you just need to create an account on telegram so you can actually use it you can also be on your phone i'm just going to do it here so i'm going to have to display my phone here or record the screen or whatnot okay so step one is to actually install the library it's simple it's pip install pi telegram but api now i believe i already have mine installed so it will probably not ring or might reinstall it but it will probably not actually continue installing yeah requirements already satisfied so yeah next up we want to actually create a bot so we have to go and find bot father so but father right here here is actually already in what you see you might see this api key don't worry this bot is going to be deleted anyways so even though you are seeing an api key here it's going to be deleted so it doesn't matter if you see this api key or not anyhow we want to create a new bot so you slash new bot if you are on a normal phone you can just tap slash help and this will give you all of the things and you just click on slash new bot and this will create a new bot for us here you can enter the name i'm going to call this youtube example bot with a smiley face and now here we have to kind of give it like its username this is a very special username and i believe all usernames has to be unique and this username has to end in underscore bot to signify that it's a bot so i'm going to say whitey example but smiley because it has a smiley face and then but think what you're going to call this here because i don't know if you can change this but this is just as examples i'll show you also creating a bot is completely free so you don't have to worry about it once you've done that you will have your api key which is very important you do need your api key copy this and don't ever give your api key to anyone if someone sees your api key they can and probably will try to access your bot and modify it in ways you don't want it anyhow now you could just say ap i key and you can make it equal to this however or let's just you know you make it easier this however i would not recommend this if it's just going to be in your main code it's very dangerous especially if someone can see your code because then you can just be like oh there's the api key so what you do is you create another for file a powerful file i'm going to call my constants geez there we go dot p y of course you can maybe call yours like secret python or something like that anything that might suit your needs i'm just gonna call it constant and then you maybe let's say you use github just tell it don't push this to anything save it and remember just to keep this out of any github repos and stuff like that so this file should not be shared anywhere people should not be able to see what goes inside of this file anyhow we set the api key in here and now we can just say from constant import and here we can just say api key there we go and now we can use this api key here next up we also need to actually import this module we installed so to do that it's rather simple it's just import telebot this is for telegram bot great so now we have our telegram bot imported and we have our constant api key imported as well and since we have this our bot should now be working now you can just click here to go to your bots i'm going to click there and now here's the bot i'm going to say start and now we can make this bot do things so right here we can just start doing pretty cool things so i'm going to say bot you can call this whatever you want i just like to use bot to make it easy and we can just say that is equal to a telebot and inside here we can then pass in the api key so i'm going to api key right there and you can specify the parse mode and i'm just going to make that none i have no real idea what the sparse mode does i believe maybe let's say you pass in some json here and you can just say parse it as json or something like that i have no idea but yeah once you've done that you have basically successfully created your bot now we need to make our bot actually listen for messenger so when someone sends a message our bot should actually notice that message so we can then say bot dot pulling you can also pass in some values in this pulling right here but we're just going to keep it clean so it will i think every 20 milliseconds check if something has happened and yeah and everything can just go between these two just keeping simple and organized now take note everything i'm about to teach you can also be found online so here is the github repository so let's just go up and here pi telegram bot api by efferener i guess and here's also the normal telegram bot api which you can also find a lot of details about the bot and whatnot and yeah i'm going to close all of these to save some extra resources and now that we have that we can make our bot actually work so let's create our first message instance i'm going to say at bot dot message handler and in here we can specify the commands so this is just something you can wrap your kind of function with and yeah we just put this above every function that our bot should use for message handling and yeah so we can just say commands and let's say for example we want the command slash hello and slash help to do this so slash help and hello second we don't put slash in here by default it will add a slash for you when you send a message and in here we can just say define and we can call whatever we want i'm just going to say send help message this is just kind of how i like to name my messages but how what you name it doesn't matter i just like to say send to know that the bot is going to send something then what is its details so help and in the end message this to i don't know just so message and then we can just bring in the message although i like to go msg just to make it a bit shorter but you can say message as well both of them will do the same thing on the end of the day here we can actually say what should happen so i'm going to say bot dot reply to and this will make the bot reply to the person who sent that message and you'll see what i mean in a second and here we can just pass in message so it knows one message to reply to and then also the text that should send hello this is a test bot and this should be msg because i called it msg there we go so this is our first message so in basically 11 lines of code if you wanted you can probably shorten this to seven lines of code we have created our first bot that can do the very basics i'm going to run python and in main.pi and my band i got a little bit excited this should be telebot dot tele bot and take note of our capitalizations here and now if we run it we should be able to do it okay you already see anything here you could probably put a start message or something there just so you know it started but once we're here let's i say let's see like it doesn't have any pop-ups we'll have to add that by with both folder but anyways you can say help the hdb not help and now it will send us hello this is a test bot and as you can see it replied to my help message right here that's what the reply to meant so basically what we did is we said okay but here's a message handler if the help or hello command is sent then execute this function then it says reply to and then we pass in the message we get here because this message is this right here and we pass it in so it knows which message to reply to and then we say what it should say so this is the text that you'd send take note that it needs to have a slash so if i say hello as you can see nothing happens but if i say hello something will happen so one thing you do have to take note of is this message variable we take in here you would have noticed on the brow on the websites i showed you that there was a bunch of variables and stuff you could access and all of these variables and stuff can be accessed by the message or not all of them most of them can so if we print out msg then this will send back a ton of the data the message contains i'm just gonna restart the server here and now if i were to send here hello as you can see a ton of details get sent back to us that's this is everything what's inside of that message so here you can get like the content type of the message to the beginning to the from user to take note if you use javascript or something you might just use user but if you use python or this in particular this module it'll be fromuser and then here you'd have your user id the bot the first name of the user the username last name language code and just a bunch of details that can be useful depending on how you use it anyhow so you can just take note of that and of course you can access any of these things by just going dot elizabeth from user by just doing that and you can access it as well anyhow let's remove that and continue now let's say you want the bot to send something back or just send a message if the user sends a specific content type for example an audio or a photo or maybe a document or a sticker so i'm just going to copy this and here i'm going to create a new one here and this will be or this will contain the content types and here you can specify the content types for example i'm going to specify photo and sticker because they would be the easiest okay now once we have that we can send we can just change this to send content message and then bot.reply to and we can just specify the message again and here we can see that's not a text message anyhow we save that now we stop this and run it again and we go back to our bot here and i if i just send something normal it won't really care but as soon as i send something like a sticker for example so let me just click a heart here send that sticker and that is not a text message as you can see if i were to send an image let me just quickly find an image if i send this image and that is not a text message so you can also listen for specific content types and take note there's also a lot more of these that exist for example we have a reject six there we go and this is if you want to match specific rejects and we also have things like func so this will search for functions that are called so you can if this function returns true then something will happen i'll give you an example of the function one but i would give you one of the rejects one because i didn't know any rejects but i can give you an example of the function one and i could also show you about stacking these i'm going to copy this and i'm going to change this to commands and let's say let's say someone sends this command then the following should be said wow you're scary right now if i were to and i should probably just change this to send multi i'm just giving it stupid names but you shouldn't give your stupid names and then here we go if you run again if i say boo then we get wow you're scary but let's say you also want them to send this message if my specific user says something to them for example you might be in a group and let's say the user and it's just like a joke for your friends so let's say whenever timmy sends a message this bot should reply with wow that's so great or something like that now you can do something like that so when the user to me sends something so i'm going to copy this right here and i'm going to change this from commands to func and this is going to be taking a lambda function so lambda and i'm going to be taking the message that has been sent and this message is going to be or this is going to go msg dot from user so we're accessing one of the variables we can find here and you'll notice that let's actually resize this a bit here's the from user if we can find the first name here we can see that it's netsu or even the username it's netsudagnil so let's access this username and makes that whenever nature diagnose nature diagonals next to that and dagonal sends a message that it should say this then you can say dot username is equal to netsu tag nail so whenever this lambda function turns true this should execute and here we're basically getting the same message object as we're getting passed in here and it's just checking it is the username the same as this then do the following let's restart our server now even if i don't say boo if i just go low it's going to say wow you're scary or if i go go away i can reply while you're scary or i could even say slash boo while you're scary let's see what happens if we say hello then it says hello this is a testbot but it doesn't say you're scary so it will basically execute the first one fine but you could probably also just copy this and paste it here as well if you wanted to so just so you understand what happened here is that whenever the user of this username netsuite diagno which is me sends a message then it's a replay of value scary or when they say slash boo now let's try to send in a dice you'll notice that if we were to let's say dice here then we can throw a dice and you can give us like a little number here so we got four now we can actually do this with the bot as well we can say send dice and now this dies all it needs is the chat id to which sender dies too now you can get the chat id by saying message or we can say chat id actually just specify and we can say message dot chat dot id and of course you can go check here and you'll find the chat id here now if we were to run this now whenever i send something or even boo or whatnot it will throw a dice so hey and now as you can see it first dies lol lol and there we go so it threw three dices we got a free we got a five and we got a four nice so that's pretty neat we'll also make the bots in the middle i'm just going to comment this out so it doesn't really send the message this message every time i try and do something but we can also send a message whenever a user edits their message so i'm going to copy this and i'm going to delete this and we're just going to change this to edited message handler as you can see right there and we can change the command here to let's say noise and then we can just instead of sending a dice we're gonna actually send a normal message so without replying like we've been doing this entire time we can send a normal message to do that we can just say send message i'm gonna pass in the chat id and then the message we want to send so then we can just say text here and then wow i saw what you did so whenever they edit the message then it will say wow i saw what you did and we just changed the send edit message and here we can say python main.pi and now let's say i say lol doesn't say anything but now i go here and i say edit message and now instead of saying lol i say slash noise and boom wow i saw what you did so you can make it that's this message will be sent because let's say they mistyped the command now you can make it that this message is sent when they edit this as well so you can just add an edit message it is message handler here as well by helping hello but it's not necessary but it's just kind of cool you can also change the keyboard the user has which is really really neat so i'm going to scroll down a bit and i'm going to copy this and actually i want to copy this one up here because that's kind of what i want at the moment okay now here i'm going to change the command to game so let's say i want to play a game and now we also want to import so from telebot import types okay now that we have imported types we can change this to send game message and we can remove that and here we can create a markup so this is what it's called when you change their keyboard and we can make that equal to types dot reply keyboard markup so this will basically allow us to kind of set how the new keyboard looks in this case i want to give it a row width of 2. so row with would basically be able to contain two items in it all right now that we have that we can see btn1 is equal to types dot keyboard button and now we're changing the button that we want to have on it and here i can say let's say how do you throw dice again throwing a dice is boo okay so let's go slash boom so this will throw dice you can put whatever you want in here i just want to put commands in here in this case boo will do this and you can have let's yeah let's create three buttons now button two and button three we want button three to basically close the this keyboard right here now this button too will contain let's say help so let's say if they want some help and here we can say close okay cool now if we want to add these so it actually appears we have to say markup dot add and you have to add the buttons so b btn 1 btn2 and btn3 now we're adding it to the keyboard and let's just scroll down a bit and now here we can just say but dot send message and let's copy this same message there they don't have to type as much and then here we have the chat id already given to us yeah and then here we can say what to do you want for example and in here we can specify the reply markup because it should now contain this markup here equal to this markup now what you might want to do is you might want to make this like some sort of global version or something so you can use this over and over again but anyhow now once you have done that let's save it and let's see what happens so now here if i say slash game we get a little keyboard here if i say slash help now it's going to send me the help say go boo and then there it refers me a dice but close doesn't work yet so we have to modify it so close actually works so i'm going to copy this and right above it i'm going to specify what happens when we say close so you might want to change this to not just say close because you might want to do close multiple times but anyhow now this markup we can just say instead of reply keyboard markup we can say dot reply keyboard remove because we want to remove it and in here we can also just specify selective equals false i have no idea what selective means but go for it use it and in here where it says markup dot add we don't need that anymore either and here we just need to specify a new markup and then we can say good riddance once they say goodbye cancel and run again but of course you can just go out of this by pressing that button or going back on your phone but now if we say close then as you can see it closed and said good riddance so yeah so that's pretty neat so we can go game and i have a bunch of options so if i click here then you have boo help and close now you can close and boom no more options and yeah that is the basics of use of creating a telegram bot with python i hope you enjoyed and i will see you all again in the next tutorial um you
Info
Channel: Steve's teacher
Views: 658
Rating: 5 out of 5
Keywords: telegram, bot, code bot, telegram bot, telegram bot tutorial, tutorial, course, code telegram bot, python, telepy, telebot, pyTelegramBotApi, code bot python, telegram python, make bot, create bot, make telegram bot easy, crash course, telegram bot pythpn, telegram bot create, group, for groups, make telegram bot, Steve's teacher, steves teacher, steve teacher, hd, how to, programmer vibe, teacher steve, learn, teach, easy, beginner, programming, code, coding, education, educate, netsu
Id: RD3SyYHDQpo
Channel Id: undefined
Length: 25min 57sec (1557 seconds)
Published: Sun Aug 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.