Telegram is one of the most used program for instant messaging and it is known for the possibility to have bots that interact with user privately or within groups.
In this post we will see how-to create a Telegram Bot to send messages in a Telegram chat group using the Python programming language and telepot library.
This will be the final result!

STEP 1: Creating a Bot with BotFather
First of all in order to create a telegram bot you have to have a Telegram account. If you do, then go ahead and open your telegram app (mobile or desktop) and follow the steps:
- Add to your contact list BotFather
- Start a conversation with BotFather by typing
/start. You will see a command list immediately. - In order to create a new telegram bot, you have to type /newbot command and follow the instructions. Be careful, your bot’s username has to end with bot. For example, PythonBot or python_bot.
- I decided to choose VerySimplePersonalBot.


BotFather is also able to perform following actions for our newly created bot:
- Put description to your bot
- Upload avatar
- Change access token
- Delete your bot
- Set commands
- Define privacy mode
STEP 2: Implement Bot in Python
Open PyCharm or you favorite IDE, then you have to install a package called “telepot”. Use PyCharm Terminal and type:
pip install telepot
Then you just need to create a new file .py and paste the following code:
import telepot
import time
def on_chat_message(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
print(msg)
if content_type == 'text':
name = msg["from"]["first_name"]
txt = msg['text']
bot.sendMessage(chat_id, 'Hello %s, this is your new bot!'%name)
bot.sendMessage(chat_id, 'You have just typed: %s'%txt)
TOKEN = '*******************' # Use your token
bot = telepot.Bot(TOKEN)
bot.message_loop(on_chat_message)
print('Listening ...')
while 1:
time.sleep(10)
As you can see at the beginning of the code, we imported the telepot package, installed previously, and the time package. In order to create a bot object you need the token provided by BotFather and you have to paste it on line 15.
Then, we link the bot with the on_chat_message function. This function will be called every time a new message is received. The on_chat_function will always receive just an argument, that corresponds to the message that was sent by the bot.
Let’s firstly print the message on the console using print(). Then our handle function creates three variables: content_type to know if the message is of type text or a file, chat_type for know if it is a private chat or a group and chat_id that is the unique number associated with each user.
If the message received is text, the bot sends a welcome message to the user and then it will repeat the message sent by the user as a parrot.
STEP 3: Test the Telegram Bot
Let’s save everything in .py format and launch it. Now we can chat with our bot.

Finally, let’s have a look at our console. We will see the message received by our handling function. The message will have the following format:

Enjoy your new bot and have fun making it smarter!
