使用Cloudflare Worker代理Telegram Bot Api(2023.05) | 您所在的位置:网站首页 › 中国几大碑林区是哪些 › 使用Cloudflare Worker代理Telegram Bot Api(2023.05) |
参考链接:使用Cloudflare Worker代理Telegram Bot Api – 废墟 (anerg.com) (68条消息) cf反代tg代理并使用自己的域名_拖拉斯基~的博客-CSDN博客 Cloud Flare 添加谷歌镜像站(反向代理)_cloudflare 反向代理_StarDream-Online的博客-CSDN博客 在白嫖到了免费的二级域名(白嫖二级域名 2023.05亲测可用 - 简书 (jianshu.com)) 之后,用它来代理tg的bot api 添加站点 image.png首先需要添加自己的域名到Cloudflare,没有域名是无法继续下一步的 image.png image.png image.png这里的服务器名称应该是有个地方会提供给你的,就是以 cloudflare.com 结尾的两个地址 NS anderson.ns.cloudflare.com NS nancy.ns.cloudflare.com这个是我目前网站的服务器名称 此外还需要添加一个 DNS 记录 image.png名称随便填,这里填 tg,IPv4地址随便填 填了tg之后 ,tg.你的域名就是你实际使用到的代理地址了 image.png 添加Workers在Cloudflare中新建一个workers image.png选择创建Workers image.png随便填个名字,比如tg,然后直接"Deploy" image.png这个部分和参考链接里面的界面好像不太一样,没有什么“http路由器”这些,但是不影响后面的过程 保存完之后,返回Workers的创建页面,点击标题进行编辑 image.png直接选择“快速编辑”,进入到代码编辑页面 image.png将以前的代码全部删除,然后粘贴下面的代码 /** * 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(?[^/]+)\/(?[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路由”进行操作 image.png路由填tg.你的域名/*,服务选择你刚刚创建的workers,然后保存 image.png 测试Tg bot的api是 ### 这个地址需要外网访问 https://api.telegram.org/bot/sendMessage?chat_id=&text=将上面的参数替换成你自己的参数,如果你的账号可以收到新的消息表示你的机器人bot创建是正常的 上面的地址如果可以正常收到消息,再访问下面这个地址 ### 这个地址在国内就可以访问 https://tg.你的域名/bot/sendMessage?chat_id=&text=以上就完成了Cloudflare反向代理Tg bot,通过Cloudflare还可以反向代理谷歌等网址,参考链接里面有例子,具体需要自行了解 |
CopyRight 2018-2019 实验室设备网 版权所有 |