aiogram Telegram Bot API

repository·master·Indexed 43 days ago

https://github.com/aiogram/telegram-bot-api

An unofficial Docker image for the Telegram Bot API server. Provides a containerized environment to run the official Telegram Bot API, featuring configuration via environment variables such as TELEGRAM_API_ID and TELEGRAM_API_HASH, support for persistent storage at /var/lib/telegram-bot-api, and guidance on exposing downloaded files via Nginx.

Tokens
1.1K
Snippets
4
Records
5
Agent score
5%

What's inside aiogram telegram-bot-api

  1. Expose downloaded files via Nginx

    master

    By default, the tdlib/telegram-bot-api does not provide a way to download files from the API unless running in local-mode. To serve files to end-users, you must expose them using a separate web server like Nginx.

    A common pattern for local development is to run the Telegram Bot API and Nginx as separate containers using docker-compose, where Nginx serves the files stored by the API.

    # Example docker-compose structure
    services:
      telegram-bot-api:
        image: aiogram/telegram-bot-api
        # ... configuration ...
    
      nginx:
        image: nginx
        # ... configuration to serve files from the API volume ...
  2. Configure persistent storage for Telegram Bot API

    master

    To ensure server data is not lost when the container is removed, mount a volume to the server's working directory: /var/lib/telegram-bot-api.

    Note on Permissions: All files in this directory are owned by the user telegram-bot-api and group telegram-bot-api (uid: 101, gid: 101). This is compatible with the nginx Docker image.

    -v telegram-bot-api-data:/var/lib/telegram-bot-api
  3. Run Telegram Bot API using Docker

    master

    To start the Telegram Bot API server, you must first obtain an api-id and api-hash from https://my.telegram.org. You then provide these via the TELEGRAM_API_ID and TELEGRAM_API_HASH environment variables when running the container.

    Use the following command to run the container in detached mode, mapping port 8081, setting up persistent storage, and ensuring it restarts automatically:

    docker run -d -p 8081:8081 --name=telegram-bot-api --restart=always -v telegram-bot-api-data:/var/lib/telegram-bot-api -e TELEGRAM_API_ID=<api_id> -e TELEGRAM_API_HASH=<api-hash> aiogram/telegram-bot-api:latest
  4. Deploy Telegram Bot API using Docker Compose

    master

    You can use the following docker-compose.yml template to deploy the Telegram Bot API with persistent storage and required API credentials.

    version: '3.7'
    
    services:
      telegram-bot-api:
        image: aiogram/telegram-bot-api:latest
        environment:
          TELEGRAM_API_ID: "<api-id>"
          TELEGRAM_API_HASH: "<api-hash>"
        volumes:
          - telegram-bot-api-data:/var/lib/telegram-bot-api
        ports:
          - "8081:8081"
    
    volumes:
      telegram-bot-api-data:
  5. Configure Telegram Bot API via environment variables

    master

    The container is configured using environment variables. Below is a reference of the available configuration options:

    VariableDescription
    TELEGRAM_API_IDApplication identifier for Telegram API access.
    TELEGRAM_API_HASHApplication hash for Telegram API access.
    TELEGRAM_STATEnable statistics HTTP endpoint. Use -e TELEGRAM_STAT=1 -p 8082:8082 to enable and access via curl http://<host>:8082.
    TELEGRAM_FILTERFilter bots using the format "<remainder>/<modulo>". Allows only bots where bot_user_id % modulo == remainder.
    TELEGRAM_MAX_WEBHOOK_CONNECTIONSMaximum number of webhook connections allowed per bot.
    TELEGRAM_VERBOSITYLog verbosity level.
    TELEGRAM_LOG_FILEFilename where logs will be redirected (defaults to stdout/stderr).
    TELEGRAM_MAX_CONNECTIONSMaximum number of open file descriptors.
    TELEGRAM_PROXYHTTP proxy server for outgoing webhook requests (format: http://host:port).
    TELEGRAM_LOCALAllow the Bot API server to serve local requests.
    TELEGRAM_HTTP_IP_ADDRESSSet the IP address to listen on (e.g., TELEGRAM_HTTP_IP_ADDRESS: "[::]" for IPv6).
    TELEGRAM_HTTP_PORTThe port the API server listens on (default is 8081). Use this if running in host network mode.