站点图标 嘟嘟社区

[疑问] tg bot api反代要怎么加路径


目前用的是这个代码
访问为
https://xxxxx.com/bot131232115:AAF33xWSkdTQ231231312312XyoJ6b5kCGMvEv0/sendMessage?chat_id=2342432348&text=22282H

我想改为加路径模式
https://xxxxx.com/tg/bot131232115:AAF33xWSkdTQ231231312312XyoJ6b5kCGMvEv0/sendMessage?chat_id=2342432348&text=22282H

路由里面改了也不行
https://xxxxx.com/tg/*

请问代码需要怎么修改?

  1. const whitelist = ["/bot131232115:"];
  2. const tg_host = "api.telegram.org";
  3. addEventListener(‘fetch’, event => {
  4.     event.respondWith(handleRequest(event.request))
  5. })
  6. function validate(path) {
  7.     for (var i = 0; i < whitelist.length; i++) {
  8.         if (path.startsWith(whitelist[i]))
  9.             return true;
  10.     }
  11.     return false;
  12. }
  13. async function handleRequest(request) {
  14.     var u = new URL(request.url);
  15.     u.host = tg_host;
  16.     if (!validate(u.pathname))
  17.         return new Response(‘Unauthorized’, {
  18.             status: 403
  19.         });
  20.     var req = new Request(u, {
  21.         method: request.method,
  22.         headers: request.headers,
  23.         body: request.body
  24.     });
  25.     const result = await fetch(req);
  26.     return result;
  27. }

复制代码

有人帮忙看看么
不会帮顶

退出移动版