Adding Commands to a Telegram Bot (Python)

Tiempo de lectura: 4 minutos

Reading time: 3 minutes

Following the previous day’s post How to Make a Telegram Bot (Python), we will complete this Python script to add command functionality to the bot.

Commands are used to instruct the bot to perform specific functions.

A command is written in the Telegram chat in the following format:

/command

Let’s make the bot do something when we write this example command.

To do this, we retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Ensure the bot keeps running
bot.polling(none_stop=True)

And let’s add the following:

# Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
    chat_id = message.chat.id
    bot.send_message(chat_id, 'You have invoked the command')

In the commands parameter, we add the commands that will execute the created method. Inside the function, we retrieve the chat ID and use send_message to respond with the bot.

If we combine the code, it looks like this:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Ensure the bot keeps running
bot.polling(none_stop=True)

Now we can register the command using @BotFather in Telegram and selecting our bot.

Here we select Edit Bot.

Reading time: 3 minutes

Following the previous day’s post How to Make a Telegram Bot (Python), we will complete this Python script to add command functionality to the bot.

Commands are used to instruct the bot to perform specific functions.

A command is written in the Telegram chat in the following format:

/command

Let’s make the bot do something when we write this example command.

To do this, we retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Ensure the bot keeps running
bot.polling(none_stop=True)

Now we can register the command using @BotFather in Telegram and selecting our bot.

Here we select Edit Bot.

And now we select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: commands are written in the following format:

command – description

command – description

If there are multiple commands, they are added with a line break.

And that’s it for today’s tutorial.

Reading time: 3 minutes

Following the previous day’s post How to Make a Telegram Bot (Python), we will complete this Python script to add command functionality to the bot.

Commands are used to instruct the bot to perform specific functions.

A command is written in the Telegram chat in the following format:

/command

Let’s make the bot do something when we write this example command.

To do this, we retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Ensure the bot keeps running
bot.polling(none_stop=True)

Now we can register the command using @BotFather in Telegram and selecting our bot.

Here we select Edit Bot.

And now we select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: commands are written in the following format:

command – description

command – description

If there are multiple commands, they are added with a line break.

And that’s it for today’s tutorial.

Reading time: 3 minutes

Following the previous day’s post How to Make a Telegram Bot (Python), we will complete this Python script to add command functionality to the bot.

Commands are used to instruct the bot to perform specific functions.

A command is written in the Telegram chat in the following format:

/command

Let’s make the bot do something when we write this example command.

To do this, we retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Ensure the bot keeps running
bot.polling(none_stop=True)

Now we can register the command using @BotFather in Telegram and selecting our bot.

Here we select Edit Bot.

And now we select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: commands are written in the following format:

command – description

command – description

If there are multiple commands, they are added with a line break.

And that’s it for today’s tutorial.

Reading time: 3 minutes

Continuing from the previous day’s post How to Make a Telegram Bot (Python), we will now complete the Python script to add command functionality to the bot.

Commands are used to specify the actions we want the bot to perform.

A command is written in the Telegram chat in the following format:

/command

Now, let’s make the bot do something when we enter this example command.

To do that, we need to retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering it with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Make sure the bot keeps running
bot.polling(none_stop=True)

Now, we can register the command using @BotFather in Telegram and selecting our bot.

Here, select Edit Bot.

Then, select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: Commands are written in the following format:

command – description

command – description

If there are multiple commands, they should be added with a line break.

And that concludes today’s tutorial.

Reading time: 3 minutes

Following the previous day’s post on How to Make a Telegram Bot (Python), we will now complete the Python script to add command functionality to the bot.

Commands are used to specify the actions we want the bot to perform.

A command is written in the Telegram chat in the following format:

/command

Now, let’s make the bot do something when we enter this example command.

To do that, we need to retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering it with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Make sure the bot keeps running
bot.polling(none_stop=True)

Now, we can register the command using @BotFather in Telegram and selecting our bot.

Here, select Edit Bot.

Then, select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: Commands are written in the following format:

command – description

command – description

If there are multiple commands, they should be added with a line break.

And that concludes today’s tutorial.

Reading time: 3 minutes

Continuing from the previous day’s post on How to Make a Telegram Bot (Python), we will now enhance the Python script to add command functionality to the bot.

Commands allow users to specify the actions they want the bot to perform.

In Telegram, commands are written in the chat using the following format:

/command

Now, let’s make the bot perform a specific action when a command is entered.

To do that, we need to retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import the telebot library
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering it with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages in the chat where the bot is included
Pass messages (a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the received message
print('The message is', message.json['text'])
# Get the chat ID to be able to respond with the bot
chat_id = message.chat.id
# Respond with a text using the bot
bot.send_message(chat_id, "I'm responding to you as a Bot")
Register the function to receive chat messages
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def execute_command(message):
chat_id = message.chat.id
bot.send_message(chat_id, 'You have invoked the command')
Make sure the bot keeps running
bot.polling(none_stop=True)

Now, we can register the command using @BotFather on Telegram and selecting our bot.

Here, select Edit Bot.

Then, select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: Commands should be written in the following format:

command – description

command – description

If there are multiple commands, they should be listed with a line break.

And that concludes today’s tutorial.

Reading time: 3 minutes

Following the previous day’s post on How to Make a Telegram Bot (Python), we will now complete this Python script to add command functionality to the bot.

Commands are used to instruct the bot to perform specific functions.

A command is written in the Telegram chat using the following format:

/command

Now, let’s make the bot do something when we write that example command.

To do that, we retrieve the code from the previous day:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering it with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages from the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the message that arrives
print('The message is', message.json['text'])
# Get the chat id to be able to respond with the bot
id_chat = message.chat.id
# Respond with a text using the bot
bot.send_message(id_chat, "I'm responding and I'm a Bot")
Register the function to receive messages from the chat
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def command(message):
id_chat = message.chat.id
bot.send_message(id_chat, 'You have invoked the command')
Tell the bot not to stop
bot.polling(none_stop=True)

And let’s add the following:

# Add the function to execute the command
@bot.message_handler(commands=['command'])
def command(message):
    id_chat = message.chat.id
    bot.send_message(id_chat, 'You have invoked the command')

In the commands parameter, we add the commands that will trigger the method we created. Inside the function, we retrieve the chat ID and use send_message to respond with the bot.

If we combine the code, it looks as follows:

# -*- coding: utf-8 -*-
Import telebot
import telebot
Add the token provided by BotFather
token_bot = "543759345:AHsjdfUUdfsi2kjksaKKKdmmmsddd"
Initialize the bot by registering it with the token
bot = telebot.TeleBot(token_bot)
Create a function to receive all the messages from the chat where the bot is included
Pass messages (it is a list of messages)
def receive_messages(messages):
# Iterate through the list of messages:
for message in messages:
# Example to print the message that arrives
print('The message is', message.json['text'])
# Get the chat id to be able to respond with the bot
id_chat = message.chat.id
# Respond with a text using the bot
bot.send_message(id_chat, "I'm responding and I'm a Bot")
Register the function to receive messages from the chat
bot.set_update_listener(receive_messages)
Add the function to execute the command
@bot.message_handler(commands=['command'])
def command(message):
id_chat = message.chat.id
bot.send_message(id_chat, 'You have invoked the command')
Tell the bot not to stop
bot.polling(none_stop=True)

Now we can register the command using @BotFather on Telegram and selecting our bot.

Here, select Edit Bot.

Then, select Edit Commands to add the commands.

Once added, the command help will appear within the Telegram chat where we have included the bot.

Note: Commands are written in the following format:

command – description

command – description

If there are multiple commands, they are added with a line break.

And that concludes today’s tutorial.

Leave a Comment