Main

How To Create A Telegram Bot In Python For Beginners (2024 Tutorial)

In today's video we will be creating a chat bot for Telegram using Bot Father and Python. This is the perfect bot for getting started with creating more complex Telegram bots. It will show you how to get an API Token, how to customize the bot, how you can add commands to the bot, and how you can make it work in groups! Source Code: https://neamatullah.com/building-a-python-telegram-bot-a-step-by-step-guide/ --------------------------------------------------------------- 🧠 My ChatGPT Free course👇 https://www.udemy.com/course/chatgpt-for-programmer/?referralCode=32F7482081EFCA3F8A93 --------------------------------------------------------------- 🧠 My Hacking Masterclass👇 https://www.udemy.com/course/the-ultimate-ethical-hacking-masterclass/?referralCode=04396DED9904CEE40EC9 --------------------------------------------------------------- 🧠 My Complete python bootcamp + 100 Real-world Application👇 https://www.udemy.com/course/python-project-masterclass/?referralCode=0DBF917BCBA6F262B7D7 --------------------------------------------------------------- 🧠 My Python Automation Masterclass👇 https://www.udemy.com/course/mastering-python-automation-for-every-day-life/?referralCode=C9492B65364B02709639 --------------------------------------------------------------- Community - https://www.neamatullah.com Facebook - https://www.facebook.com/lxryofgod/ Linkedin - https://www.linkedin.com/in/neamatullah-ekhteyari-b420012b0/ instagram - https://www.instagram.com/neamat_andishmand/ --------------------------------------------------------------- Resources: finding phone location using Python: https://youtu.be/ig4fVz8dBnw hack a computer: https://youtu.be/gXueAx176T8?si=MeDiY5siSFfNz3Fm the complete python bootcamp: https://youtu.be/NEGy3lJURPw?si=uPVfnhwQFroZPDIR chatgpt for programmer: https://youtu.be/OXbqu6-2hg8?si=V-H8WLo9PnPwm9Tp Timestamps 00:00 - Intro 00:41 - Creating Bot With Bot father 04:04 - Handling the Command for bot with python 15:20 - Start the chat with bot

CodeWithDevin

3 days ago

Hi friends, welcome to my YouTube channel. In this video I will show you how to create a telegram bot using Python step by step. Step one is about creating a telegram bot using bot father. Step two is about asking for an offer. Step three is about handling the command for bot using Python. Step four, start chatting with bots in private and groups. So by the end of this video you will be having your telegram bot completely customizable. So let's start this video. Step one is about creating telegr
am bot with bot fodder. To create your telegram bot you need to open this telegram in your laptop or in your phone. Come to the search bar, search for bot father and then you will be having the botfod click on the bot father and within here you will be having a sub button hit that. Then you will be having this description. The description you see we have new bot here. For creating a new bot. We need to hit this new bot. It is asking me to please choose a name to your bot. I'm going to name this.
Now let's choose a username that ends with bot. Now we need to copy this token and we need to save it here. And also I am going to import from typing import final and I'm going to use it within here. Now we have the token here, right? And also we need to have our username. So this is the username that we can use it. And this username must start with an ad. And also I'm going to use this final with this chair click on help and within here. Now we can select more details. For example set name. Th
is is for editing the bot. Okay, set description. So let's go with a little description here. I'm going to select Cobra Python bot. Description is added and now let's go to set about the text. The about detail is also added. Now it is time to add a profile picture for this one. Okay, profile is also added. Let's see what else can we do here? We can set command, but right now I'm not going to do that because we didn't handle that in our Python script. So later on we can add the commands. If I cam
e here and search for Cobra Python bot. Here it is. We can find it here. And I am cobra Python bot and I can help you with learning python programming language and also the description I am cobra bot help you to learn python. Now if I hit start, nothing gonna happen because we didn't add any command to handle this. Step two is a simple task that only takes 2 seconds. Take a quick pause and click on this awesome button called subscribe so we can stay connected. It will also encourage me to share
many more projects like the one you're watching right now. Step three, handling the commands with Python. Now let's install the python telegram bot. Okay, so for that I'm going to use pip install Python telegram bot. Okay, this is now successfully installed. Now that we have our token, our bot username, it is time to import some of the module from this telegram library to start coding. So from telegram I am going to import. The first one that I'm going to import is called update. Okay, this is t
he first thing I'm going to import and also I am from telegram dot ext or extension. I am going to import application common handler message handler filters context type it is time to handle it's time to handle the command. Okay, so for with the new version we missed this time the function async at the beginning because it will allow me to asynchronize it. So it's going to be start command and within here I'm going to use update, update type of update context type of context types dot default ty
pe. Within this method or function we are going to use, we are going to handle the logic of the bot that we are going to build. So I'm going to use a weight here. So update dot message. So dot reply to the reply to message. That's one thing we can use. And also reply text. Here it is. And within here we need to provide the text. For example, it's gonna be hello for thanks for chatting with me. And within here. So here you see we have the start command. Okay, I'm going to copy this and I can use
it twice. I can use it for help command and also some other custom command. So this one is going to be for help command and let's change the text. It's going to be a custom command. So we need to change this to kind of custom. Okay, and within here I'm going to handle it. Okay, so up to here we have handled the command here. Okay, so we can provide some kind of commands, okay, so that means these all are for the command. Handling the command. Now it is time to handle the responses. Responses. An
d within here I'm going to create a function and it's going to take a text, text type of string, it's going to return string, and within this we're going to have processed type of string and it is going to change the text to a lowercase. So within here you're going to put the logic of the bot that you have. Okay, so for example, I am going to handle the hello first in the process. Hey there if. How are you? I'm good. Okay. If in processed then we can return remember to subscribe and at the end.
And if none of this is gonna be in the text then I'm going to say that I do not understand. Update dot message dot chat dot type. So this will give me the message type. And within here we need that text. Text is going to be type of string and it's also gonna contain the message. Okay dot text. So within here we need to print if the user. And here let's use that update dot message dot chat dot id is in the message type. Then let's just print it. Okay. Dot strip, no white spaces. So we are going t
o use strip, okay and then I will use response and it's going to be type of string and then handle responses. And within here I will give that new text and let's go with else and then just return. And the response is going to be type of string and let's call the handle and we just give the text here. So this is going to be the response here, okay. And and then the response will be printed here and then use await update dot message dot reply text and just provide the response here. So this part i
s responsible for handling the messages and responses. And now it is time to handle the error as well. So I will use as sync div of error. And within here just use the update and the context type of context type dot default type. Okay so let's print a formatted string here and it's going to be update here. Let's say cost error and then print the error. The error is in the context dot error. Okay, that is kind of cool. Now it is time to go and use the main method. Okay, so within here we need to
create the app. App is going to be equal to the application dot builder dot token and within here we need to give the token dot build. So the first thing we need to handle in here is the command app dot add handler and it's going to be the command handler start command start underscore command. Again, let's go with app dot add handler. And within here it's going to be a command handler. And because it's a function we need to this time go with help and we need to call the help command. And the la
st thing is the custom command. So I will use app dot add handler and it's going to be a command handler. And this one is going to be custom and let's go with custom command. Okay. Within here let's print a statement and this one is going to be starting the bot. And within here I'm going to handle one more thing and that is going to be the message. Okay so app dot add handler and this one is going to be a message handler. Okay. Message handler. And within here we need to add the filters that we
just imported that, okay, filters dot text, okay. And use a camera and then handle message. So this will handle the messages. And also it is time to handle the error. So I will use error and app dot error handler. And within here just use the error. So that's kind of cool. And it is time to pull the bot. Okay so it's gonna be polls the bot within here I will print it pulling and let's use app dot fran underscore polling. And it's going to be pull interval. And I will give 3 seconds. So it will p
ull each three second. Okay, after all this is completed, it's time back to, it's time to go back to the telegram bot father and set the command for the bot that you created. Okay, so within here let's go back to the bot father. This time we need to use the set command and within here we need to select it. Okay. Okay so here we need to choose the bot which is Cobra bot, cobra python bot. And here let's type that command as you see the command one on description. Command tune description is how i
t's gonna work. Three command here start, help and customer. So the first one is going to be start and within here it's gonna be starts the bot. Okay, I'm going to add another one. Okay, so set command command and let's add one more. Now you see that all the commands set successfully. It is time to go back to the ide that we have it and it is time to run this program. So as you see right now we have start the bot and pooling. Step four is chatting with the bots in private and group here and sear
ch for the bot that we had. And here it is. If you hit this alcobra python bot and I can help you with the learning Python programming language. If I hit start it's gonna telling me that. Hello, thanks for chatting with me and we have a menu here that we have only custom right now. Type help here. Please type something so I can respond. Okay, so kind of if you just type hello, it's gonna respond either. If I say how are you? It's gonna tell that I'm good. And after all that you see that in priva
te it's telling me that user and then it's giving me the id for the bot private and it's telling me hello and hey there. Let's go back and create a group and new group it's going to be learn Python next. Let's see here it is and create it. So the group is created here now and if I just say, if I just say hello or hello, nothing gonna happen here, type cobra python and then say hello, it's gonna tell me nothing because we didn't give the right camera. Over here we group info and within here Cobra
Python promote administration and here you can add some of these permission. Okay. And then hit save. After that hit cancel. Now let's just type add a cobra python bot and then say hello. This time you see that it's telling me hey there again, let's tag it and say how are you? It's gonna tell me I'm good here. Let's see. We have this in the supergroup and also we have it in the private. Okay, so we printed the id right now it is in the private and we have say help and it say I do not know, some
body wrote okay that and also we have it in the group. Okay, so the super group we say how are you? It is telling us I'm good. It is how we can use it with groups and also we can use it with private. If you find this helpful, please like it. And if you have any kind of question or problem, you can ask it in the comments below. Thanks for watching and I will see you in the next video.

Comments