Creating a Basic Telegram Bot using Python and Telebot
In this tutorial, we'll walk you through the process of creating a basic Telegram bot using Python and the Telebot library. The bot will respond to user messages and provide a welcome message upon the
/start command. Let's get started!
Telegram Account: You'll need a Telegram account to create a bot and obtain its token.
Python Installed: Ensure that Python is installed on your system.
Step 1: Getting Your Bot Token
Open the Telegram app and search for BotFather.
Start a chat with BotFather by sending the
/newbotcommand to create a new bot. Set a name and username for your bot.
Copy the token provided by BotFather.
Step 2: Writing the Python Code
bot = telebot.TeleBot("your_bot_token_here") # Replace with your bot token
# /start command for your bot
start_message = "<b>Hello, welcome to the bot!</b>"
bot.reply_to(message, start_message, parse_mode='HTML')
# A text message handler
@bot.message_handler(func=lambda message: True)
reply_text = message.text
# Start the bot
Running the Bot
Save the Python code in a
Open your terminal or command prompt and navigate to the directory containing the
your_bot_token_herewith the actual bot token.
Run the bot script by executing the command:
Interacting with Your Bot
Open your Telegram app and search for your bot's username.
Start a chat with your bot.
/startcommand to receive a welcome message.
Send any text message to the bot, and it will respond with the same message.
Congratulations! You've successfully created a basic Telegram bot using Python and the Telebot library. This bot responds to the
/start command with a welcome message and echoes back any text messages you send. This is just a starting point, and you can build more advanced interactions and features using the Telebot library.