參考鏈接:使用Cloudflare Worker代理Telegram Bot Api – 廢墟 (anerg.com)
(68條消息) cf反代tg代理并使用自己的域名_拖拉斯基~的博客-CSDN博客
Cloud Flare 添加谷歌鏡像站(反向代理)_cloudflare 反向代理_StarDream-Online的博客-CSDN博客
在白嫖到了免費的二級域名(白嫖二級域名 2023.05親測可用 - 簡書 (jianshu.com))
之后,用它來代理tg的bot api
添加站點
首先需要添加自己的域名到Cloudflare,沒有域名是無法繼續下一步的
這里的服務器名稱應該是有個地方會提供給你的,就是以 cloudflare.com
結尾的兩個地址
NS anderson.ns.cloudflare.com
NS nancy.ns.cloudflare.com
這個是我目前網站的服務器名稱
此外還需要添加一個 DNS 記錄
名稱隨便填,這里填 tg,IPv4地址隨便填
填了tg之后 ,tg.你的域名
就是你實際使用到的代理地址了
添加Workers
在Cloudflare中新建一個workers
選擇創建Workers
隨便填個名字,比如tg
,然后直接"Deploy"
這個部分和參考鏈接里面的界面好像不太一樣,沒有什么“http路由器”這些,但是不影響后面的過程
保存完之后,返回Workers的創建頁面,點擊標題進行編輯
直接選擇“快速編輯”,進入到代碼編輯頁面
將以前的代碼全部刪除,然后粘貼下面的代碼
/**
* 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));
});
記得保存;
添加Workers路由
點擊網站,再點擊“workers路由”進行操作
路由填tg.你的域名/*
,服務選擇你剛剛創建的workers,然后保存
測試
Tg bot的api是
### 這個地址需要外網訪問
https://api.telegram.org/bot<bot的token>/sendMessage?chat_id=<tg用戶的id>&text=<發送內容>
將上面的參數替換成你自己的參數,如果你的賬號可以收到新的消息表示你的機器人bot創建是正常的
上面的地址如果可以正常收到消息,再訪問下面這個地址
### 這個地址在國內就可以訪問
https://tg.你的域名/bot<bot的token>/sendMessage?chat_id=<tg用戶的id>&text=<發送內容>
以上就完成了Cloudflare反向代理Tg bot,通過Cloudflare還可以反向代理谷歌等網址,參考鏈接里面有例子,具體需要自行了解