Telegram Bot Api 反向代理搭建

由于一些原因，配置 epusdt 需要使用 tg 反向代理地址才能使用！

Copy
#telegram代理url(大陆地区服务器可使用一台国外服务器做反代tg的url)，如果运行的本来就是境外服务器，则无需填写
tg_proxy=

两种实现方案，根据个人喜好选择使用或发挥

Nginx 反代 Telegram Api

安装 nginx

Copy
sudo apt update && sudo apt install -y nginx

创建配置文件

Copy
nano tgapi.conf

输入一下内容并保存

Copy
server {
listen 80;
server_name tgapi.domain;
location / {
return 444;
}
location ~* ^/bot {
resolver 8.8.8.8;
proxy_buffering off;
proxy_pass https://api.telegram.org$request_uri;
}
}

加载配置

Copy
sudo systemctl reload nginx
#或
sudo nginx -s reload

测试访问

输入以下命令行，BOT_TOKEN 换成自己机器人 token。

Copy
curl https://tgapi.domain/bot<BOT_TOKEN>/getMe

看的机器人信息，就说明可以使用了。

配置 epusdt telegram 代理 url

epusdt 配置 (.env) 参考

Copy
#telegram代理url(大陆地区服务器可使用一台国外服务器做反代tg的url)，如果运行的本来就是境外服务器，则无需填写
tg_proxy=https://tgapi.domain

docker 配置 nginx 参考 docker-compose.yam 内容

Copy
version: "3"
services:
nginx:
container_name: "nginx"
restart: always
ports:
- "80:80"
image: nginx:bookworm
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./log:/var/log/nginx
extra_hosts:
- "host.docker.internal:host-gateway"

使用 Cloudflare Worker 代理 Telegram Bot Api

使用前提

一个托管在 cloudflare 的域名

k 开启 cloudflare 的免费 worker 服务

首先登录 Cloudflare 以后点击左侧的 Workers 和 Pages

点击 创建应用程序-创建 worker

名称 随意填写，点击 部署

创建完成后，点击刚创建的 Worker，再点击 快速编辑

在左侧删除原有的代码，填入下面给出的代码

Copy
/**
* Helper functions to check if the request uses
* corresponding method.
*
*/
const Method = (method) => (req) => req.method.toLowerCase() === method.toLowerCase();
const Get = Method('get');
const Post = Method('post');

const Path = (regExp) => (req) => {
const url = new URL(req.url);
const path = url.pathname;
return path.match(regExp) && path.match(regExp)[0] === path;
};

/*
* The regex to get the bot_token and api_method from request URL
* as the first and second backreference respectively.
*/
const URL_PATH_REGEX = /^\/bot(?<bot_token>[^/]+)\/(?<api_method>[a-z]+)/i;

/**
* Router handles the logic of what handler is matched given conditions
* for each request
*/
class Router {
constructor() {
this.routes = [];
}

handle(conditions, handler) {
this.routes.push({
conditions,
handler,
});
return this;
}

get(url, handler) {
return this.handle([Get, Path(url)], handler);
}

post(url, handler) {
return this.handle([Post, Path(url)], handler);
}

all(handler) {
return this.handler([], handler);
}

route(req) {
const route = this.resolve(req);

if (route) {
return route.handler(req);
}

const description = 'No matching route found';
const error_code = 404;

return new Response(
JSON.stringify({
ok: false,
error_code,
description,
}),
{
status: error_code,
statusText: description,
headers: {
'content-type': 'application/json',
},
}
);
}

/**
* It returns the matching route that returns true
* for all the conditions if any.
*/
resolve(req) {
return this.routes.find((r) => {
if (!r.conditions || (Array.isArray(r) && !r.conditions.length)) {
return true;
}

if (typeof r.conditions === 'function') {
return r.conditions(req);
}

return r.conditions.every((c) => c(req));
});
}
}

/**
* Sends a POST request with JSON data to Telegram Bot API
* and reads in the response body.
* @param {Request} request the incoming request
*/
async function handler(request) {
// Extract the URl method from the request.
const { url, ..._request } = request;

const { pathname: path, search } = new URL(url);

// Leave the first match as we are interested only in backreferences.
const { bot_token, api_method } = path.match(URL_PATH_REGEX).groups;

// Build the URL
const api_url = 'https://api.telegram.org/bot' + bot_token + '/' + api_method + search;

// Get the response from API.
const response = await fetch(api_url, _request);

const result = await response.text();

const res = new Response(result, _request);

res.headers.set('Content-Type', 'application/json');

return res;
}

/**
* Handles the incoming request.
* @param {Request} request the incoming request.
*/
async function handleRequest(request) {
const r = new Router();
r.get(URL_PATH_REGEX, (req) => handler(req));
r.post(URL_PATH_REGEX, (req) => handler(req));

const resp = await r.route(request);
return resp;
}

/**
* Hook into the fetch event.
*/
addEventListener('fetch', (event) => {
event.respondWith(handleRequest(event.request));
});

保存并部署

回到管理后台首页，点击左侧的网站，在右侧点击你已托管在Cloudflare的域名

选择该域名下的 Workers 路由

选择添加路由

路由 填写你想要用的二级域名，比如：tgapi.domain/* 注意后面必须是/*结尾，Worker选择刚才创建的服务，保存就可以了。