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
.envfile based on the structure of the.env.examplefile. -
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-urlto the/api/v1/auth/telegram/callbackendpoint.
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/callbackhandles the callback from the Telegram Login Widget.GET /api/v1/auth/logoutremoves 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/avatarredirects to the current user's avatar image.POST /api/v1/telegram/send-messagesends a text message from the request body'smessagefield to the current user.
The dependencies are available in app/api/deps.py:
SessionDepfor the database session.CurrentUserDepfor current user data from the database.TelegramBotDepprovides the Telegram bot object of typeBotfor sending messages, etc.
