Skip to content

EZDAMIR/Telegram-Reg-Example

Repository files navigation

Telegram User Registration Bot with FastAPI Backend

This project is a demonstration of a user registration system built with a Telegram bot and a FastAPI backend. It utilizes a PostgreSQL database for data storage and is fully containerized with Docker for straightforward setup and deployment.

Features

  • User registration and deregistration via a Telegram bot.
  • RESTful API built with FastAPI for user management.
  • Asynchronous architecture using python-telegram-bot, and SQLAlchemy 2.0.
  • Database migrations handled by Alembic.
  • Containerized with Docker and Docker Compose for easy development and deployment.

Tech Stack

  • Backend: FastAPI
  • Bot Framework: python-telegram-bot
  • Database: PostgreSQL
  • ORM: SQLAlchemy 2.0 (async)
  • Migrations: Alembic
  • Containerization: Docker, Docker Compose
  • Dependency Management: Poetry

Project Structure

The project is organized into two main components:

  • fastapi-application/: Contains the FastAPI application.
    • api/: API endpoints (routers).
    • core/: Core application logic, configuration, and database models.
    • alembic/: Database migration scripts.
  • telegram-bot/: Contains the python-telegram-bot application.
    • handlers/: Bot command and message handlers.
    • keyboards/: Inline and reply keyboard layouts.
    • states/: Finite State Machine (FSM) states for conversations.
  • docker-compose.yml: Defines the services (API, bot, database) for the application.
  • *.Dockerfile: Docker build configurations for the API and bot services.

Getting Started

Prerequisites

  • Docker
  • Docker Compose

Installation and Running

  1. Clone the repository:

    git clone https://github.com/EZDAMIR/Telegram-Reg-Example.git
    cd Telegram-Reg-Example
  2. Configure Environment Variables: Create a .env file in the project root by copying the template.

    cp fastapi-application/env.template .env

    Open the .env file and replace YOUR_TELEGRAM_BOT_TOKEN_HERE with your actual Telegram Bot Token.

    TELEGRAM_BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN_HERE
    
    FASTAPI__DB__URL=postgresql+asyncpg://user:password@pg:5432/db
    FASTAPI__DB__ECHO=True
  3. Build and Run with Docker Compose: Execute the following command from the project root to build the images and start the containers.

    docker-compose up --build

    The application stack, including the database, API, and bot, will be started. Database migrations will be applied automatically on startup.

Usage

Telegram Bot Commands

  • /start - Displays a welcome message.
  • /register - Initiates the user registration process.
  • /unregister - Removes your data from the system.
  • /help - Shows a help message with available commands.

API Documentation

Once the application is running, the auto-generated API documentation is available at:

  • Swagger UI: http://localhost:8000/docs
  • ReDoc: http://localhost:8000/redoc

About

Simple telegram bot to collect all available information about the user

Resources

Stars

Watchers

Forks