Send message to Telegram user using Python
Have you ever wondered how people do automation on Telegram? You may know that Telegram has a big user base and so it is one of the preferred social media to read people. What good thing about Telegram is that it provides a bunch of API’s methods, unlike Whatsapp which restricts such things. So in this post, we will be sharing how to send messages to a Telegram user using Python.
First, create a bot using Telegram BotFather. To create a BotFather follow the below steps as follows:
- Open the telegram app and search for @BotFather.
- Click on the start button or send “/start”.
- Then send “/newbot” message to set up a name and a username.
- After setting name and username BotFather will give you an API token which is your bot token.\
Then create an app on the telegram. Follow the below steps:
- Log into the telegram core: https://my.telegram.org
- Go to ‘API development tools’ and fill out the form.
- You will get the api_id and api_hash parameters required for user authorization.
You need several Python library imports for the script functioning.
- telebot: To install this module type the below command in the terminal.
pip install telebot
- telethon: To install this module type the below command in the terminal.
pip install telethon
Below is the implementation.
TelegramClient, sync, events
, api_id, api_hash)
'Enter the code: '
client.send_message(receiver, message, parse_mode
Exception as e:
- The code starts by importing all the required libraries.
- The first library is telebot which is used to interact with Telegram.
- Next, we import the sync module which helps us to keep our code organized and easy to read.
- We also import the events module so that we can handle various telegram related events.
- Next, we get our API_id, API_hash, and token from Telegram as described earlier in this tutorial.
- Then, we create a variable called message for holding our message.
- We then connect to Telegram and assign the session to a variable called client .
- Next, we sign in the client using its phone number and user ID ( input(‘Enter the code: ‘)) .
- Finally, we try sending a message using the telegram client but there may be many errors coming in while trying so let’s print them out for further analysis.
- Once everything is okay, we disconnect the session by calling client.disconnect() .
- The code will first import all the required libraries and set up a TelegramClient instance.
- Next, it will get your API_id, API_hash and token from Telegram.
- Finally, it will create a message variable and assign it to the client object.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!