
A FastAPI project template with Telegram authentication that:
- Uses the Telegram Login Widget for authentication.
- Demonstrates how to send messages to the current user.
- Uses PostgreSQL to store user data.
- Uses Ruff for linting and formatting.
- Is Dockerized.
You can start by either:
OR
-
Directly cloning this repository:
git clone https://github.com/dabarov/fastapi-telegram.git
For direct installation and usage:
-
Create a
.env
file based on the structure of the.env.example
file. -
Install the Python packages:
pip install -r requirements.txt
-
Run migrations after setting the database variables in
.env
:alembic upgrade head
-
Launch your app with the following command:
uvicorn app.main:app --port 8000 --reload
Alternatively, you can run the application in a Dockerized environment with the following command:
docker-compose -f compose.dev.yml up
- Add the Telegram Login Widget to your frontend based on this article.
- Point the
data-auth-url
to the/api/v1/auth/telegram/callback
endpoint.
The routes are available in the app/api/routes
directory.
There are two endpoints for authentication (app/api/routes/auth.py
):
GET /api/v1/auth/telegram/callback
handles the callback from the Telegram Login Widget.GET /api/v1/auth/logout
removes authentication for the user.
There are two endpoints to demonstrate the usage of the current user's data (app/api/routes/user.py
):
GET /api/v1/telegram/user/avatar
redirects to the current user's avatar image.POST /api/v1/telegram/send-message
sends a text message from the request body'smessage
field to the current user.
The dependencies are available in app/api/deps.py
:
SessionDep
for the database session.CurrentUserDep
for current user data from the database.TelegramBotDep
provides the Telegram bot object of typeBot
for sending messages, etc.