Skip to content

Commit

Permalink
add local bot api
Browse files Browse the repository at this point in the history
  • Loading branch information
avoonix committed Aug 17, 2024
1 parent 43c11fd commit 53ca1d8
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/local-bot-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Bot API Docker Image

on:
workflow_dispatch:
inputs:
platform:
description: 'Platform'
required: true
default: 'arm64'
type: choice
options:
- arm64
- amd64

jobs:
docker_inference:
runs-on: ubuntu-latest
permissions:
packages: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push (local-bot-api)
uses: docker/build-push-action@v5
with:
context: local-bot-api
platforms: linux/${{ inputs.platform }}
push: true
tags: ghcr.io/avoonix/fuzzle-bot-api:latest-${{ inputs.platform }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ I organize Telegram stickers with e621 tags.

The easiest way to deploy the bot yourself is with Docker compose.

> OUTDATED - a better example should come soon
```yml
version: '3.8'
services:
Expand Down
21 changes: 21 additions & 0 deletions local-bot-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# adapted from https://github.com/lukaszraczylo/tdlib-telegram-bot-api-docker/blob/main/Dockerfile
FROM alpine:latest AS build
WORKDIR /srv
RUN apk add --no-cache alpine-sdk linux-headers git zlib-dev openssl-dev gperf php cmake
RUN git clone --depth 1 --recurse-submodules --shallow-submodules https://github.com/tdlib/telegram-bot-api.git /srv
COPY patches /patches
RUN git apply /patches/*
RUN mkdir -p build && cd build && cmake -DCMAKE_INSTALL_PREFIX:PATH=../tdlib -DCMAKE_BUILD_TYPE=Release ..
RUN cd /srv/build && cmake --build . --target install

FROM alpine:latest
RUN apk add --no-cache zlib-dev openssl-dev libstdc++
COPY --from=build /srv/build/telegram-bot-api /srv/telegram-bot-api
VOLUME /data
EXPOSE 80
RUN addgroup -g 101 nginx
RUN adduser -G nginx -H -u 101 --disabled-password nginx
# same user as nginx so that it can access the files from the volume
ENTRYPOINT ["/srv/telegram-bot-api", "--dir=/data", "--local", "--http-port=80", "--username=nginx", "--groupname=nginx"]

# run on host: sudo chown 101:101 telegram-bot-api-data
13 changes: 13 additions & 0 deletions local-bot-api/patches/add-custom-id.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp
index 86bd10d..350ccf9 100644
--- a/telegram-bot-api/Client.cpp
+++ b/telegram-bot-api/Client.cpp
@@ -4277,6 +4277,8 @@ class Client::JsonStickerSet final : public td::Jsonable {
object("sticker_type", type);
object("contains_masks", td::JsonBool(type == "mask"));

+ object("__custom__id", std::to_string(sticker_set_->id_));
+
object("stickers", JsonStickers(sticker_set_->stickers_, client_));
}

67 changes: 67 additions & 0 deletions nginx/default.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# use $sanitized_request instead of $request to hide Telegram token
# log_format token_filter '$remote_addr - $remote_user [$time_local] '
# '"$sanitized_request" $status $body_bytes_sent '
# '"$http_referer" "$http_user_agent"';


upstream telegram-bot-api {
server local-bot-api:80;
}

server {
listen 80;
server_name _;

rewrite_log on;
chunked_transfer_encoding on;
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
client_max_body_size 2G;
client_body_buffer_size 30M;
keepalive_timeout 0;

# set $sanitized_request $request;
# if ( $sanitized_request ~ (\w+)\s(.*\/bot\d+):[-\w]+\/(\S+)\s(.*) ) {
# set $sanitized_request "$1 $2:<hidden-token>/$3 $4";
# }
# access_log /var/log/nginx/access.log token_filter;
access_log off;


# local mode
location ~* \/file\/bot\d+:(.*)\/data\/(.*) {
rewrite ^/file\/bot(.*)\/data\/(.*) /$2 break;
try_files $uri @files;
}

location ~* \/file\/bot\d+:(.*) {
rewrite ^/file\/bot(.*) /$1 break;
try_files $uri @files;
}

location / {
try_files $uri @api;
}

location @files {
root /data;
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 64 8k;
gzip_http_version 1.1;
gzip_min_length 1100;
}

location @api {
proxy_pass http://telegram-bot-api;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}

0 comments on commit 53ca1d8

Please sign in to comment.