Create Your Own Telegram Bot With Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to build a telegram bot in python so let's get right into it all right guys now before we get into the actual tutorial i want to mention that this video is sponsored by tab nine and as always don't skip that part because it may be interesting to you i never promote anything on this channel that i don't think is useful or interesting to the audience and 9 is very interesting and useful you can see that i'm using it here on the bottom right corner myself tab 9 is active in pycharm uh and what it is is an ai auto completion engine and it supports all modern languages that are somehow important python javascript java c plus and so on uh it is supported in all the relevant ides and editors you can see vs code atom emacs vim sublime all the jet prints products here android studio and so on and one thing that's very positive about tab nine is the code privacy they take your privacy very seriously they don't do anything stupid with your code or your data um so it's a very very uh trustworthy company you can go to tablon.com codeprivacy especially nowadays where we have the controversy with github copilot this may be a good alternative uh so you might want to look into that when it comes to the plans they have a completely free plan you don't have to pay anything at all um and it's forever free you have the basic completion model if you want to have a more advanced completion model that's a little bit more professional uh you can look into tab 9 pro it costs some money except for if you're a student so if you're a student you can go to table9.com students and you can use your school email to get a free tab 9 pro account or tad mind pro license so you can get it for free as a student and if you're not a student you can get 25 off using a coupon code from this video so you can get a 25 discount as a neural nine uh viewer here so you can just click the link in description down below maybe i'm going to somehow enter the coupon code here as well uh and you're going to get 25 percent off tab nine pro uh on a yearly plan all right guys so let's get started and create our first telegram bot the first thing we need to do is we need to take our phone and you need to install telegram onto it uh you need to install the app on your phone you need to set it up and then if you want you can also connect it to the browser so that you can work on it on your computer which is what i'm going to do but i mainly do this for recording purposes so all this can be done on the phone as well uh what you need to do basically is you need to find the bot father bot so you need to go to search and you need to type bot father and you're then going to find this spot here uh with the blue check mark you click on it and you can start it or if you have used it in the past you're going to restart it and you're going to start a conversation with that bot now as you can see we have a couple of commands here like new bot or set name and so on uh and we're going to use these commands to actually create new telegram bots which we're then going to program in python or going to use in python uh so what we need to do here is we need to say slash new box and then it says all right and you bought how are we going to call it please choose a name for your bot let's say we're going to call it neural bot in this case good now let's choose a username for your bot it must end in bot so for example tetrisbot or tetris underscore bot it has to have a bot name in the end now i'm not sure if we can use neural bot as well here so let's just use neural bot sorry this username is already taken okay let's take neural underscore bot it's also taken then neural nine underscore bot there you go and what you can see here is we get done congratulations on your new bot you will find it at wherever we're going to find it and then you can now add a description so on uh and the important thing is use this token to access the http api keep your token secure and store it safely it can be used by anyone to control your bot which is also the reason why you don't see it at the moment because i have censored it uh this is your secret key this is your api token your api uh key that you want to keep secret so if someone has that key they can control your bot so if your bot is in any way dealing with some sensitive data or doing some sensitive actions you want to keep this this api code here secret so this is how we create it and now we can use this bot in python so now we can start with the python coding and the first thing we want to do is we want to open up cmd and want to say pip install python dash telegram dash bot like that and once this is installed we can go into the code and say import telegram dot ext now what we're going to do first here in the code is we're going to load the token because in my case i have now stored the token that we got from the bot father into this text file and i'm going to load it from the text file so that i don't have to code it here and show it to the audience if you don't care about this keep in mind it's insecure to do it like that but you can just say token equals then a string and your token whatever it is if you want to i'm going to load it from the file so i'm going to say with open token.txt in reading mode s f and then token equals f dot reads like that then we're going to create the updater object and this updater object is what we're going to use to do anything with our bot so we're going to say updater equals telegram dot ext dot updater we're going to pass the token and we're going to say um use context equals true and then we're going to get the dispatcher we're going to call this disp uh is equal to not telegram it's equal to updater dot dispatcher like that this is just the object and we can then by the way you can also just use update updater.dispatcher all the time but i think this is just a better way to do it so updater dot dispatcher i don't even know why i deleted that uh so we have the dispatcher now and this is what we're going to use but now we have to define some functions so what is our bot going to be able to do and one command that it should be able to handle is the start command so what happens when we start the bond we want to call this function start so def start and we're going to pass here the update and the context because we set use context equals true and we're going to be able to use the context for some information so if we just want to print something so this basically the function that gets triggered when someone starts our bot someone wants to talk to neural ninebot okay what happens uh we start the connection we start a conversation what is the first message or what happens when we do that and what we want to happen is update dot message dot reply text and here we pass whatever we want to say so for example hello welcome to neural bot and you can also add some information like those are my commands or whatever you don't have to but in this case we're just going to say hello welcome to neural bot then we can have another function which is going to be uh help and here we can say update context again and we can do something like i don't know update dot message dot reply text as you can see here great auto completion from tab nine just completed the thing that i just wrote before uh and here i can say um the we can do a multi-line string the following commands are and as you can see here tab nine again says available already uh we can say following commands are available we can say start welcome message we can say help this message and we can decide what else we want to have for example let's say we want to have the command content because the neural ninebot is going to give you some information information about neural nine content um and maybe you wanna have contact as well so information about contact i don't know this could be a help message now let's do another one def content with update and context as you can see here again auto completion um and we can just do something like update dot message dot reply text again and i don't know we have videos and books watch and read them and we're not going to do anything meaningful here because we're not going to write anything meaningful here because this tutorial is not on how to write text we're just going to have some functions here and the last one is going to be contact we're going to have update and context here again and we're going to say update message reply text and we're going to say something like i don't know you can contact me on my discord server or maybe since we're talking to the bot we should say you can contact florian come on on the discord server not florene florian like that which is me by the way so those are some basic functions which we can now add to the hand to the dispatcher so what we want to do is we want to create a command handler it's going to handle a certain command map a function to it and then we want to add that command handler to the dispatcher so we're going to say this dot add handler and we're going to pass a uh what was a telegram dot x dot command handler and the first thing we passed here is the command name so start for example this is basically what you want the user to type in for example slash start but without the slash without this initial slash and the second thing is the function that you want this command to trigger so in this case when someone says slash start we want it to call the start function and we don't call the function here we just pass it as an object and we can now copy this four times and change this to uh help to contact down here and to content in here and we change the functions here as well and that is it and yeah that should work so now in the end we're going to say updater dot uh updater dot start polling and updater dot idle like that so that should work out we have four functions we have mapped them with command handlers to the commands and the bot should be running all right so in order to put the bot online we need to run the script we're going to just right click it and run it and once it's running we should be able to contact that bot so we're going to go to search here and we're going to go to neural nine underscore bot and you can see here neural bot neural nine underscore bot this is a global search so we're going to go to that bot and then we're going to click on start and once we do that you can see hello welcome to neural bot okay now let's do hello we're not going to really get a response here so we can type some nonsense and if we now go ahead and say slash help this is going to give me the following commands are available and then we have the commands we can type slash content we have videos and books watch and read them we can type slash contact you can contact florian on the discord server and we can also do this by just clicking on these commands here from the help function right so those are the basic command handlers that we implemented just right now so now i want to show you how you can also handle individual ordinary messages and not only commands so not only something like slash something but actually something like hello world for example just answering ordinary messages if we want to do that we're not going to use a command handler we're going to use a message handler but still we need to define a function for that so i'm going to just say def handle message and we're going to pass here update and context again and we're going to say update dot message dot reply text and usually this is the place where you would uh use some ai chat bot for example if you have watched my discord ai chatbot tutorial we could use something like that here we can just get the message from the user feed it into the model and get a proper response or we can just do it in this way in this case with a formatted string we're just going to say use set and then update dot message dot text update.message.text is going to give us what the user actually sent to the bot and we're then just going to say use set and then the message of the user so if the user sends hello world it's going to get us a response uh you said hello world uh this is just to show you how you can work with that we can take this update message text and we can put that into neural network get a response and then send that using the reply text function uh and if this video performs well i'm going to do a video on how to do that but for now we're going to leave it at that this formatted string here and if we now want to use that function for handling messages we're going to say disp dot add handler and we're going to add not a command handler but a message handler and what we're going to pass first is telegram dot x dot filter filters dot text because we're going to work with text here and what we're going to pass as a second parameter is the function handle message not calling it just passing it so we can run that now and start the bot by doing that and if we now contact the bot again let's see if it reacts to commands there you go so if i now say hello it's going to say you said hello bot or if i just write something it's gonna said you said this thing that i just said um so this is how it works and this is a basic message handler so last but not least i want to show you a practical example of a useful command and it's going to be a stock command we're going to have the command slash stock and then some ticker symbol for example aapl or fb or tsla whatever and as a result of that we want to get the current stock price and maybe some additional information in order to do that we're going to define a function called stock and we're going to have update and context here as well and now we're going to actually use context because of course if we have slash stock it's not just a command we also want to have slash stock and then the ticker symbol which is an argument to that command and this is what we can get with context but before we do that we need to install a library and use the library which we're going to use for the financial data if you're not interested in finance you want you don't want to do that example you don't have to install it but if you want to go with the example here then just open up cmd and install the pandas dash data reader this is what we're going to use to get some stock data i have it installed already and if you have installed it you're just going to say import pandas underscore data reader s web okay so we have that and now we're going to say ticker equals context dot rx 0. so context.rx gives us the parameters or the arguments that we pass to the stock command so if we have stock aapl and uh second third parameter this would be rx0 this would be rx1 this would be rx2 okay so we are interested in the ticker so context rx2 and where are zero and we're not gonna check for all the cases where we pass something that's invalid we're just going to assume that the user passes something uh reasonable here and we're now going to get the data so we're going to say web data reader for that particular ticker from the yahoo finance api we're not going to pass any dates uh and now in order to get the latest price price we're just going to get data dot ilock negative 1 which is the last and then just the close price from it and we're going to to then say update message reply text the current price off and then we need to make this an f string and we can say ticker is and then price uh we're going to use a formatting here though we're going to say point point to f we're going to use dollars here point to f basically means uh cut or only use two decimal places here uh and that should be it we should now be able to use that function once we have the command handler of course so command handler stock and stock you go now let's run this and see if it works we're going to open up the telegram bot here again let's test with a simple hello if it works you said hello okay so now stock aapl the current price of apple is this okay then slash stock fb the current price of fb is that so it works it gets the data real time now if i pass some nonsense here we're going to probably see a crash but we should not be able to see it here on the telegram we should see it here as you can see we got an exception i'm not even sure if that now terminates the bot so if i do stock apple no it's still working okay so that's fine now before we end this video i want to show you that this also works from the phone so i have my phone here with the telegram app and obviously this works from the phone because it's just a web interface for the phone but still i want to show it to you so if i type slash stock and then for example ccl which is carnival cruise lines and then i send this you can see that it happens in a web interface and the current price is 22.92 dollars so this also works from the phone i can say hello world but of course it's this only works if the bot is running in python all right so that's it for today's video hope you enjoyed hope you learned something if so let me know by hitting the like button leaving a comment in the comment section down below of course also subscribe to this channel and hit the notification bell to not miss a single future video for free if this video performs well i'm going to do another video on a telegram bot maybe with some ai with some ai chat bot for telegram uh so make sure you hit the like button if you like it and besides that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 7,152
Rating: 4.9497485 out of 5
Keywords: telegram, telegram bot, python telegram, python telegram bot, python, chatbot, telegram chatbot, chat bot
Id: CNkiPN_WZfA
Channel Id: undefined
Length: 20min 41sec (1241 seconds)
Published: Thu Aug 12 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.