Skip to content

Commit

Permalink
chore: local v2 ts 基础适配
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Aug 25, 2024
1 parent 3c59f73 commit d1483dc
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 19 deletions.
2 changes: 1 addition & 1 deletion adapter/local/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createCache, startServer } from 'cloudflare-worker-adapter';
// eslint-disable-next-line ts/ban-ts-comment
// @ts-expect-error
import { installFetchProxy } from 'cloudflare-worker-adapter/fetchProxy';
import worker from '../../main';
import worker from '../../src';
import { ENV } from '../../src/config/env.js';

const config = JSON.parse(fs.readFileSync('./config.json', 'utf-8'));
Expand Down
2 changes: 1 addition & 1 deletion adapter/local_v2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
```
1. 使用 `npm run config` 将toml配置文件转换为json配置文件
2. 或者修改 `config-example.json``config.json` 并修改其中的配置
3. 如果你的网络环境需要代理,请在 `index.js` 中修改相关注释代码
3. 如果你的网络环境需要代理,请在 `index.ts` 中修改相关注释代码

### 本地运行
1. 使用 `npm run start` 启动服务
Expand Down
29 changes: 15 additions & 14 deletions adapter/local_v2/index.js → adapter/local_v2/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import fs from 'node:fs';
import * as process from 'node:process';
import { LocalCache } from 'cloudflare-worker-adapter/localCache';
import { ENV, initEnv } from '../../src/config/env.js';
import { deleteTelegramWebHook, getBotName, getTelegramUpdates } from '../../src/telegram/telegram.js';
import { LocalCache } from 'cloudflare-worker-adapter';
import { ENV } from '../../src/config/env.js';
import i18n from '../../src/i18n/index.js';
import { handleMessage } from '../../src/telegram/message.js';
import { handleUpdate } from '../../src/telegram/handler';

// 如果你的环境需要代理才能访问Telegram API,请取消注释下面的代码,并根据实际情况修改代理地址
// eslint-disable-next-line import/order
import { installFetchProxy } from 'cloudflare-worker-adapter/fetchProxy';
import { createTelegramBotAPI } from '../../src/telegram/api';
import type { TelegramBotAPI } from '../../src/telegram/api';

const proxy = process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY || process.env.http_proxy;
installFetchProxy(proxy);
Expand All @@ -24,33 +25,33 @@ if (!fs.existsSync(CACHE_PATH)) {

// Initialize environment
const cache = new LocalCache(CACHE_PATH);
initEnv({
ENV.merge({
...(JSON.parse(fs.readFileSync(CONFIG_PATH, 'utf-8')).vars),
DATABASE: cache,
}, i18n);

// Delete all webhooks
const offset = {};
const clients: Record<string, TelegramBotAPI> = {};
const offset: Record<string, number> = {};
for (const token of ENV.TELEGRAM_AVAILABLE_TOKENS) {
offset[token] = 0;
const name = await getBotName(token);
await deleteTelegramWebHook(token);
console.log(`@${name} Webhook deleted, If you want to use webhook, please set it up again.`);
const api = createTelegramBotAPI(token);
clients[token] = api;
const name = await api.getMeWithReturns();
await api.deleteWebhook({});
console.log(`@${name.result.username} Webhook deleted, If you want to use webhook, please set it up again.`);
}

while (true) {
for (const token of ENV.TELEGRAM_AVAILABLE_TOKENS) {
try {
/**
* @type {TelegramWebhookRequest[]}
*/
const { result } = await getTelegramUpdates(token, offset[token]);
const { result } = await clients[token].getUpdatesWithReturns({ offset: offset[token] });
for (const update of result) {
if (update.update_id >= offset[token]) {
offset[token] = update.update_id + 1;
}
setImmediate(async () => {
await handleMessage(token, update).catch(console.error);
await handleUpdate(token, update).catch(console.error);
});
}
} catch (e) {
Expand Down
7 changes: 4 additions & 3 deletions adapter/local_v2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "module",
"version": "1.0.0",
"license": "MIT",
"main": "index.js",
"main": "index.ts",
"scripts": {
"start": "node index.js",
"start": "tsx index.ts",
"build": "node esbuild.config.js",
"config": "node config.js",
"docker": "npm run build && docker build -t chatgpt-telegram-bot:latest ."
Expand All @@ -17,6 +17,7 @@
},
"devDependencies": {
"esbuild": "^0.23.0",
"toml": "^3.0.0"
"toml": "^3.0.0",
"tsx": "^4.18.0"
}
}
Loading

0 comments on commit d1483dc

Please sign in to comment.