How to Write a Quick and Easy Telegram Bot that Posts Messages with Python
This guide will show you a quick and easy implementation of a Telegram bot that is able to post text messages to a channel. I did this as I have a piece of code that continously scrapes data from the web and performs data analysis on it. I wanted to have a way of getting regular updates on my analysis without having to manually check my output files. In addition, I also wanted to be able to share my analysis findings with an audience by posting the messages to a channel.
Step 1: Engage with “the BotFather”
Click this link to open your telegram client* and start a chat with the BotFather. You will need to send the message /newbot and follow the instructions**.
*if you don’t have an account already you’ll need to sign up for one
**for the username you might need to try a few different times before you get one that hasn’t been used by someone else before
Step 2: Make your bot the admin of the channel
Now, in order for your bot to be able to post to your required channel, it needs to be added as an Admin of that respective channel. In addition to this, for the next steps to work, the channel must have been public at least once in its history. This is due to the fact that we will need the channel id. If the channel has always been private, then it is sufficient to turn it public for a short while in order to set up its link. In the example below, we have defined the channel username as ChannelForBotDEV.
Once you have clicked the Save button, you can turn the channel back to private if you require it to be so.
Step 3: Sending messages through Python
Lastly you will need to add the following piece of Python code to your project. Please notice that you only need to update the TOKEN and CHAT_USERNAME variables using the information from Step 1 and Step 2*.
*please note that for CHAT_USERNAME you need to to add an @ symbol in front of the username that you set up in Step 2
Conclusion
Now you have a function sendMessage that you can call in Python with an argument containing the text body of the message you want your bot to send. This will allow you to easily integrate it within your existing projects so that you can call it whenever you want your script to send a message in Telegram.
For my next article I will talk about how you can automate your existing script to regularly call this function and post updates to your Telegram channel.
Resources
- Telegram Bot API documentation
Ten articles before and after
Create Telegram Chatbot. In January 2021, Telegram announced… – Telegram Group
Telegram Secret Chat — Enable Now to be Secure – Telegram Group
Bot sederhana dengan menggunakan python! – Telegram Group
python Telegram bot pytelegrambotapi telegram bot simple – Telegram Group
How I Connected with 10,000 people over a couple of weeks – Telegram Group
Membuat MJPEG Stream Camera Menggunakan ESP32-CAM — Part 2 – Telegram Group
Snap-It Find-It: Your Shopping Companion Bot – Telegram Group
First telegram bot in python.. Lets Create our first echo telegram bot… – Telegram Group
How to use the MOONCLONE Bot. To being using MOONCLONE BOT open up… – Telegram Group
How to restart a python program from inside – Telegram Group