Michele Palamidessi
XXXX
What a Bot is?
How can I create a Telegram Bot?
What happens on telegram's servers?
What (should) happens on our server?
Some codes to interact with a bot
Python 3.x
Some python IDE
Telegram account
GIT
Postman
Telegram documentation says:
Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages, commands and inline requests. You control your bots using HTTPS requests to our bot API.
The original Bot, it will help you create new bots and change settings for existing ones.
One Bot to rule them all, One Bot to find them, One Bot to bring them all and in the darkness bind them
This command should be use to create a Bot.
It will ask you:
and it returns
This command create a new game.
You should:
This command change the list of commands supported by your bot.
Users see this commands as suggestion when type /
Toggle whether your bot can be added to groups or not
Set which message your bot will receive when added to a group.
Is an Http-based interface create for developers for buildings bots on Telegram.
https://core.telegram.org/bots/api
# Pseudo code, it does not works
import requests
requests.get("https://api.telegram.org/bot<Bot_token>/getUpdates")
request.post("https://api.telegram.org/bot<Bot_token>/sendMessage")
request.post("https://api.telegram.org/bot<Bot_token>/sendPhoto")
request.<method>("https://api.telegram.org/bot<Bot_token>/METHOD_NAME")The Python Package Index (PyPI) is a repository of software for the Python programming language.
- pip install boto3
- pip uninstall boto3
-pip freeze > requirements.txt
The venv module provides support for creating lightweight “virtual environments” with their own site directories.
Each virtual environment has its own Python binary.
# Clone repositories
git clone https://github.com/Palaxx/telegram_bot_lab.git
# Create virtual enviroments
cd telegram_bot_lab
#Create the virtual env
python3 -m venv venv
#Activate on linux
source venv/bin/activate
#Activate on windows
venv\Scripts\activate.bat
#Install dependencies
pip install -r requirements.txtgit checkout level0
Retrieve the messages
Using getUpdates endpoint and offset variable, we can retrieve the desired messages
The Telepot library
A library which help to write application using Telegram Bot API
The environment file
A file .env used to store all environment based variables
git checkout level1
Send message
Use the sendMessage with the chat_id of the received one to reply to the sender
Telepot MessageLoop
Using MessageLoop to retrieve always the last message, without the offset logic
{
"message_id":29,
"from":{
"id":22238267,
"is_bot":false,
"first_name":"Michele",
"last_name":"Palamidessi",
"username":"Palaxx",
"language_code":"it"
},
"chat":{
"id":123456,
"first_name":"Michele",
"last_name":"Palamidessi",
"username":"Palaxx",
"type":"private"
},
"date":1576518472,
"text":"ciao"
}git checkout level2
Botfather /setcommands
greetings - Greets the sender sayhelloto - Greets another person {name}
Detect commans and parsing parameters
def on_chat_message(msg):
# Some code before that
if '/sayhelloto' in text:
parameters = get_command_parameters(text)
bot.sendMessage(
chat_id,
"Hi {}, nice to meet you!".format(name)
)
def get_command_parameters(text:str) -> list:
parameters = text.split()
return parameters[1::]git checkout level3
Send photos
Using the sendPhoto of telepot library the bot could send a picture to the telegram client
Custom keyboards
Sometimes you want some more formal input from the user and this is where custom keyboards can become extremely useful.
git checkout level4
Going deeper into chat_id
chat id is created the first time a user talk with a bot
chat_id identify in an unique way the tupla of user|group and bot.
You can use chat id to send different messages based on user
The schedule package
Using previously saved chat_id and the power of schedule package, you can send custom messages on specific day/hour/minutes
git checkout level5
Goal:
Create working bot which notify the users on their preferred cryptocurrency every X hours.
The bot accept the registration of the user and required the selection of one cryptocurrency from a list.
Every X hours will retrieve the price from an external API and send the info to the user.
git checkout level6
Goal:
Integrate an html games into our bots, update the user score everytime a new highscore is reached.
Additional infos:
By Michele Palamidessi
Topics: What a Bot is? How can I create a Telegram Bot? What happens on telegram's servers? What (should) happens on our server? Explore some telegram bot features in 7 coding examples