Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open more actions menu

Repository files navigation

FastAPI-Telegram

Python PostgreSQL FastAPI Telegram Docker Ruff Template

FastAPI-Telegram

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.

Using the Template

You can start by either:

  • Clicking on the Use this template button and selecting Create a new repository.

    Use this template

OR

  • Directly cloning this repository:

    git clone https://github.com/dabarov/fastapi-telegram.git

Instalation

For direct installation and usage:

  1. Create a .env file based on the structure of the .env.example file.

  2. Install the Python packages:

    pip install -r requirements.txt
  3. Run migrations after setting the database variables in .env:

    alembic upgrade head
  4. 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

Usage

Adding to Your Frontend Application

  • 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.

Available Endpoints

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's message field to the current user.

Available FastAPI Dependencies

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 type Bot for sending messages, etc.

About

FastAPI project template with Telegram Login Widget for authentication

Topics

Resources

Stars

Watchers

Forks

Used by

Contributors

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.