Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
scraping   5   40645
How to create completely automated telegram channel with python

In this article we will see how to create a completely automated telegram channel.

To demonstrate the process we will take through the example of a telegram channel News India ( created by me which post news every hour.

The automated process of generating content and posting on the channel is divided into three parts.
- Creating telegram channel and bot
- Generating/fetching content using python script
- Posting the content to telegram channel.

Creating telegram channel and Bot:

-Install telegram app from Google Play.

-After you are done with initial setup. Click on menu and create a new channel.

how to create completely automated telegram channel with python

- Make this a public channel so that anybody can search and join it.

- Add first member to it. Your channel is up.

- Now we need to create a bot.

- Search for 'botfather'. This is the bot which help us by creating all bots for us.

how to create completely automated telegram channel with python

- Now like any other bot, this bot understands few pre-defined commands. Start with /start  You will be presented with other commands.

how to create completely automated telegram channel with python
- To create a new bot, use command /newbot .

- You will be asked to choose a name. Choose appropriate name for your bot. Remember name of your bot must end with 'bot'.  For example newsindiachannelbot  in my case.

- If name is available, botfather will provide telegram link of your bot and a token to access HTTP API. Something like this:  923778870:AAH54XXXMBUXXXPz4XX-fbeXXXTXYYYY

Adding bot as Admin:

- Now go to the settings of your channel and click 'Administrators.'.  Click Add administrator.

- You need to search for the bot. For example @yourbotname.

- Congratulations, first step is completed.

how to create completely automated telegram channel with python  

Getting content using python script:

My channel as name suggest will be sharing news headlines every hour. For this I am scrapping Google News page. This is for educational purpose only and it is not recommended to send too many requests to any website without their permission.

Install below packages using pip.


To scrap web page, I am using BeautifulSoup. Lets see the below code.

def get_news_data(starting_url):
    news_data = [] 
    # get complete page on this url
    response = requests.get(starting_url)
    soup = BeautifulSoup(response.text, 'lxml')
    # find all elemens with given class name
    elements = soup.find(attrs={"class": "deQdld"})
    # iterate over the list of elements
    for element in elements:
        data = {}
        news_element = element.find(attrs={"class":"nuEeue hzdq5d ME7ew"})
        if news_element:
                # get the news url link 
                link = news_element.attrs["href"]
                # get the news text
                text = news_element.string.strip()
            except Exception as e:

            if link and text:            
                data["link"] = link
                data["text"] = text
    # return the list of news items
    return news_data

Now I have the list of dictionaries where each dictionary have two key value pairs, news text and news link. Now create a complete message to post on the channel from this list of news dictionaries.

def get_msg(news_data):
    msg = "\n\n\n"
    for news_item in news_data:
        text = news_item["text"]
        link = news_item["link"]
        msg += text+'  [<a href="'+link+'">source</a>]'
        msg += "\n\n"

Posting the message in channel using bot:

Install the python package python-telegram-bot==6.1.0  using pip. 

Find out the ID of your channel. Use below 2 lines to post the message to your channel.

import telegram
from config import telegram_token_news

# use token generated in first step
bot = telegram.Bot(token=telegram_token_news)
status = bot.send_message(chat_id="@newsindiachannel", text=msg, parse_mode=telegram.ParseMode.HTML)


I always recommend to store secrets, password and token in a separate config file. Once you run this script, it will scrap the data from Google news page, parse and format it and post the data to news channel.

how to create completely automated telegram channel with python  

Now you can schedule a cron every hour or every few hours depending on your requirement.

I am using PythonAnyWhere server to host my Django Apps. I have scheduled the above script on python anywhere server.

How to schedule a cron on PythonAnyWhere server.

Complete code for the script above is available on Github.

You may join these telegram channels:
- PythonCircle (group): Discussions about Django :

Feel free to comment in case of any issue.

scraping   5   40645
5 comments on 'How To Create Completely Automated Telegram Channel With Python'
Login to comment

Amit Aug. 5, 2018, 4:28 a.m.
Exactly what i was looking for. precisely explained
Naveenkumar Aug. 27, 2019, 6:02 a.m.
from config import telegram_token_news I have a problem with this line. config in not supporting
Wa Dec. 17, 2019, 11:38 p.m.
great content!
Ryan June 5, 2020, 3:48 a.m.
I created a separate config, but maybe my format is incorrect.My telegram key has a colon ":" and I get contains:telegram_token_news = 'xxxxxxxx:xxxxxxxxxxx'SyntaxError: invalid syntax.I tried single ' and double " quotes around it, but doesn't fix.
Abc Nov. 3, 2020, 10:54 a.m.
NoneTraceback (most recent call last): File ".\", line 51, in <module> news_data = get_news_data(url) File ".\", line 13, in get_news_data for c_wizi in element:TypeError: 'NoneType' object is not iterablei have this error how to resolve

Related Articles:
Scraping data of 2019 Indian General Election using Python Request and BeautifulSoup and analyzing it
scraping 2019 election data india, Scraping data of 2019 Indian General Election using Python BeautifulSoup and analyzing it, using beautifulsoup to collection election data, using request...
Python Script 10: Collecting one million website links
Collecting one million website links by scraping using requests and BeautifulSoup in Python. Python script to collect one million website urls, Using beautifulsoup to scrape data, Web scraping using python, web scraping using beautifulsoup, link collection using python beautifulsoup...
Python Script 7: Scraping tweets using BeautifulSoup
Scrapping tweets using BeautifulSoup and requests in python. Downloading tweets without Twitter API. Fetching tweets using python script by parsing HTML....
Scraping 10000 tweets in 60 seconds using celery, RabbitMQ and Docker cluster with rotating proxy
Scraping large amount of tweets within minutes using celery and python, RabbitMQ and docker cluster with Python, Scraping huge data quickly using docker cluster with TOR, using rotating proxy in python, using celery rabbitmq and docker cluster in python to scrape data, Using TOR with Python...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap