dragg2 commited on
Commit
b324aaa
·
verified ·
1 Parent(s): dcb12f1

Upload 4 files

Browse files
Files changed (4) hide show
  1. Dockerfile +15 -16
  2. README.md +6 -5
  3. app.py +59 -58
  4. start.sh +14 -16
Dockerfile CHANGED
@@ -1,18 +1,17 @@
1
-
2
- FROM aiogram/telegram-bot-api:latest
3
-
4
- RUN apk add --no-cache python3 py3-pip && \
5
- python3 -m venv /opt/venv && \
6
- /opt/venv/bin/pip install --no-cache-dir --upgrade pip && \
7
- /opt/venv/bin/pip install --no-cache-dir fastapi uvicorn httpx
8
-
9
  ENV PATH="/opt/venv/bin:$PATH"
10
- ENV TELEGRAM_HTTP_PORT=7860
11
  WORKDIR /app
12
-
13
- COPY app.py /app/app.py
14
- COPY start.sh /app/start.sh
15
- RUN chmod +x /app/start.sh
16
-
17
- EXPOSE 7860
18
- CMD ["/app/start.sh"]
 
1
+
2
+ FROM aiogram/telegram-bot-api:latest
3
+
4
+ RUN apk add --no-cache python3 py3-pip && \
5
+ python3 -m venv /opt/venv && \
6
+ /opt/venv/bin/pip install --no-cache-dir --upgrade pip && \
7
+ /opt/venv/bin/pip install --no-cache-dir fastapi uvicorn httpx
8
+
9
  ENV PATH="/opt/venv/bin:$PATH"
 
10
  WORKDIR /app
11
+
12
+ COPY app.py /app/app.py
13
+ COPY start.sh /app/start.sh
14
+ RUN chmod +x /app/start.sh
15
+
16
+ EXPOSE 7860
17
+ CMD ["/app/start.sh"]
README.md CHANGED
@@ -1,10 +1,11 @@
1
- ---
2
- title: Dragontgbot
3
- emoji: 🐢
4
- colorFrom: purple
5
- colorTo: red
6
  sdk: docker
7
  pinned: false
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ ---
2
+ title: Dragontgbot
3
+ emoji: 🐢
4
+ colorFrom: purple
5
+ colorTo: red
6
  sdk: docker
7
  pinned: false
8
+ app_port: 7860
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py CHANGED
@@ -1,58 +1,59 @@
1
-
2
- from fastapi import FastAPI, Request
3
- from fastapi.responses import StreamingResponse
4
- import httpx
5
-
6
- UPSTREAM = "http://127.0.0.1:7860"
7
- app = FastAPI()
8
-
9
- @app.get("/")
10
- async def root():
11
- return {"ok": True, "hint": "use /tg/ prefix, e.g. /tg/bot<TOKEN>/getMe"}
12
-
13
- @app.api_route("/tg/{path:path}", methods=["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"])
14
- async def proxy(path: str, request: Request):
15
- url = f"{UPSTREAM}/{path}"
16
-
17
- params = list(request.query_params.multi_items())
18
-
19
- headers = dict(request.headers)
20
- headers.pop("host", None)
21
-
22
- async def iter_request_body():
23
- async for chunk in request.stream():
24
- yield chunk
25
-
26
- async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client:
27
- async with client.stream(
28
- request.method,
29
- url,
30
- params=params,
31
- headers=headers,
32
- content=iter_request_body(),
33
- ) as r:
34
-
35
- passthrough_headers = {}
36
- for k, v in r.headers.items():
37
- lk = k.lower()
38
- if lk in [
39
- "content-type",
40
- "content-length",
41
- "content-disposition",
42
- "accept-ranges",
43
- "content-range",
44
- "etag",
45
- "cache-control",
46
- "last-modified",
47
- ]:
48
- passthrough_headers[k] = v
49
-
50
- async def iter_response():
51
- async for chunk in r.aiter_bytes():
52
- yield chunk
53
-
54
- return StreamingResponse(
55
- iter_response(),
56
- status_code=r.status_code,
57
- headers=passthrough_headers,
58
- )
 
 
1
+
2
+ from fastapi import FastAPI, Request
3
+ from fastapi.responses import StreamingResponse
4
+ import httpx
5
+
6
+ # 内部真实 Bot API Server(telegram-bot-api)监听端口
7
+ UPSTREAM = "http://127.0.0.1:8081"
8
+ app = FastAPI()
9
+
10
+ @app.get("/")
11
+ async def root():
12
+ return {"ok": True, "hint": "use /tg/ prefix, e.g. /tg/bot<TOKEN>/getMe"}
13
+
14
+ @app.api_route("/tg/{path:path}", methods=["GET","POST","PUT","PATCH","DELETE","HEAD","OPTIONS"])
15
+ async def proxy(path: str, request: Request):
16
+ url = f"{UPSTREAM}/{path}"
17
+
18
+ params = list(request.query_params.multi_items())
19
+
20
+ headers = dict(request.headers)
21
+ headers.pop("host", None)
22
+
23
+ async def iter_request_body():
24
+ async for chunk in request.stream():
25
+ yield chunk
26
+
27
+ async with httpx.AsyncClient(timeout=None, follow_redirects=True) as client:
28
+ async with client.stream(
29
+ request.method,
30
+ url,
31
+ params=params,
32
+ headers=headers,
33
+ content=iter_request_body(),
34
+ ) as r:
35
+
36
+ passthrough_headers = {}
37
+ for k, v in r.headers.items():
38
+ lk = k.lower()
39
+ if lk in [
40
+ "content-type",
41
+ "content-length",
42
+ "content-disposition",
43
+ "accept-ranges",
44
+ "content-range",
45
+ "etag",
46
+ "cache-control",
47
+ "last-modified",
48
+ ]:
49
+ passthrough_headers[k] = v
50
+
51
+ async def iter_response():
52
+ async for chunk in r.aiter_bytes():
53
+ yield chunk
54
+
55
+ return StreamingResponse(
56
+ iter_response(),
57
+ status_code=r.status_code,
58
+ headers=passthrough_headers,
59
+ )
start.sh CHANGED
@@ -1,30 +1,28 @@
1
-
2
- #!/bin/sh
3
- set -e
4
-
5
- if [ -z "${TELEGRAM_API_ID}" ] || [ -z "${TELEGRAM_API_HASH}" ]; then
6
- echo "Missing TELEGRAM_API_ID / TELEGRAM_API_HASH"
7
- exit 1
8
- fi
9
-
10
- # HF persistent storage: /data (如果你开了持久化)
11
- WORK_DIR="${TELEGRAM_WORK_DIR:-/data/telegram-bot-api}"
12
- TEMP_DIR="${TELEGRAM_TEMP_DIR:-/data/telegram-bot-api-tmp}"
13
  mkdir -p "$WORK_DIR" "$TEMP_DIR"
14
 
15
  VERBOSITY="${TELEGRAM_VERBOSITY:-4}"
16
- LOG_FILE="${TELEGRAM_LOG_FILE:-/data/telegram-bot-api.log}"
17
-
18
  telegram-bot-api \
19
  --api-id="${TELEGRAM_API_ID}" \
20
  --api-hash="${TELEGRAM_API_HASH}" \
21
  --dir="$WORK_DIR" \
22
  --temp-dir="$TEMP_DIR" \
23
  --http-ip-address=127.0.0.1 \
24
- --http-port=7860 \
25
  --local \
26
  --verbosity="$VERBOSITY" \
27
- --log="$LOG_FILE" \
28
  --username=telegram-bot-api \
29
  --groupname=telegram-bot-api &
30
 
 
1
+
2
+ #!/bin/sh
3
+ set -e
4
+
5
+ if [ -z "${TELEGRAM_API_ID}" ] || [ -z "${TELEGRAM_API_HASH}" ]; then
6
+ echo "Missing TELEGRAM_API_ID / TELEGRAM_API_HASH"
7
+ exit 1
8
+ fi
9
+
10
+ # 你说“不用持久化也行”,那就用容器内可写目录(避免 /data 没开持久化时报权限错误)
11
+ WORK_DIR="${TELEGRAM_WORK_DIR:-/var/lib/telegram-bot-api}"
12
+ TEMP_DIR="${TELEGRAM_TEMP_DIR:-/tmp/telegram-bot-api}"
13
  mkdir -p "$WORK_DIR" "$TEMP_DIR"
14
 
15
  VERBOSITY="${TELEGRAM_VERBOSITY:-4}"
16
+
 
17
  telegram-bot-api \
18
  --api-id="${TELEGRAM_API_ID}" \
19
  --api-hash="${TELEGRAM_API_HASH}" \
20
  --dir="$WORK_DIR" \
21
  --temp-dir="$TEMP_DIR" \
22
  --http-ip-address=127.0.0.1 \
23
+ --http-port=8081 \
24
  --local \
25
  --verbosity="$VERBOSITY" \
 
26
  --username=telegram-bot-api \
27
  --groupname=telegram-bot-api &
28