(My Notes) Telegram Bot <pyTelegramBotAPI>

Victor Fernandez
5 min readMay 7, 2022
Photo by Jéan Béller on Unsplash

The Problem:

I want to have an output for some of my projects, but I don’t want something like a website or an app running in AWS. I want to run it from a Raspberry Pi.

The Solution:

Use the Telegram bot as a client to interact with my application. If possible, use it such that I don’t need to port forward or do any extra configuration to the raspberry pi running it.

The Implementation:

Of all the options out there, the one I decided to implement PyTelegramBotAPI. I will create a GitHub repository, prepare a raspberry pi with a headless Raspbian and clone the mentioned repository.

The Steps

  1. Configure the boot in the Telegram app.
  2. Up, running, and listening.
  3. Advance Bot replies.

1. Configure the boot in the Telegram app.

To create a telegram bot, I need to use BotFather.

The steps to create the bot will be:

  1. Search for BotFather 👴🏽.
  2. BotFather will provide some instructions. From those instructions, I need to use /newbot.
  3. I give a name to the bot “no_my_first”.
  4. I give a username to the bot with the postfix bot, “no_my_first_bot
  5. After assigning the username BotFather will provide the Bot API key.
Creating a new Bot

The response of the Bot father will be something like this:

Bot API

2. Up, running, and listening.

The package pyTelegramBotAPI encapsulates the telegram API, with this package I can send messages and documents and several ways to parse or listen to incoming messages.

  1. Create an instance of the TeleBot() using the API key.
  2. Define Message handler.
  3. Keep the bot running.

Create an instance of the TeleBot() using the API key.

First, I need to install the module.

pip install pyTelegramBotAPI

Second, I need to create an instance of the TeleBot class.

Telegram bot

Define Message handler.

To create a message handler, I will use the decorator @bot.message_handler().
The function decorated with this handler can have any name.

The only requirement is a single parameter, the message itself.

All handlers are tested in the order in which they are declared — PyTelegramBotAPI Documentation

Handler with commands

This handler will handle or react to messages that include a command. Commands are words prefixed with / the list of commands handled by a handler is defined in the decorator as a list after the parameter commands this without /.

Telegram bot with message handler (Commands)
Answer to a Message handler (commands)

Handler with function

This handler will handle the message if the result of a function is true.

Here is an example of a function that will return always true, so the handler will echo each message that is not a command.

Telegram bot with message handler (functions)
Answer to a Message handler (functions)

⚠️ Handler with a Function that expects a keyword

I can create a different function without a decorator that will track the presence of a specific word.

reply_to message
There is a typo in the response :(

Keep the bot running

To keep the bot running I need to add one command to the source file bot.infinity_polling().

The source code for a simple bot

3. Advance Bot replies.

Photo by Daniel Lloyd Blunk-Fernández on Unsplash

The documentation provides extensive information about the telegram API, but what I’m interested in are the types and something callReply markup, this will allow me to provide different types of responses like audio, videos, and keyboard options.

Each functionsend_xyz contains an argument reply_markup. This argument receives an instance of ReplyKeyboardMarkup, ReplyKeyboardRemove or ForceReply.

Here is an example of what I what to do.

The Bot will reply with a custom keyword

  1. Create an instance of ReplyKeyboardMarkup. The instance will have 2 rows row_width=2.
  2. Create the Buttons types.KeyboardButton(“option”).
  3. Add to the markup object.
  4. Add the keyboard to the message.
The bot provides a custom keyboard as a response to the word options in the message
Returning custom keyboard

Run a bot in Raspberry pi

I need to clone the repository in the Raspberry Pi.

The steps:

  1. Access to the raspberry pi by SSH.
  2. Git clones the repository ( an SSH key needs it, but GitHub has a tutorial for that).
  3. Install the dependencies with pip.
  4. Run the program.

Final Thoughts

  1. There are other options not explored in this note, options as the telegram server.
  2. The API allows the usage of WebSockets but the setup and usage are more complicated and I don’t need the benefits of the WebSockets.
  3. Middleware handlers are useful to further manage the message but since the purpose of the bots is to provide basic interaction for my projects on raspberry pi, I don’t see it necessary to implement.

Victor Fernandez

--

--

Victor Fernandez

I’m Victor, I’m a Field Application Engineer for a CCTV manufacturer. I love Raspberry Pi, Python, and Microcontrollers and I write about my personal projects.