Creating a Telegram bot is a fun and quick way to prototype an idea, explore a new API, or create a personal utility app, among many other possibilities. Here, I’ll feature a series of examples showing how to get started with the TeleBot API in Python and potential practical applications. Finally, we will also consider possible future projects using this API.
Getting started with TeleBot
First, we will need to choose an API to interact with the Telegram Bot API. Many great APIs make working with the Telegram Bot API incredibly simple. For this example, we will use TeleBot, which is written in Python.
Second, we will need to initiate a conversation with the BotFather to create and name our bot, as well as receive an API key. To do this, open the Telegram app and search for
BotFather in the search bar. Next, type
/start and then
/newbot and follow the prompts. Finally, after naming your bot, you will be given an API token which you can use to interact with your newly-created bot. Make note of this, as we will need it in the following section.
A Simple Bot Setup
Next, we will look at connecting to our bot via the TeleBot API and creating a simple message command. Open your favorite editor and type in the following:
import telebot gives us access to the TeleBot API, while
decouple allows us to easily store parameters in a
.env file. (Use
pip install python-decouple to install decouple.) Create a
.env file and add the following line:
xxx is the API key that you received earlier from the BotFather. Line 4 grabs the API key from the
.env file, while line 5 initializes a connection to your bot.
Next, lines 7-9 handle the actual command that our bot can process. In our example, if the bot receives the “hello” command, it will respond by sending the message, “Hello there!” to the current chat. Finally, line 11 causes the bot to continuously watch for input.
Open a command line prompt wherever your file is located and run your code with
python3 file_name.py. Next, open Telegram and search for the name of your bot and begin a conversation. Finally, type
/hello (bot commands will start with a forward slash).
Congratulations! Our bot has responded with the expected output. One final thing to note is that we can add additional commands to the
message_handler. For example, we could change line 7 to be the following:
Then we can also type
/hi to have the bot output the string “Hello there!”
Creating a Useful Bot
Even from the simple hello bot example, it is clear that we can construct more complex applications rapidly. We will look at one more small example that integrates a third-party API to query Japanese kanji. The following is the code we’ll explore:
The base structure of the
message_handler is the same, but this time we will also import requests (to make simple HTTP requests) and random to select a random kanji from the API data. Make note of line 14 where we query the kanjiapi.dev API and line 17 where we grab a random kanji from the list of joyo kanji that is returned. Next, we make another request to the kanji API on line 18 to obtain the meaning of the kanji. After querying the returned JSON data for a list of meanings on line 20 and some text formatting on lines 21 – 25, we have created a response of “kanji: meaning_1, meaning_2 ,…”.
After running this code, we can navigate back to Telegram and start a new conversation with our bot. Typing
/kanji will elicit the following response from the bot:
Furthermore, because we are selecting a random joyo kanji in our command invocation, subsequent commands will yield different kanji.
In just a few lines, we have created a simple Telegram bot that we can use to give us a “word of the day” to facilitate Japanese learning. From this point, we could add many more commands to increase the usefulness of our bot, making it as simple or complicated as we desire.
Create Your Own Bot Projects
It is easy to get started with the Telegram Bot API using Telebot. Furthermore, Telebot makes prototyping new app ideas, exploring APIs, and creating other utilities enjoyable. Some interesting potential applications could be stock/crypto price tracking and trading bots or bots to automate game playing. I hope you enjoy experimenting with Telegram bots!