DEV Community

Joy Biswas
Joy Biswas

Posted on

How I Self-Hosted the Telegram Bot API with Docker to Bypass 50MB Upload Limits

I was writing a personal Telegram bot for uploading files in Telegram. But the issue I faced was that Telegram bots have a 50MB upload limit. There are two ways to handle this issue. One is using the MTProto framework. Another issue with the MTProto framework is that it's super slow. The second option is self-hosting the Telegram API locally and bypassing the 50MB upload limit. I'm going to stick with self-hosting. As usual, most of the time I use Docker to self-host everything. No need to worry about unnecessary dependencies on my PC.

Dockerfile

Dockerfile

FROM alpine:latest AS build
WORKDIR /app

RUN apk update && \
        apk upgrade && \
        apk add --no-cache alpine-sdk linux-headers git zlib-dev openssl-dev gperf cmake

RUN git clone --recursive https://github.com/tdlib/telegram-bot-api.git

RUN rm -rf /app/telegram-bot-api/build && \
        mkdir /app/telegram-bot-api/build && \
        cd /app/telegram-bot-api/build && \
        cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/app/telegram-bot-api .. && \
        cmake --build /app/telegram-bot-api/build/ --target install

FROM alpine:latest AS runtime
WORKDIR /app

ARG API_ID
ARG API_HASH
ENV API_ID=${API_ID}
ENV API_HASH=${API_HASH}

RUN echo ${API_ID}
RUN echo ${API_HASH}

RUN apk update && \
        apk upgrade && \
        apk add --no-cache alpine-sdk linux-headers git zlib-dev openssl-dev gperf cmake

COPY --from=build /app/telegram-bot-api/bin/telegram-bot-api /app/telegram-bot-api

EXPOSE 8081

ENTRYPOINT ["/bin/sh", "-c"]
CMD ["/app/telegram-bot-api --api-id=$API_ID --api-hash=$API_HASH --local"]
Enter fullscreen mode Exit fullscreen mode

docker-compose.yaml

services:
  telegram_bot_api:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        API_ID: ${API_ID}
        API_HASH: ${API_HASH}
    image: telegram-bot-api
    container_name: telegram-bot-api
    env_file:
      - .env
    ports:
      - "8080:8081"
    restart: unless-stopped
Enter fullscreen mode Exit fullscreen mode

You also need a .env file:

API_ID=12344566
API_HASH=YourAPIHash
Enter fullscreen mode Exit fullscreen mode

Obtain your api_id and api_hash from Telegram's official site: https://my.telegram.org/auth

Testing Your Setup

To check if everything is working fine, test your self-hosted API with:

curl http://localhost:port/bot<token>/getMe
Enter fullscreen mode Exit fullscreen mode

Replace <token> with your actual bot token. If the setup is working correctly, you should receive a JSON response with your bot's information.

For more detailed information about self-hosting the Telegram Bot API, visit the official documentation: https://core.telegram.org/bots/api#using-a-local-bot-api-server

Official repo of tdlib/telegram-bot-api.
If you want to build without Docker, follow the steps here: https://tdlib.github.io/telegram-bot-api/build.html

Top comments (0)