(My Notes) Telegram Bot <pyTelegramBotAPI>
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
- Configure the boot in the Telegram app.
- Up, running, and listening.
- 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:
- Search for BotFather 👴🏽.
- BotFather will provide some instructions. From those instructions, I need to use
/newbot
. - I give a name to the bot “
no_my_first
”. - I give a username to the bot with the postfix
bot
, “no_my_first_bot
” - After assigning the user name BotFather will provide the Bot API key.
The response of the Bot father will be something like this:
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.
- Create an instance of the
TeleBot()
using the API key. - Define Message handler.
- 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.
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 /
.
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.
⚠️ 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.
Keep the bot running
To keep the bot running I need to add one command to the source file bot.infinity_polling()
.
3. Advance Bot replies.
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
- Create an instance of
ReplyKeyboardMarkup
. The instance will have 2 rowsrow_width=2
. - Create the Buttons
types.KeyboardButton(“option”)
. - Add to the markup object.
- Add the keyboard to the message.
Run a bot in Raspberry pi
I need to clone the repository in the Raspberry Pi.
The steps:
- Access to the raspberry pi by SSH.
- Git clones the repository ( an SSH key needs it, but GitHub has a tutorial for that).
- Install the dependencies with
pip
. - Run the program.
Final Thoughts
- There are other options not explored in this note, options as the telegram server.
- 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.
- 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
- Medium
- My LinkedIn
Ten articles before and after
[BEST] Tricks To Rank #1 Your Telegram Channel (2022) – Telegram 简体版
使用隧道中转 socks5 代理的方式来上 Telegram 存在安全风险吗? | – Telegram 简体版
Sending CloudWatch Alarm Status To Telegram – Telegram 简体版
12:24 AM | Time Conversion and Information | – Telegram 繁体版 下载
What Time is 11:31? | – Telegram 繁体版 下载
Telegram Announcement: The Upcoming PYDEX Launch – Telegram 官网下载
Telegram电报注册账号手机号收不到短信Telegram电报 – TG 下载
7793 Hours to Days | Telegram Web