Skip to content
Proyek

donbarbos/telegram-bot-template

Lihat di GitHub

Tentang

๐Ÿค– Template for telegram bot using postgres, pgbouncer, redis, docker, amplitude, prometheus, grafana, CI with admin panel

Sistem dan infrastruktur

Cara angka ini diukur

Yang dapat Anda lakukan

  • Admin Panel based on Flask-Admin-Dashboard (Flask-Admin + AdminLTE = โค๏ธ )
  • Product Analytics System: using Amplitude or Posthog or Google Analytics
  • Performance Monitoring System: using Prometheus and Grafana
  • Tracking System: using Sentry

Pertumbuhan bintang terukur

dalam 24 jam
0
dalam 7 hari
+1
dalam 15 hari
0
dalam 1 bulan
Mengumpulkan riwayat

Perubahan bersih pada periode yang tumpang tindih, bukan total harian. Pengukuran yang belum tersedia dibiarkan kosong. Bintang menunjukkan perhatian, bukan kualitas.

README.md

Asli

Telegram bot template

Best way to create a scalable telegram bot with analytics

GitHub tag (latest SemVer) Linters Status Docker Build Status Python License Code style Package manager

โœจ Features

๐Ÿš€ How to Use

๐Ÿณ Running in Docker (recommended method)

  • configure environment variables in .env file

  • start services

    docker compose up -d --build
    

๐Ÿ’ป Running on Local Machine

  • set environment and install dependencies using uv (you can find branch with Poetry here)

    uv sync --frozen --all-groups
    
  • start the necessary services (at least your database and redis)

  • configure environment variables in .env file

  • start telegram bot

    uv run python -m bot
    
  • start admin panel

    uv run gunicorn -c admin/gunicorn_conf.py
    
  • make migrations

    uv run alembic upgrade head
    

๐ŸŒ Environment variables

to launch the bot you only need a token bot, database and redis settings, everything else can be left out

name description
BOT_TOKEN Telegram bot API token
RATE_LIMIT Maximum number of requests allowed per minute for rate limiting
DEBUG Enable or disable debugging mode (e.g., True or False)
USE_WEBHOOK Flag to indicate whether the bot should use a webhook for updates (e.g., True or False)
WEBHOOK_BASE_URL Base URL for the webhook
WEBHOOK_PATH Path to receive updates from Telegram
WEBHOOK_SECRET Secret key for securing the webhook communication
WEBHOOK_HOST Hostname or IP address for the main application
WEBHOOK_PORT Port number for the main application
ADMIN_HOST Hostname or IP address for the admin panel
ADMIN_PORT Port number for the admin panel
DEFAULT_ADMIN_EMAIL Default email for the admin user
DEFAULT_ADMIN_PASSWORD Default password for the admin user
SECURITY_PASSWORD_HASH Hashing algorithm for user passwords (e.g., bcrypt)
SECURITY_PASSWORD_SALT Salt value for user password hashing
DB_HOST Hostname or IP address of the PostgreSQL database
DB_PORT Port number for the PostgreSQL database
DB_USER Username for authenticating with the PostgreSQL database
DB_PASS Password for authenticating with the PostgreSQL database
DB_NAME Name of the PostgreSQL database
REDIS_HOST Hostname or IP address of the Redis database
REDIS_PORT Port number for the Redis database
REDIS_PASS Password for authenticating with the Redis database
SENTRY_DSN Sentry DSN (Data Source Name) for error tracking
AMPLITUDE_API_KEY API key for Amplitude analytics
POSTHOG_API_KEY API key for PostHog analytics
PROMETHEUS_PORT Port number for the Prometheus monitoring system
GRAFANA_PORT Port number for the Grafana monitoring and visualization platform
GRAFANA_ADMIN_USER Admin username for accessing Grafana
GRAFANA_ADMIN_PASSWORD Admin password for accessing Grafana

๐Ÿ“‚ Project Folder Structure

.
โ”œโ”€โ”€ admin # Source code for admin panel
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ app.py # Main application module for the admin panel
โ”‚   โ”œโ”€โ”€ config.py # Configuration module for the admin panel
โ”‚   โ”œโ”€โ”€ Dockerfile # Dockerfile for admin panel
โ”‚   โ”œโ”€โ”€ gunicorn_conf.py # Gunicorn configuration file for serving admin panel
โ”‚   โ”œโ”€โ”€ static # Folder for static assets
โ”‚   โ”‚   โ”œโ”€โ”€ css/
โ”‚   โ”‚   โ”œโ”€โ”€ fonts/
โ”‚   โ”‚   โ”œโ”€โ”€ img/
โ”‚   โ”‚   โ”œโ”€โ”€ js/
โ”‚   โ”‚   โ””โ”€โ”€ plugins/
โ”‚   โ”œโ”€โ”€ templates # HTML templates for the admin panel
โ”‚   โ”‚   โ”œโ”€โ”€ admin/
โ”‚   โ”‚   โ”œโ”€โ”€ index.html
โ”‚   โ”‚   โ”œโ”€โ”€ my_master.html
โ”‚   โ”‚   โ””โ”€โ”€ security/
โ”‚   โ””โ”€โ”€ views # Custom View modules for handling web requests
โ”‚       โ”œโ”€โ”€ __init__.py
โ”‚       โ””โ”€โ”€ users.py
โ”‚
โ”œโ”€โ”€ bot # Source code for Telegram Bot
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ __main__.py # Main entry point to launch the bot
โ”‚   โ”œโ”€โ”€ analytics/ # Interaction with analytics services (e.g., Amplitude or Google Analytics)
โ”‚   โ”œโ”€โ”€ cache/ # Logic for using Redis cache
โ”‚   โ”œโ”€โ”€ core/ # Settings for application and other core components
โ”‚   โ”œโ”€โ”€ database/ # Database functions and SQLAlchemy Models
โ”‚   โ”œโ”€โ”€ filters/ # Filters for processing incoming messages or updates
โ”‚   โ”œโ”€โ”€ handlers/ # Handlers for processing user commands and interactions
โ”‚   โ”œโ”€โ”€ keyboards # Modules for creating custom keyboards
โ”‚   โ”‚   โ”œโ”€โ”€ default_commands.py # Default command keyboards
โ”‚   โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”‚   โ”œโ”€โ”€ inline/ # Inline keyboards
โ”‚   โ”‚   โ””โ”€โ”€ reply/ # Reply keyboards
โ”‚   โ”œโ”€โ”€ locales/ # Localization files for supporting multiple languages
โ”‚   โ”œโ”€โ”€ middlewares/ # Middleware modules for processing incoming updates
โ”‚   โ”œโ”€โ”€ services/ # Business logic for application
โ”‚   โ””โ”€โ”€ utils/ # Utility functions and helper modules
โ”‚
โ”œโ”€โ”€ migrations # Database Migrations managed by Alembic
โ”‚   โ”œโ”€โ”€ env.py # Environment setup for Alembic
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ README
โ”‚   โ”œโ”€โ”€ script.py.mako # Script template for generating migrations
โ”‚   โ””โ”€โ”€ versions/ # Folder containing individual migration scripts
โ”‚
โ”œโ”€โ”€ configs # Config folder for Monitoring (Prometheus, Node-exporter and Grafana)
โ”‚   โ”œโ”€โ”€ grafana # Configuration files for Grafana
โ”‚   โ”‚   โ””โ”€โ”€ datasource.yml
โ”‚   โ””โ”€โ”€ prometheus # Configuration files for Prometheus
โ”‚       โ””โ”€โ”€ prometheus.yml
โ”‚
โ”œโ”€โ”€ scripts/ # Sripts folder
โ”œโ”€โ”€ Makefile # List of commands for standard
โ”œโ”€โ”€ alembic.ini # Configuration file for migrations
โ”œโ”€โ”€ docker-compose.yml # Docker Compose configuration file for orchestrating containers
โ”œโ”€โ”€ Dockerfile # Dockerfile for Telegram Bot
โ”œโ”€โ”€ LICENSE.md # License file for the project
โ”œโ”€โ”€ uv.lock # Lock file for UV dependency management
โ”œโ”€โ”€ pyproject.toml # Configuration file for Python projects, including build tools, dependencies, and metadata
โ””โ”€โ”€ README.md # Documentation

๐Ÿ”ง Tech Stack

  • sqlalchemy โ€” object-relational mapping (ORM) library that provides a set of high-level API for interacting with relational databases
  • asyncpg โ€” asynchronous PostgreSQL database client library
  • aiogram โ€” asynchronous framework for Telegram Bot API
  • flask-admin โ€” simple and extensible administrative interface framework
  • loguru โ€” third party library for logging in Python
  • uv โ€” development workflow
  • docker โ€” to automate deployment
  • postgres โ€” powerful, open source object-relational database system
  • pgbouncer โ€” connection pooler for PostgreSQL database
  • redis โ€” in-memory data structure store used as a cache and FSM
  • prometheus โ€” time series database for collecting metrics from various systems
  • grafana โ€” visualization and analysis from various sources, including Prometheus

โญ Star History

๐Ÿ‘ท Contributing

First off, thanks for taking the time to contribute! Contributions are what makes the open-source community such an amazing place to learn, inspire, and create. Any contributions you make will benefit everybody else and are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork this repository
  2. Create a branch
  3. Commit your changes
  4. Push your commits to the branch
  5. Submit a pull request

๐Ÿ“ License

Distributed under the MIT license. See LICENSE for more information.

๐Ÿ“ข Contact

donbarbos: [email protected]

Jelajahi proyek terkait

Memuatโ€ฆ