Skip to content

Commit

Permalink
fix: fix the issue of bot name value retrieval in scheduled tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
adolphnov committed Aug 14, 2024
1 parent aeb47b8 commit 5ff0301
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 21 deletions.
17 changes: 9 additions & 8 deletions adapter/docker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ import toml from 'toml';
import fs from 'fs';
import { default as worker } from 'chatgpt-telegram-workers';


const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
const cache = new RedisCache(redisUrl);

// 定时任务
const raw = fs.readFileSync('./config/config.toml');
const env = { ...toml.parse(raw).vars, DATABASE: cache };
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME > 5) {
setInterval(async () => {
await worker.scheduled(null, env, null);
}, env.SCHEDULE_TIME * 60 * 1000);
}
// 定时任务
const raw = fs.readFileSync('./config/config.toml');
const env = { ...toml.parse(raw).vars, DATABASE: cache };
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME >= 5) {
setInterval(async () => {
await worker.scheduled(null, env, null);
}, env.SCHEDULE_TIME * 60 * 1000);
}

adapter.startServer(
8787,
Expand Down
2 changes: 1 addition & 1 deletion adapter/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ try {
// 定时任务
const raw = fs.readFileSync('../../wrangler.toml');
const env = { ...toml.parse(raw).vars, DATABASE: cache };
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME > 5) {
if (env.SCHEDULE_TIME && env.SCHEDULE_TIME >= 5) {
setInterval(async () => {
await worker.scheduled(null, env, null);
}, env.SCHEDULE_TIME * 60 * 1000);
Expand Down
2 changes: 1 addition & 1 deletion dist/buildinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"sha":"6aac4ab","timestamp":1723620731}
{"sha":"aeb47b8","timestamp":1723629837}
18 changes: 12 additions & 6 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ var Environment = class {
// -- 版本数据 --
//
// 当前版本
BUILD_TIMESTAMP = 1723620731;
BUILD_TIMESTAMP = 1723629837;
// 当前版本 commit id
BUILD_VERSION = "6aac4ab";
BUILD_VERSION = "aeb47b8";
// -- 基础配置 --
/**
* @type {I18n | null}
Expand Down Expand Up @@ -537,7 +537,7 @@ var Context = class {
this.SHARE_CONTEXT.chatId = message.chat.id;
this.SHARE_CONTEXT.speakerId = message.from.id || message.chat.id;
this.SHARE_CONTEXT.messageId = message.message_id;
if (ENV.SCHEDULE_TIME > 5)
if (ENV.SCHEDULE_TIME >= 5)
this.SHARE_CONTEXT.sentMessageIds = /* @__PURE__ */ new Set();
}
/**
Expand Down Expand Up @@ -4721,18 +4721,24 @@ async function schedule_detele_message(ENV2) {
const scheduleDeteleKey = "schedule_detele_message";
const scheduledData = JSON.parse(await DATABASE2.get(scheduleDeteleKey) || "{}");
let botTokens = [];
let botNames = [];
if (typeof ENV2.TELEGRAM_AVAILABLE_TOKENS === "string") {
botTokens = parseArray(ENV2.TELEGRAM_AVAILABLE_TOKENS);
} else
botTokens = ENV2.TELEGRAM_AVAILABLE_TOKENS;
if (typeof ENV2.TELEGRAM_BOT_NAME === "string") {
botNames = parseArray(ENV2.TELEGRAM_BOT_NAME);
} else
botNames = ENV2.TELEGRAM_BOT_NAME;
const taskPromises = [];
for (const [bot_name, chats] of Object.entries(scheduledData)) {
const bot_index = ENV2.TELEGRAM_BOT_NAME.indexOf(bot_name);
const bot_index = botNames.indexOf(bot_name);
if (bot_index < 0)
throw new Error("bot name is invalid");
throw new Error(`bot name: ${bot_name} is not exist.`);
const bot_token = botTokens[bot_index];
if (!bot_token)
throw new Error("bot token is null");
throw new Error(`Cant find bot ${bot_name} - position ${bot_index + 1}'s token
All token list: ${botTokens}`);
for (const [chat_id, messages] of Object.entries(chats)) {
if (messages.length === 0)
continue;
Expand Down
2 changes: 1 addition & 1 deletion dist/timestamp
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1723620731
1723629837
2 changes: 1 addition & 1 deletion src/config/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ export class Context {
this.SHARE_CONTEXT.chatId = message.chat.id;
this.SHARE_CONTEXT.speakerId = message.from.id || message.chat.id;
this.SHARE_CONTEXT.messageId = message.message_id;
if (ENV.SCHEDULE_TIME > 5) this.SHARE_CONTEXT.sentMessageIds = new Set();
if (ENV.SCHEDULE_TIME >= 5) this.SHARE_CONTEXT.sentMessageIds = new Set();

}

Expand Down
11 changes: 8 additions & 3 deletions src/tools/scheduleTask.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,23 @@ async function schedule_detele_message(ENV) {
const scheduleDeteleKey = 'schedule_detele_message';
const scheduledData = JSON.parse((await DATABASE.get(scheduleDeteleKey)) || '{}');
let botTokens = [];
let botNames = [];

if (typeof ENV.TELEGRAM_AVAILABLE_TOKENS === 'string') {
botTokens = parseArray(ENV.TELEGRAM_AVAILABLE_TOKENS);
} else botTokens = ENV.TELEGRAM_AVAILABLE_TOKENS;

if (typeof ENV.TELEGRAM_BOT_NAME === 'string') {
botNames = parseArray(ENV.TELEGRAM_BOT_NAME);
} else botNames = ENV.TELEGRAM_BOT_NAME;

const taskPromises = [];

for (const [bot_name, chats] of Object.entries(scheduledData)) {
const bot_index = ENV.TELEGRAM_BOT_NAME.indexOf(bot_name);
if (bot_index < 0) throw new Error('bot name is invalid');
const bot_index = botNames.indexOf(bot_name);
if (bot_index < 0) throw new Error(`bot name: ${bot_name} is not exist.`);
const bot_token = botTokens[bot_index];
if (!bot_token) throw new Error('bot token is null');
if (!bot_token) throw new Error(`Cant find bot ${bot_name} - position ${bot_index + 1}'s token\nAll token list: ${botTokens}`);
for (const [chat_id, messages] of Object.entries(chats)) {
if (messages.length === 0) continue;

Expand Down

0 comments on commit 5ff0301

Please sign in to comment.