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 with 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.

Getting Started

First of all, create a bot using Telegram BotFather. To create a BotFather follow the below steps –

  • 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:
  • Go to ‘API development tools’ and fill out the form.
  • You will get the api_id and api_hash parameters required for user authorization.

Modules needed

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.





# importing all required libraries
import telebot
from telethon.sync import TelegramClient
from import InputPeerUser, InputPeerChannel
from telethon import TelegramClient, sync, events
# get your api_id, api_hash, token
# from telegram as described above
api_id = 'API_id'
api_hash = 'API_hash'
token = 'bot token'
# your phone number
# creating a telegram session and assigning
# it to a variable client
client = TelegramClient('session', api_id, api_hash)
# connecting and building the session
# in case of script ran first time it will
# ask either to input token or otp sent to
# number or sent or your telegram id 
if not client.is_user_authorized():
    # signing in the client
    client.sign_in(phone, input('Enter the code: '))
    # receiver user_id and access_hash, use
    # my user_id and access_hash for reference
    receiver = InputPeerUser('user_id', 'user_hash')
    # sending message using telegram client
    client.send_message(receiver, message, parse_mode='html')
except Exception as e:
    # there may be many error coming in while like peer
    # error, wwrong access_hash, flood_error, etc
# disconnecting the telegram session 


Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Competitive Programmer, Full Stack Developer, Technical Content Writer, Machine Learner

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.