In this user guide you will find all required information to run & edit Telegram Bot
Many thanks to all the mentors who taught me on the course:
Oleksii Klymenok
Vitalii Hrebennikov
Telegram Bot is written using Flask Application, NGROK, Postgres and NovaPoshta Api.
The code also contains AutoStart for a quick start of the Web application. The application uses
the model view controller structure and OOP principles.
Follow the steps below to get started your tests of Telegram Bot:
sudo apt update
sudo apt upgrade
sudo apt install postgresql
sudo service postgresql status
psql --version
psql --version
sudo -u postgres psql2)Create a new table named tgbot:
CREATE DATABASE tgbot;3)Create a new user with the CREATE USER command:
CREATE USER "user" WITH PASSWORD "user";4)Assign the required privileges to the user. For example, to give it full rights to all databases, you can run the following command:
GRANT ALL PRIVILEGES ON DATABASE tgbot TO "user";5)Connect to the tgbot database:
\c tgbot6)Activate the uuid-ossp extension with the following command:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";7)Exit the interactive Postgres shell by typing \q or pressing Ctrl+D.
\q
postgresql://user:user@localhost:5432/tgbot
pip install -r requirements.txt
python run.py
The code uses the standard Flask application structure with two additional files located in the app folder.
For package tracking, used the NovaPoshta API v.2 due to its versatility and
convenience.
First of all, program inside app.start_postgresql_ngrok.StartNGROK.register_webhook automatic set
Webhook to TelegramBot api (if you use Auto Start) with your NGROK url and register it.
Program send JSON to url: https://api.telegram.org/[BOT TOKEN]/setWebhook
The request body for the setWebhook method in JSON format is as
follows:
{
"url": "[YOUR NGROK URL]"
}
{
"apiKey": "[YOUR KEY]",
"modelName": "TrackingDocument",
"calledMethod": "getStatusDocuments",
"methodProperties": {
"Documents" : [
{
"DocumentNumber":"20400048799000",
"Phone":"380600000000"
}
,
{
"DocumentNumber":"20400048799001",
"Phone":"380600000000"
}
]
}
}
All the functionality of the telegram bot and its capabilities are in the file handler.py
I use Data Base structure from my Pet Project - Nike Shop Django and add telegram_id for table Profile,
telegram_id needs for save chat_id for user and authorized him.
I used Flask with SQLAlchemy to create my models in file app.models and Postgres Server like Database Management System.
Relational Schema:
In conclusion, this documentation provides a comprehensive overview of the Telegram bot.
Through the use of Flask, a lightweight and versatile web framework, and Flask-SQLAlchemy,
a powerful extension for integrating an SQL database with Flask applications, we have created
a seamless and efficient user experience.
By following this documentation, you have learned how to set up the necessary environment,
configure the bot settings, implement commands and callbacks, handle user interactions, and
leverage the database capabilities for storing and retrieving data.
Remember, this is just the beginning of what you can achieve with this Telegram bot.
Feel free to explore the possibilities and customize it according to your needs. Experiment
with additional features, integrate external APIs, and create unique functionalities that will
make your bot stand out.
Thank you for choosing this documentation as your guide. To further expand your knowledge
and discover more exciting projects, be sure to check out my GitHub repository.
There, you'll find wealth of interesting resources and open-source projects to explore.
Happy coding and best of luck with your Telegram bot journey!
Check out my GitHub to see another project:
Or check out my GitHub Pages to see my CV: