What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
You can build a first Telegram bot with a small Python script: create a bot with Telegram’s @BotFather, install the python-telegram-bot library, and run the script with polling. When you send the bot /start or a text message, it will reply. This is a local learning setup, not a production deployment.
How a Telegram bot works
Your Python program communicates with Telegram through the Bot API, an HTTPS interface that sends requests and receives JSON responses. The bot’s token authenticates those requests. In this guide, the python-telegram-bot library handles the API details and routes incoming updates—such as messages—to your code.
Telegram’s official “From BotFather to ‘Hello World’” tutorial describes the first steps as suitable for people who are just starting out, while noting that familiarity with the programming language helps.
The Tool Desk
Outbyte PC Repair FREERepair Windows errors before they cause bigger problemsFix Now →Outbyte Driver Updater FREEScan for outdated or missing drivers - takes under a minuteDriver Scan →Create a bot and protect its token
- Open Telegram and start a conversation with @BotFather, Telegram’s bot-creation tool.
- Send
/newbotand follow its prompts to choose a display name and a username. The username must meet Telegram’s requirements shown in the conversation. - Copy the token BotFather returns. Treat it like a password: anyone who obtains it may be able to control your bot.
Do not commit the token to source control, post it in a public chat, or include it in a screenshot. Telegram’s tutorial shows API requests containing a token in the URL, which is why a real token must remain private. For this local example, the program reads it from an environment variable rather than embedding it in the source file. If a token is exposed, use BotFather to replace or revoke it; consult Telegram’s current guidance in the BotFather conversation for the available account controls.
#1 Best Overall
Prepare Python and install the library
The current python-telegram-bot documentation identifies Python 3.10 or newer as supported and describes an asynchronous interface with high-level helpers in telegram.ext. These requirements can change; check the project documentation if you are using a different Python installation.
Create a project folder and a virtual environment using the method appropriate for your operating system, then activate that environment. A virtual environment keeps this project’s packages separate from other Python projects. Platform-specific activation commands are intentionally not shown here because they differ across shells and operating systems.
Rank #2
With the environment active, install the library using its documented command:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallpip install python-telegram-bot --upgrade
The first bot below uses only basic message handling, so it does not need optional extras for features such as webhooks, job scheduling, or HTTP/2.
Write a bot that replies to messages
Save this as bot.py. It registers a command handler for /start and a text-message handler for ordinary text. The handlers are asynchronous, matching the current library API.
import os
from telegram import Update
from telegram.ext import (
Application,
CommandHandler,
ContextTypes,
MessageHandler,
filters,
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message:
await update.message.reply_text("Hi! Send me a message and I’ll repeat it.")
async def echo(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
if update.message and update.message.text:
await update.message.reply_text(update.message.text)
def main() -> None:
token = os.environ.get("TELEGRAM_BOT_TOKEN")
if not token:
raise RuntimeError("Set the TELEGRAM_BOT_TOKEN environment variable first.")
application = Application.builder().token(token).build()
application.add_handler(CommandHandler("start", start))
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, echo))
application.run_polling()
if __name__ == "__main__":
main()
What the main pieces do
Application.builder()creates the library’s application, andadd_handler()registers code to handle matching updates.CommandHandler("start", start)routes the/startcommand to the greeting function.MessageHandler(filters.TEXT & ~filters.COMMAND, echo)selects text messages that are not commands. The echo function replies with the message text.run_polling()starts the application, fetches updates from Telegram, and manages the polling lifecycle and shutdown.
Set the token and run the bot
Set TELEGRAM_BOT_TOKEN in the same terminal session where you will run the script. Replace the example value with your actual token; do not include angle brackets.
For a POSIX-style shell such as bash or zsh:
export TELEGRAM_BOT_TOKEN="your-token-from-botfather"
python bot.py
For Windows PowerShell:
$env:TELEGRAM_BOT_TOKEN = "your-token-from-botfather"
python bot.py
Leave the process running. In Telegram, open your bot’s chat and send /start, then send a text message. The bot should greet you and echo the text. A user must initiate a conversation with the bot before the bot can reply in that chat.
Polling or webhooks?
| Approach | How updates arrive | Fit for a first bot |
|---|---|---|
| Polling | The running program repeatedly asks Telegram for updates. The library’s run_polling() provides a convenient lifecycle. |
Good for a local script and learning the update-and-reply loop. |
| Webhooks | Telegram delivers updates to a configured webhook endpoint; the library supports webhook operation. | A later option when you move toward deployment. It is not required for this local exercise. |
This guide does not cover hosting or production configuration. Those choices depend on where the program will run and how it will be made reachable.
Quick Recap
Best Value
Troubleshoot the first run
- The script says the token environment variable is missing: Set
TELEGRAM_BOT_TOKENin the terminal that launches Python. An environment variable set in another terminal or after starting the program will not be available to this process. - The bot does not respond: Confirm the script is still running and that you started the bot’s chat by sending
/start. Check that the token belongs to this bot. /startworks but text does not: The example handles text messages that are not commands. Other update types, such as photos or stickers, do not match that filter.- An older tutorial’s code looks different:
python-telegram-botmoved to an asynchronous architecture in version 20. This example follows the current documented style; older v13-era examples may use a different API and should not be mixed with it.

