![FastAPI-Telegram](https://private-user-images.githubusercontent.com/36531464/344425274-5bc2a850-481c-42f1-a99c-6ea13a84fd69.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE1MzU5NDksIm5iZiI6MTcyMTUzNTY0OSwicGF0aCI6Ii8zNjUzMTQ2NC8zNDQ0MjUyNzQtNWJjMmE4NTAtNDgxYy00MmYxLWE5OWMtNmVhMTNhODRmZDY5LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIxVDA0MjA0OVomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTg4N2ZjNzgwNGQyMGRkZGRiYTgxYWQyYWM2YTEyNzNmOGFhYzk0YmM0MjlkMjUxZjk0MDEwZDQyZTYxMTU2MWUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.nChgO25Rw2ssQYY9WJx4_fwroDQTNIiGwL3rvyNQ-oQ)
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.