baicai

白菜

一个勤奋的代码搬运工!

Telegram Bot Api 反向代理搭建

由於一些原因,配置 epusdt 需要使用 tg 反向代理地址才能使用!

#telegram代理url(大陸地區伺服器可使用一台國外伺服器做反代tg的url),如果運行的本來就是境外伺服器,則無需填寫
tg_proxy=

兩種實現方案,根據個人喜好選擇使用或發揮

Nginx 反代 Telegram Api#

安裝 nginx#

sudo apt update && sudo apt install -y nginx

創建配置文件#

nano tgapi.conf

輸入以下內容並保存

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;
    }
}

加載配置#

sudo systemctl reload nginx
#或
sudo nginx -s reload

測試訪問#

輸入以下命令行,BOT_TOKEN 換成自己機器人 token。

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

查看機器人信息,就說明可以使用了。

配置 epusdt telegram 代理 url#

epusdt 配置 (.env) 參考

#telegram代理url(大陸地區伺服器可使用一台國外伺服器做反代tg的url),如果運行的本來就是境外伺服器,則無需填寫
tg_proxy=https://tgapi.domain

docker 配置 nginx 參考 docker-compose.yam 內容#

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#

使用前提

  1. 一個托管在 cloudflare 的域名
  2. 開啟 cloudflare 的免費 worker 服務

首先登錄 Cloudflare 以後點擊左側的 Workers 和 Pages#

點擊 創建應用程序-創建 worker

名稱 隨意填寫,點擊 部署

創建完成後,點擊剛創建的 Worker,再點擊 快速編輯

在左側刪除原有的代碼,填入下面給出的代碼

/**
 * 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 = '未找到匹配的路由';
		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選擇剛才創建的服務,保存就可以了。

載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。