Spaces:
Running
Running
Upload 4 files
Browse files
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
headers.
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
"content-
|
| 41 |
-
"content-
|
| 42 |
-
"
|
| 43 |
-
"
|
| 44 |
-
"
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 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 |
-
#
|
| 11 |
-
WORK_DIR="${TELEGRAM_WORK_DIR:-/
|
| 12 |
-
TEMP_DIR="${TELEGRAM_TEMP_DIR:-/
|
| 13 |
mkdir -p "$WORK_DIR" "$TEMP_DIR"
|
| 14 |
|
| 15 |
VERBOSITY="${TELEGRAM_VERBOSITY:-4}"
|
| 16 |
-
|
| 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=
|
| 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 |
|