Creating a Telegram Bot in Python 3.9 Tutorial (Fast & Easy)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so what is up guys in this tutorial i'm going to be showing you how to create a bot in telegram so let's go ahead and click on my current bot this is the code palace bot and as you can see i already have a few messages but if you type something such as hello and click on send the bot will respond with a certain phrase and if we ask for the time it will give us the time and the date but of course if we write something random that the robot does not understand it's going to tell us that it doesn't really know what we're saying and these are all pre-made responses so you can substitute this with your own machine learning or any custom phrase that you want i'll show you how to do that later and also if you go to the icon you can add your own custom photo and it will have a description and all of this good stuff so that's what we will be making in this tutorial so to get started you're going to want to go ahead and open your telegram app and click on the search bar because inside here we need to search for the bot father so just try to find this and this is where we will be creating and getting the api key and the first thing we have to do inside here is type in slash new bot and click on create a new bot and it's going to ask us what would we like to call it we'll just call it sample bot and then we have to assign it a username and the username must end with the word bot so we can call this abc123 abc bot and click on sent and it will say congratulations your bot has been created and it's going to give you this user token so you're going to have to make sure to copy this because this will be your api key but before we move on from here we're going to ask for slash help and click on send so we can see what other commands we have and i'm not going to go over all of that right now but as you can see there is an option to set the user pick and set the about text and set the description so if you want to do that just click on those links and they will give you very clear instructions on how to change it for example if you want to set the about text it's going to say choose a bot that you want to set it for we'll set it for the new bot abc bot i will say i am abc bot and click on send and it will say success the about section has successfully been updated so you can go ahead and do the same thing for the other fields such as the profile image and that will be that but let's scroll up again and copy the key that we just created so make sure to copy that as you can see it has been copied and then we have to go ahead and create a new python project so as you can see we have a main.pi file but we're not going to use this yet because we're going to create another file and this one's just going to be for the constants so we're going to call it constants and inside here we can type in api underscore key and we're just going to create a pair of apostrophes and insert our api key there and this is just going to be to keep things simple for later in case we need to use this api key again then we're going to create another pi file and this is going to be used to handle the responses so we'll just call this responses and inside here the first thing we want to do is import from date time we want to import the date time and then we're going to create a function that is going to be called sample underscore responses and it needs an input text because that will be used to create a message or to create a response and the user message is going to equal the string of the input text and we want this to be lower case since python will recognize a uppercase letter and a lowercase letter as two completely different letters so it's important we make sure that the user doesn't have to worry about capitalizing their words correctly then down here we're going to type if the user message is in let's pretend they say hello or they say hi or they say sup for example if the user message we write has these words inside it then we are just going to go ahead and return hey how's it going and that will take care of our first response then we can go ahead and copy this and create another response such as who are you and who are you with a question mark and let's just cancel this last one because that will be enough for the example and we will write i am abc but and that's all it will respond now let's create a function that gives the user the time in case the user wants to ask for the time so if user message is in time or time with a question mark then we will get the local time which is going to be datetime.now we want to format this string so we're going to type in and create a new variable which is going to be datetime and that's going to equal now string formatted time or i believe that's what it means and we're going to insert the format that we want so we want to get the day first and then we want to get the month and then we will go ahead and get the year and for the hours we'll go ahead and first get the hour of course then the minutes and then the second so this will format the time for us to a readable format and we can go ahead and return the string of date time and finally if none of these messages were recognized by the bot we want to return a message that shows that it did not understand what we said so i don't understand you or something along those lines and of course these are sample responses that you can change with whatever logic you want this is just to show you that you can insert any string as a response and eventually you can hook this up to machine learning if you know how to do that but for the purposes of this video i think it's very straightforward and very easy to implement so we'll stick with this and finally we can go to our main dot pi file and before we continue let's go to our terminal and we need to call pip install python dash telegram dash bot and then we can close that once it has successfully installed the next thing we have to do is import our constants as keys and then we're going to get from telegram dot extensions we will import everything to keep things simple and then we should also import the responses as ah then the first thing i want to do is print that our program has started so we can say bot started and then under that we're going to create a start command because we can also create commands for a robot in case you want to do slash help or slash stat it will have some helpful text for your bot so we'll just write this and we need to insert an update and the context so inside here we need to type in updates dot message dot reply underscore text and inside we have to type type something random to get started with an exclamation mark then we can just copy this and paste it right under but this time we will call it help command and we will write if you need help you should ask for it on google exclamation mark next let's go ahead and create a function that handles the messages and gives the user a reply so to do this we'll type in definition handle message and that's going to take an update and a context of course and i believe actually that the context is not necessary anymore for the recent versions but i'm just going to include it because i don't want the tutorial to break for no reason and let's continue so inside here we're going to type that the text is going to equal the string of the update dot message dot text and then we want to make sure it is in lower case so we'll call dot lower and then we will get the response which is going to equal r dot sample responses and we are going to insert the text that we've sent to the user then we can go ahead and update dot message and i accidentally added an extra s here let's take that back but we go ahead and call update dot message and inside here we call reply text and we insert the response so this receives the text from the user this processes it and this puts it back out to the user then we're going to create a function that logs the errors so we're going to type in def error and that's going to take an update and a context and in case anything goes wrong we are just going to format this string and write updates insert the update here cost error context dot error then we need to go ahead and create the main function so we're going to call def main and let's make some space and the first thing we want to do is create an updater and this updater is going to start the bot so updater and here we're going to call our keys dot api key and we need to go ahead and write uses context and set that to true and again i believe that was for older versions but i'm just going to use that now because that's how i wrote the program for this tutorial then down here we are going to create a dispatcher which is going to equal updater dot dispatcher and then down below we're going to make sure that the robot has the commands that we specified so we're going to call dispatcher dot add handler and it's going to be a command handler and then we can give it a title so the first one we want to give it is a start title and that's going to be for our start command but make sure you don't include the parentheses at the end because we do not need those then we can go ahead and copy this and paste it right under and this is going to be the help command that we had right above then we have to go ahead and create a handler for the messages and this is going to be called a message handler and inside here we are first going to call the filters.txt and then we need to add the text we want to respond which is the handle message then we are going to call dp dot add error handler and we are going to add the error and finally we can add the code that starts the program such as the updater dot start polling and inside here you can add a interval that you want the robot to check for updates so if you put 60 that will wait 60 seconds before checking for user inputs if you put 5 it will wait 5 seconds before checking for the next message and it does it every 5 seconds so maybe if you want to delay the bot from sending too much info you can do this but if you want an instant reply just don't put anything or add zero so it just checks all the time then we have to go ahead and call updated.idle and i believe this is just to make sure that it continuously stays active even if nothing's happening but uh i wouldn't be able to explain too much about that and finally let's go ahead and call this main function and with that being done we can go ahead and run this program and hopefully there are no errors so so far as you can see the bot has been started and now if we actually go to our application and i forgot what i named the bots i really hope i can find it abc123 abc bot and as you can see it is right there so that's how you find the bot and once we start it it should start the start command and it says type something random to get started and we can also do that for the help command so let's go ahead and type in help and i messed up because i kept start here twice so make sure to change the command to help and then rerun the program so let's go back here and then we do slash help and click on enter and we'll say if you need help you should ask for it on google but of course this can be a place where you can tell the user how to use the bot in case you have some very special functionality and then let's write time and click on enter it will give us the date who are you question mark send a message i am abc bot and then let's type something it doesn't really understand and as you can see it's going to write i don't understand you in case there's some text that the bot does not know how to handle so there's a lot you can do with this bot and the key point of this tutorial is that you can really play around with this handle message function because it can easily turn into ai or some other functionality it can include links and it can do so many things but i believe this was a very easy introduction to how to use the telegram bot and if you have any questions you're more than welcome to leave it in the comment section below and with that being said thanks for watching as always and i'll see you guys in the next video see you
Info
Channel: Code Palace
Views: 85,729
Rating: undefined out of 5
Keywords: android studio, code, coding, program, programming, tutorials, android, studio, java, kotlin, python, c#, how to, app, applications, developer, developement, learn coding, learn programming, free, coding in flow, androidevs, code palace, windows, computers, ios, technology
Id: PTAkiukJK7E
Channel Id: undefined
Length: 13min 7sec (787 seconds)
Published: Mon Jan 25 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.