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 (https://t.me/newsindiachannel) 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 and 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.
– 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.
– Now like any other bot, this bot understands few pre-defined commands. Start with /start . You will be presented with other commands.
– 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:
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.
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.
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.
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.
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.
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: – News India: For latest news in Hindi and English every few Hours : t.me/newsindiachannel – The Python Django (group): Discussions about Django : t.me/thepythondjango – Government Jobs Bulletin: Latest Government Jobs in India: t.me/govtjb