简介

Telegram国内不可用的问题.利用cloudflare-workers解决来转发tg机器人的API

Github

const whitelist = [""];
const tg_host = "api.telegram.org";
 
addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request))
})
 
function validate(path) {
    for (var i = 0; i < whitelist.length; i++) {
        if (path.startsWith(whitelist[i]))
            return true;
    }
    return false;
}
 
async function handleRequest(request) {
    var u = new URL(request.url);
    u.host = tg_host;
    if (!validate(u.pathname))
        return new Response('Unauthorized', {
            status: 403
        });
    var req = new Request(u, {
        method: request.method,
        headers: request.headers,
        body: request.body
    });
    const result = await fetch(req);
    return result;
}

linux测试是否成功

## 下方填写自己申请@BotFather的Token,如10xxx4:AAFcqxxxxgER5uw
export TG_BOT_TOKEN=""
## 下方填写 @getuseridbot 中获取到的纯数字ID
export TG_USER_ID=""
## 你获取的域名例如telegram.myki.workers.dev
export TG_API_HOST=""
curl --data chat_id="${TG_USER_ID}" --data "text=Hello, World." "https://${TG_API_HOST}${TG_BOT_TOKEN}/sendMessage"

cloudflare-workers仓库里面油google或者dockerhub等等的反代都可以参考下