Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python | Whatsapp birthday bot
Next article icon

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.

Getting Started

First, create a bot using Telegram BotFather. To create a BotFather follow the below steps as follows:  

Then create an app on the telegram. Follow the below steps: 

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. 

Python3
# importing all required librariesimporttelebotfromtelethon.syncimportTelegramClientfromtelethon.tl.typesimportInputPeerUser,InputPeerChannelfromtelethonimportTelegramClient,sync,events# get your api_id, api_hash, token# from telegram as described aboveapi_id='API_id'api_hash='API_hash'token='bot token'message="Working..."# your phone numberphone='YOUR_PHONE_NUMBER_WTH_COUNTRY_CODE'# creating a telegram session and assigning# it to a variable clientclient=TelegramClient('session',api_id,api_hash)# connecting and building the sessionclient.connect()# in case of script ran first time it will# ask either to input token or otp sent to# number or sent or your telegram idifnotclient.is_user_authorized():client.send_code_request(phone)# signing in the clientclient.sign_in(phone,input('Enter the code: '))try:# receiver user_id and access_hash, use# my user_id and access_hash for referencereceiver=InputPeerUser('user_id','user_hash')# sending message using telegram clientclient.send_message(receiver,message,parse_mode='html')exceptExceptionase:# there may be many error coming in while like peer# error, wrong access_hash, flood_error, etcprint(e);# disconnecting the telegram sessionclient.disconnect()

Code Explanation: 

  1. The code starts by importing all the required libraries.
  2. The first library is telebot which is used to interact with Telegram.
  3. Next, we import the sync module which helps us to keep our code organized and easy to read.
  4. We also import the events module so that we can handle various telegram related events.
  5. Next, we get our API_id, API_hash, and token from Telegram as described earlier in this tutorial.
  6. Then, we create a variable called message for holding our message.
  7. We then connect to Telegram and assign the session to a variable called client .
  8. Next, we sign in the client using its phone number and user ID ( input('Enter the code: ')) .
  9. 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.
  10. Once everything is okay, we disconnect the session by calling client.disconnect() .
  11. The code will first import all the required libraries and set up a TelegramClient instance.
  12. Next, it will get your API_id, API_hash and token from Telegram.
  13. Finally, it will create a message variable and assign it to the client object. 

Send message to Telegram user using Python
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp