A Telegram bot is an automated software that interacts with users on the Telegram messaging platform. Bots can be programmed to perform various tasks, such as sending messages, responding to user queries, managing channels, and more. They can also integrate with external services and APIs to provide advanced functionalities.
This guide will walk you through the steps to create a simple Telegram bot.
If you don't have a Telegram account, download and install the app Telegram on your phone or use the desktop version. After installing, create an account.
- To create a new Telegram bot, you'll need to interact with BotFather, which is the official Telegram bot used for creating and managing bots. To Know more about Telegram BotFather bot visit Telegram BotFather Bot
- Follow the steps below to create your own bot:
- Open the Telegram app on your phone or desktop.
- In the search bar at the top, type
BotFather
. - Select BotFather (the verified account with a blue checkmark).
- Start a conversation with BotFather.
- In the chat with BotFather, send the following command:
/newbot
- BotFather will reply with a message asking you to provide a name for your bot.
- Enter a name for your bot. This name will be displayed when users interact with your bot.
- Example:
My Cool Bot
.
- BotFather will ask for a username for your bot. This username must be unique and end with "bot". It will be used to identify your bot.
- Example:
mycoolbot
ormy_cool_bot
. - Note: The username is used in the bot's URL, so make sure it’s something recognizable and relevant to your bot’s function.
- After you’ve provided a name and username, BotFather will generate and send you an API token.
- The message will look like:
Done! Congratulations on your new bot. You will find it at t.me/mycoolbot. You can now add a description, about section, and profile picture for your bot. Use this token to access the HTTP API: <your-bot-token>
- The message will look like:
- Important: Save this API token securely as it is the key to interact with Telegram's Bot API. You'll need it in your bot code to authenticate your bot and make requests to Telegram.
- BotFather also allows you to manage your bot, including setting a description, about section, and profile picture. To access more commands, simply send
/help
to BotFather for a list of options. - More details of Telegram BotFather are mentioned in Telegram BotFather
- You’ll need a programming environment. Python is commonly used to create Telegram bots, but you can also use other languages like Java, Node.js, etc.
- Install necessary libraries or packages to interact with the Telegram Bot API. For Python, you can use the
python-telegram-bot
library. - Install the Python package via pip:
pip install python-telegram-bot
- To make bot executable and perform functionality we need to write script
- Below is a simple Python example using the python-telegram-bot library.
MyCoolBot.py
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters
# Replace with your Bot's API Token
API_TOKEN = '7910823701:AAFZCP4kXGp7h_mHkw7lOHeMhuqQj3PYqI4'
# Start command handler
async def start(update: Update, context):
await update.message.reply_text('Hello! I am your simple Telegram bot. Type /help for commands.')
# Help command handler
async def help_command(update: Update, context):
await update.message.reply_text('You can use the following commands:\n/start - Start the bot\n/help - Get help')
# Echo handler - Responds to any message
async def echo(update: Update, context):
user_message = update.message.text
await update.message.reply_text(f'You said: {user_message}')
# Main function to set up the bot
def main():
# Create the Application and pass the bot's API token
application = Application.builder().token(API_TOKEN).build()
# Add command handlers
application.add_handler(CommandHandler('start', start))
application.add_handler(CommandHandler('help', help_command))
# Add message handler for text messages
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
# Start the bot and run the event loop
application.run_polling()
if __name__ == '__main__':
main()
- Save the Python script and run it. Your bot should now be active and listening for messages.
- You can interact with the bot by searching for it on Telegram using the username you created and sending the
/start
command.
- Hosting: You can host your bot on platforms like Heroku, AWS, or DigitalOcean to keep it running 24/7.