Steps to create a Telegram Bot
Follow the below instructions to make a Telegram chatbot.
Step 1: Open your telegram account and in the search bar type “BotFather”.

Step 2: Click on the “BotFather” and Click on the “Start” button.

Step 3: Type “/newbot”.

Step 4: Type your unique bot name.

Step 5: Now type a unique username for your bot.
Remember: username must end in ‘bot’. E.g. ‘Tetris_bot’ or ‘Tetrisbot’.
Step 6: After giving a unique username you will get a message like the below. it contains a token to access the HTTP API. Keep your token secure and store it safely.

Step 7: Creating a flask app for the webhook response.
First of all, you will need to install python and flask on your computer.
Talk to BotFather: Open Telegram and search for the BotFather (@BotFather). Start a chat with it and follow the instructions to create a new bot.
Create a New Bot: Use the
/newbot
command to create a new bot. Follow the prompts to give your bot a name and a username. Once created, BotFather will give you a token. Save this token; you'll need it to interact with the Telegram API.
Step 2: Set Up Your Python Environment
Install Python: Ensure you have Python installed on your machine. You can download it from python.org.
Install Required Libraries: You will need the
python-telegram-bot
library. Install it using pip:bashpip install python-telegram-bot
Step 3: Write Your Bot Code
Here's a basic example of a Telegram bot that responds to messages:
pythonfrom telegram import Update
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext
# Define your bot's token
TOKEN = 'YOUR_TELEGRAM_BOT_TOKEN_HERE'
# Define a start command handler
def start(update: Update, context: CallbackContext) -> None:
update.message.reply_text('Hello! Welcome to your new bot.')
# Define a message handler for echoing messages
def echo(update: Update, context: CallbackContext) -> None:
update.message.reply_text(update.message.text)
def main() -> None:
# Create the Updater and pass it your bot's token
updater = Updater(TOKEN)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# Register the start command handler
dispatcher.add_handler(CommandHandler('start', start))
# Register a handler for echoing messages
dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, echo))
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT, SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':
main()
Explanation of the Code
Import Libraries: We import the necessary classes and functions from the
python-telegram-bot
library.Define the Token: Replace
'YOUR_TELEGRAM_BOT_TOKEN_HERE'
with the token you received from BotFather.Define Command Handlers:
start
: Responds to the/start
command.echo
: Echoes back any text messages sent to the bot.
Create the Updater: The
Updater
class continuously fetches new updates from Telegram and passes them to the dispatcher.Register Handlers: The
dispatcher
is used to register handlers for different types of updates.Start Polling: The bot starts polling Telegram for updates.
Idle: The bot runs until you stop it manually.
Running Your Bot
Save the code to a file, for example,
telegram_bot.py
.Run the script:
bashpython telegram_bot.py
Your bot should now be running. You can interact with it on Telegram, and it will respond to your messages.
Additional Features
You can extend your bot by adding more command handlers, processing different types of updates, or integrating with other APIs and services. The python-telegram-bot
documentation provides comprehensive guides and examples for advanced features.