Build a Telegram Bot with Python on DigitalOcean
Setting Up the Development Environment
Your Development Toolbox
Before you can build a bot, you need the right tools. The first is Python, the programming language we'll use. Most modern computers, especially Macs and Linux machines, come with Python pre-installed. You can check if you have it by opening your terminal or command prompt.
# Check for Python 3
python3 --version
# On some systems, it might just be 'python'
python --version
If you see a version number like Python 3.x.x, you're good to go. If not, you'll need to download and install the latest version of Python from the official website. Installing Python also gives you pip, a tool for installing other Python packages.
A Clean Workspace
Imagine you're building a LEGO spaceship. You wouldn't want your spaceship pieces mixed in with your LEGO castle pieces. A virtual environment in Python works the same way. It’s a dedicated, isolated space for each of your projects, ensuring the tools and libraries for one project don't interfere with another.
Let's create one for our bot. In your terminal, navigate to the folder where you want to keep your project and run this command:
# Creates a new folder named 'botenv' with a fresh Python environment
python3 -m venv botenv
This creates a new directory called botenv. To start using it, you need to 'activate' it.
# On macOS and Linux
source botenv/bin/activate
# On Windows
botenv\Scripts\activate
You'll know it worked because your command prompt will change to show (botenv) at the beginning. Now, any Python libraries you install will be contained within this environment. To exit the environment, just type deactivate.
Install the Telegram Library
With our clean workspace ready, we need the specific library that lets Python talk to Telegram's API. It's called python-telegram-bot. Make sure your virtual environment is active, then install it using pip.
Start the library with pip install python-telegram-bot.
pip install python-telegram-bot
Pip will download and install the package and any other packages it depends on. You're now equipped to write code that interacts with Telegram.
Meet the BotFather
In Telegram, chatbots are created using a special bot — @BotFather.
To create a new bot, you must speak to the one bot that rules them all: BotFather. He handles bot creation, management, and gives you the all-important API token.
Open your Telegram app and search for the user @BotFather. He's the official one with a blue checkmark. Start a chat and send the /newbot command.
BotFather will ask you for two things:
- A name: This is the display name for your bot, like "Weather Bot". It can contain spaces.
- A username: This is the unique identifier for your bot. It must be unique, end in
bot(e.g.,MyTestWeatherBot), and cannot contain spaces.
Once you choose a valid username, BotFather will congratulate you and give you your API token.
This token is a long string of letters and numbers. It's the key that proves your code is authorized to control your bot. Treat it like a password. Never share it publicly or commit it to a public code repository like GitHub.
API Token
noun
A unique identifier used to authenticate a user, developer, or calling program to an API. It acts as a secret key, authorizing requests to the service.
Copy this token and save it somewhere safe. You'll need it in the next step when we start writing our code. Your environment is now fully set up.