Telegram Userbot Tutorial using Telethon
Content
Many time you might have seen on telegram that a user type something and a image get converted into
stickers, or a pattern appears in the chat.
So this is Done Using Userbots. You can deploy them freely and can use them for personal use, but today i am not gonna discuss how we can deploy some 3rd party userbot. In this blog I am going to discuss how you can code your own userbot using python based framework Telethon.
So this is Done Using Userbots. You can deploy them freely and can use them for personal use, but today i am not gonna discuss how we can deploy some 3rd party userbot. In this blog I am going to discuss how you can code your own userbot using python based framework Telethon.
Requirements :
0. Creating Virtual Environment
This is an additional and recommend step to do before creating our project. We are going to install virtual environment and then install all the required library in that virtual environment. We are doing this so that we can deploy our bot easily on any hosting site like heroku and railway. We will discuss it more later in our blog.
To install virtual environment run the following command in command terminal.
pip install virtualenv
First lets crate a folder with name "TelegramUserbot" Now lets create a virtual environment for our project.
So in order to this first get in the folder "/TelegramUserbot" , and then open the command terminal there by pressing
shift+right mouse button
Now run the following command in the terminal to create the virtual environment.
virtualenv env_name
OR
python -m virtualenv env_name
Note :- In place of env_name you can type any name according to your preference.
So this will create a virtual environment folder to store all the settings of your project. Now let's activate our virtual environment by running the following command.
Note :- Now open the vsCode in the same folder "/TelegramUserbot and use the vscode terminal for the activating virtual machine".
env_name/Scripts/activate
You will see something like (env_name) in your terminal. Now anything you install it will not be installed for the whole computer it will only be installed for that virtual environment terminal.
Here we finish our 0th Step. Let's Continue to the 1st step.
Documentation of Virtual environment
1. Installing Telethon.
So let's start by installing Telethon, Because we are going to use it library for building our userbot. To install telethon run the following command in you command terminal with activated virtual environment.
python3 -m pip install --upgrade telethon
Note :- You can use any name but the file extension should be .py
So Yes this was quite simple step. Let's move to next step.
1. Creating Session.
Now in our main.py file , lets type the following code to get the connection file or string from the telegram using telethon.
from telethon import TelegramClient
# this is your api id and api hash for your telegram account get it from https://my.telegram.org/
api_id = 14321221 # replace it with your own api id
api_hash = 'bf24ab6d9b49c3855f161f9d5afd312c' # replace it with your own api hash
# here we are using our api id and hash to connect to our telegram account and create a session.
# we will use this to client to define functions to our userbot.
client = TelegramClient('sessionName', api_id=api_id, api_hash=api_hash)
# sessionName can be anything it just defines the name of the session file.
Let's discuss what we typed above. Here api_id is the id of your telegram account in the database of Telegram. And api_hash is like the password for your telegram account , to access your chats and Data from telegram.
After this run the "main.py" file in VsCode. In terminal you will see that it is asking for the mobile number , enter the mobile number of your telegram account associated with the api_id and api_hash you have given. Then it will ask for OTP , that you will get on your mobile phone. Provide the following. If you have two step verification onn you will be asked to provide the two step password in order to continue.
Note :- Password will not be visible to you while you type it. Just type your password and press enter.
Once you entered your details correctly, it will show "Signed in successfully as {your first name}".
Now let's clear what's going on. In simple word we are login our account with a computer that is provided by telegram itself for the usage of bots. So no need to worry about you two step pass code or anything it is fully safe.
So after this you have successfully created a bot session for you telegram account.
Note :- Some file will be created in your "/TelegramUserbot" folder. Don't Delete it or you will have to create the session again.
1. Writing First Function.
Now lets create a simple function.
Function Description:
when someone will type hello in our private chat. Our bot will reply with
"Hey ! How are you?"
To do this copy the following code snippet and add this in "main.py" file.
@client.on(events.NewMessage(incoming=True, func= lambda e: e.is_private , pattern="Hello"))
async def hiHello(event):
chat = await event.get_chat()
await client.send_message(chat , "Hey ! How Are you" , reply_to=event.message)
For Now let's not get into deep details just in simple words, we are using our "client" to check for event that is "NewMessage" event. And then we applied some of the filters on that function. Like "incoming=true" checks that if new message in this client in incoming or not. Incoming means that message is sent by someone to you(client). Now "func= lambda e: e.is_private" checks that the incoming message is in private chat or not.
" pattern="Hello" " it checks that the incoming message in private chat is "Hello" or not.
If all the filters are satisfied then the function defined that is "def hiHello(event)" will run.
Inside our function, "chat = await event.get_chat()" this line gets the chat details like who is sending the message. You can print that chat by "print(chat)" and see the details of the chat like "id" of the chat. In second line of our function we says our Bot Client to send the message to that chat and what to send , and also we specified which message to reply.
Here in our "def hiHello(event)" function the event parameter is the event that happened , which means the message we received from someone is a event. So inside that event we have many data, like where that event happened, who sent the message and what is the message id we received and the message you received.
Note :- I recommended you to print the event , by "print(event)", so that you can see what else you can get from that event object.
Now Let's terminate the the running program in our terminal by pressing ctrl + c in terminal. An restart the bot.
To again start the bot run the following command in terminal of vscode.
python main.py
This will start the bot. Now go to telegram and ask someone to send you "hello" message, Or send hello to you telegram account using some another account of your owns, let's leave this to you how you do this.
You will see your bot is working properly. And you have successfully deployed you first telegram bot using Telethon.
For support and Query Join our telegram Group BitLegion
Now from here you can continue making more and more plugins. You can get more plugins on our Blog Page AntiqBlogs
Or You can watch our Youtube Videos on Telethon userbot. CodePlaza