环境准备

git,nodejs环境

vercel账户

教程

反代telegram api

安装vercel cli

首先你需要安装一下vercel的命令行工具,使用npm进行安装

1
npm install -g vercel

填写vercel.json

把下面这些内容复制到vercel.json里面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"version": 2,
"routes": [
{"src": "/pluto/(.*)","dest": "https://pluto.web.telegram.org/$1"},
{"src": "/venus/(.*)","dest": "https://venus.web.telegram.org/$1"},
{"src": "/aurora/(.*)","dest": "https://aurora.web.telegram.org/$1"},
{"src": "/vesta/(.*)","dest": "https://vesta.web.telegram.org/$1"},
{"src": "/flora/(.*)","dest": "https://flora.web.telegram.org/$1"},
{"src": "/pluto-1/(.*)","dest": "https://pluto-1.web.telegram.org/$1"},
{"src": "/venus-1/(.*)","dest": "https://venus-1.web.telegram.org/$1"},
{"src": "/aurora-1/(.*)","dest": "https://aurora-1.web.telegram.org/$1"},
{"src": "/vesta-1/(.*)","dest": "https://vesta-1.web.telegram.org/$1"},
{"src": "/flora-1/(.*)","dest": "https://flora-1.web.telegram.org/$1"}
]
}

更多关于json文件的适用方法,可以参照https://vercel.com/docs/project-configuration

登录vercel

写完json文件以后,就开始对vercel的cli进行登录

1
vercel login

成功登录以后会像我下面这样输出

1
2
3
> Success! GitHub authentication complete for lovelysam01101@outlook.com
Congratulations! You are now logged in. In order to deploy something, run `vercel`.
💡 Connect your Git Repositories to deploy every branch push automatically (https://vercel.link/git).

部署vercel.json

接着我们对刚刚的json文件进行部署

1
vercel --prod

–prod为生产环境,按照提示输入就行了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Vercel CLI 31.2.2
? Set up and deploy “D:\code\vercel”? [Y/n] y
? Which scope do you want to deploy to? xxxxxx
? Link to existing project? [y/N] n
? What’s your project’s name? vercel-json
? In which directory is your code located? ./
Local settings detected in vercel.json:
No framework detected. Default Project Settings:
- Build Command: `npm run vercel-build` or `npm run build`
- Development Command: None
- Install Command: `yarn install`, `pnpm install`, or `npm install`
- Output Directory: `public` if it exists, or `.`
? Want to modify these settings? [y/N] n
🔗 Linked to lovelyo0sam-protonme/vercel-json (created .vercel and added it to .gitignore)
🔍 Inspect: https://vercel.com/xxxxxx/vercel-json/5csUre26mhkNBdvYmFKgLwfn7YvZ [2s]
✅ Production: https://xxxxxx.vercel.app [6s]

因为vercel的域名在中国被墙的原因 打开 https://vercel.com/username/project/settings/domains

添加自定义域名

部署telegram web k

记住上述部署的域名

克隆github仓库

1
git clone https://github.com/morethanwords/tweb.git

安装依赖

打开克隆后的文件夹 shift+右键 在windows终端中打开

1
2
3
4
npm install -g npm@9.8.1 vite
npm i -f
npm i --save -D html-webpack-plugin webpack webpack-cli webpack-dev-server ts-loader cross-env big-integer @cryptography/aes
npx update-browserslist-db@latest

构建并修改文件

1
npm run build:vite

应该会有类似下面的报错 不用管

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
✓ 735 modules transformed.
rendering chunks (20)...The emitted file "langSign-f5a5610c.js" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js" overwrites a previously emitted file of the same name.
The emitted file "lang-c1c2a466.js" overwrites a previously emitted file of the same name.
The emitted file "langSign-f5a5610c.js.map" overwrites a previously emitted file of the same name.
The emitted file "pluralPolyfill-61f068d6.js.map" overwrites a previously emitted file of the same name.
The emitted file "countries-5301fc59.js.map" overwrites a previously emitted file of the same name.
The emitted file "lang-c1c2a466.js.map" overwrites a previously emitted file of the same name.
[vite:build-import-analysis] Cannot read properties of undefined (reading 'forEach')
✓ built in 10.09s
error during build:
TypeError: Cannot read properties of undefined (reading 'forEach')
at addDeps (file:///code/tweb/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45215:55)
at Object.generateBundle (file:///code/tweb/node_modules/vite/dist/node/chunks/dep-24daf00c.js:45236:33)
at file:///code/tweb/node_modules/rollup/dist/es/shared/node-entry.js:24412:40

如果出现类似下列的报错 执行 vite build

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
node:internal/fs/utils:350
throw err;
^

.md'r: ENOENT: no such file or directory, open './public/changelogs/en_0.8.6
at Object.openSync (node:fs:603:3)
at Object.writeFileSync (node:fs:2324:35)
at D:\code\tweb\src\scripts\generate_changelog.js:41:8
at Array.forEach (<anonymous>)
at processChangelog (D:\code\tweb\src\scripts\generate_changelog.js:24:12)
at D:\code\tweb\src\scripts\generate_changelog.js:47:5
at Array.forEach (<anonymous>)
at Object.<anonymous> (D:\code\tweb\src\scripts\generate_changelog.js:45:11)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: './public/changelogs/en_0.8.6\r.md'
}

接下来打开 public 文件夹(xxxxxx为随机数)

打开 mtproto.worker-xxxxxx.js

按下 ctrl+f 查找 https://"+o+".web.telegram.org/ 修改为 https://你的自定义域名/"+o+"/ 并保存

接下来打开 mtproto.worker-xxxxxx.js.map

按下 ctrl+f 查找 .web.telegram.org/ 找到第二个 修改为 'https://' + '你的自定义域名/' + subdomain + '/' + path 并保存

接下来将 public 文件夹中的内容上传至虚拟主机就可以用了

(对于上传至cloudflare pages npm i -g wrangler wrangler pages deploy ./public