How to Send Messages and Files to Telegram with Python

Kok Hua
2 min readJul 19, 2023

--

Telegram API: Send and receive text, message

In this step-by-step guide, I will show how to send messages and images to Telegram and to download images from Telegram using Python. By utilizing the python Telethon library and TelegramClient, you can effortlessly send messages and images with just a few lines of python code.

Steps

  • Obtain your Telegram API ID and API hash values by visiting my.telegram.org and setting up the API. More details in the documentation.
  • pip install telethon
  • Run the python code

Sending messages and files

from telethon import TelegramClient
import asyncio

## Run the following 2 lines if using Google Colab
import nest_asyncio
nest_asyncio.apply()

api_id='API_ID'
api_hash='API_HASH'
session_name= 'session1'
## must be your channel or have authorization to post
channel_invite_link = 't.me/AICats_Prompt'


async def func():
entity = await client.get_entity(channel_invite_link)
# Send an image follow by a text.
await client.send_file(entity, '/file/to/media.jpeg')
await client.send_message(entity=entity, message="Your text")

# connection
async with TelegramClient(session_name, api_id, api_hash) as client:
client.loop.run_until_complete(func())

Notes

  1. The session name can be any arbitrary name. If an error occurs with the session name being locked, use a new random session name.
  2. The channel should either be your own, or you must have authorization to post.

Dowloading Images

The same method can be used to download media or other content from any Telegram channel.

import uuid
from telethon import TelegramClient
import asyncio

## Run the following 2 lines if using Google Colab
import nest_asyncio
nest_asyncio.apply()

api_id='API_ID'
api_hash='API_HASH'
session_name= 'session1'
channel_invite_link = 't.me/AICats_Prompt'

async def func():
entity = await client.get_entity(channel_invite_link)
messages = []
#download images from last 10 messages
async for m in client.iter_messages(entity, reverse=False, limit = 10):
messages.append(m)

for m in reversed(messages):
print(await m.download_media(f"image/{uuid.uuid1()}"))

async with TelegramClient(username, api_id, api_hash) as client:
client.loop.run_until_complete(func())

Notes

  1. The messages are displayed from most recent to oldest by default, ensuring you always receive the latest message first. To access the oldest message first, you can use reverse=True.
  2. You can include a filter in client.iter_messages to specify and filter out specific message types, such as photos, etc. You can refer to official telethon documentation for more filters selection.

Conclusion

Above is a simple method for sending messages and files to a Telegram channel using Python. Additionally, we can use the same API to download images. The Telethon library offers many other functions that you can explore for your specific needs. Have fun getting started with your first Telegram project!

--

--

Kok Hua

Marketing Analyst | Data Engineer | Python Automation Enthusiast. Blog: https://simply-python.com