Telegram is a popular messaging app that is widely used for communication and sharing media. In this tutorial, we will learn how to use Python to send messages to a Telegram channel using the python-telegram-bot library.

Prerequisites:

  1. Python 3 installed on your machine
  2. A Telegram account and access to a smartphone or desktop app

VersionDetails:

Python: 3.12.0
pip: 23.2.1
python-telegram-bot: 21.0.1

Overview:

  1. Create a Telegram bot
  2. Install the python-telegram-bot library
  3. Send a message to a channel
  4. Test your code
  5. Bonus

Links:

python-telegram-bot : https://python-telegram-bot.org/

Steps:

1. Create a Telegram bot

To use the Telegram API, you will need to create a Telegram bot and obtain its API key. You can do this by talking to the BotFather in Telegram. To do this:

  1. Open the Telegram app on your smartphone or desktop.
  2. Search for the “BotFather” username in the search bar.
  3. Click on the “Start” button to start a conversation with the BotFather.
  4. Type “/newbot” and follow the prompts to create a new bot. The BotFather will give you an API key that you will use in the next step.

2. Install the python-telegram-bot library

Next, you will need to install the python-telegram-bot library using pip. Open a terminal and enter the following command:

pip install python-telegram-bot

3. Send a message to a channel

To send a message to a Telegram channel using Python, you will need to:

Import the telegram module:

import telegram

Create a bot object using the API key you obtained from the BotFather:

bot = telegram.Bot(token='YOUR_API_KEY')

Obtain the channel ID for the channel you want to send the message to.

You can do this by talking to the @get_id_bot in Telegram.

Use the send_message() method to send a message to the channel:

bot.send_message(chat_id=channel_id, text='Hello, World!')

Note: You may also need to give your bot permission to post in the channel. To do this, you will need to add the bot as an administrator to the channel.

4. Test your code

To test your code, run it and check the Telegram channel to see if the message was sent successfully.

Python3 <your_file_name>.py

Bonus

To send an Image use:

bot.send_photo(chat_id=channel_id, photo=open('/path/to/photo.jpg', 'rb'))

To send an document use:

bot.send_document(chat_id=channel_id, document=open('/path/to/document.pdf', 'rb'))

To send and video use:

bot.send_video(chat_id=channel_id, video=open('/path/to/video.mp4', 'rb'))

Sample File:

import asyncio
import telegram
TOKEN = "YOUR_BOT_TOKEN"
chat_id = 'CHAT_ID_OR_CHANNEL_ID'
# Channel ID Sample: -1001829542722
bot = telegram.Bot(token=TOKEN)
async def send_message(text, chat_id):
    async with bot:
        await bot.send_message(text=text, chat_id=chat_id)
async def send_document(document, chat_id):
    async with bot:
        await bot.send_document(document=document, chat_id=chat_id)
async def send_photo(photo, chat_id):
    async with bot:
        await bot.send_photo(photo=photo, chat_id=chat_id)
async def send_video(video, chat_id):
    async with bot:
        await bot.send_video(video=video, chat_id=chat_id)
async def main():
    # Sending a message
    await send_message(text='Hi Sujit!, How are you?', chat_id=chat_id)
    # Sending a document
    await send_document(document=open('/path/to/document.pdf', 'rb'), chat_id=chat_id)
    # Sending a photo
    await send_photo(photo=open('/path/to/photo.jpg', 'rb'), chat_id=chat_id)
    # Sending a video
    await send_video(video=open('path/to/video.mp4', 'rb'), chat_id=chat_id)
if __name__ == '__main__':
    asyncio.run(main())

Conclusion:

In this tutorial, we learned how to use Python to send messages to a Telegram channel using the python-telegram-bot library. We covered how to create a Telegram bot, install the python-telegram-bot library, and use the send_message() method to send a message to a channel. With this knowledge, you can use Python to build a variety of bot applications for Telegram.

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments