Site icon DataFlair

How to Send Messages on Telegram using Python

python send messages telegram

Python course with 57 real-time projects - Learn Python

Telegram is a communication application used by millions of people across the world. Telegram also provides API support which enables us to create bots and automate sending messages as well. Thus in this project, we will send messages to a telegram user using python.

Python Telegram message sender project:

Here, we create a simple telegram message sender using tkinter and telethon

Project Prerequisites:

Telethon is a library that serves to use the API of Telegram. It is easy to follow and hence does not require any prerequisite knowledge. Tkinter contains widgets, thus we use it to create the GUI of the application.
It is available in python by default. Import to see if it is available

import tkinter

If the package is available for use, it will not show any error. If not, Linux users can install it using the command

sudo apt-get install python3-tkinter.  Windows users can reinstall python.

Download Telegram Message Sender Project Code:

You can download the source code for the project to Send message to Telegram user using Python from the given link: Python Telegram Message Sender

Project File Structure:

Below is the flow of the Python project to Send message to Telegram user:

1. Obtaining the API ID and Hash code
2. Importing libraries
3. Creating the sender function
4. Creation of the application interface

1. Obtaining the API ID and Hash code:

Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!

To obtain the hash code and the API ID visit Telegram API link. In the resulting page, key in your phone number with the country code and provide the confirmation code or the passcode obtained through telegram after selecting the Next button.

In the next page, type in the requested details. These details do not impact your API hash and ID values.

Finally you will obtain your API ID and Hash codes.

2. Importing libraries:

#DataFlair Tutorial for sending messages to telegram users using python
#Import necessary modules
from telethon import TelegramClient, events, sync
from tkinter import *
from tkinter import messagebox

Code Explanation:

3. Creating the sender function:

#Sender function
def send_message():
   #API details
   user_details = user_entry.get()
   message_content = message_entry.get("1.0","end-1c")
  
   #Raise a warning if no input is given
   if (len(user_details) <=0) & (len(message_content)<=1):
       messagebox.showerror(message = "ENTER USER DETAILS" )
   else:
 #These API codes wont work, hence create your own
       api_id = 1234567
       api_hash = '1234567890abcdefghijklmopqrt123'
       #Initialise telegram client with API codes
       client = TelegramClient('session_name', api_id, api_hash)
       #Start the process
       client.start()
       #Send the message
       client.send_message(user_details, message_content)
       messagebox.showinfo(message = "MESSAGE SENT (:" )

Code explanation:

4. Creation of the application interface:

#Define the user interface
telegram_mess = Tk()
telegram_mess.geometry("400x300")
telegram_mess.title("DataFlair's Telegram Message Sender")
 
bg_img = PhotoImage(file = "/home/deepika/Downloads/internship/telegram_deepika/pictures/bg_image.png")
 # Show image using label
label1 = Label(telegram_mess, image = bg_img, bd=0)
label1.pack()
 
#Application Title in the window
title_label  = Label(telegram_mess, text="DataFlair's Telegram Message Sender", bg="#1d8bd6")
title_label.place(x=80,y=10)
 
#Input for user entry
user_label = Label(telegram_mess, text="ENTER USER DETAILS:", bg="#2591D9")
info_label = Label(telegram_mess, text="Note: Phone number with code or username with @", bg="#2C99DC")
user_label.place(x=0,y=40)
info_label.place(x=0,y=70)
user_entry = Entry(telegram_mess, width=20)
user_entry.place(x=160,y=40)
 
#Message input
message_label = Label(telegram_mess, text="ENTER MESSAGE:", bg ="#35A1DF")
message_label.place(x=0,y=100)
message_entry = Text(telegram_mess, width=30, height=3)
message_entry.place(x=130,y=99)
 
#send button
send_button = Button(telegram_mess, text="Send Button", command=send_message, relief= RAISED)
send_button.place(relx=0.5,rely=0.59, anchor=CENTER)
telegram_mess.mainloop()

Code explanation:

NOTE: Telegram will ask to provide a phone number and a code sent to this number to authorise access while running it for the first time. This will occur at the command line. You can also verify the added device containing the short name given for the application in the first step.

Python Telegram Message Sender Output:

Provide a username (with ‘@’) or phone number (with “+countrycode”) and view the message sent from your end to the user.

Summary

Thus we created a simple telegram message sender in python. The project introduces the use of Telegram APIs and one simple application of it. We also learnt tkinter to create GUI in python.

Exit mobile version