Fix: skip old messages, drop pending updates, reduce max_tokens for faster response
Browse files
bot.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
const { Bot } = require("grammy");
|
| 2 |
|
| 3 |
// Environment variables
|
| 4 |
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
|
|
@@ -43,9 +43,9 @@ async function callZenAPI(messages, modelId) {
|
|
| 43 |
|
| 44 |
let body;
|
| 45 |
if (endpoint.includes("/messages")) {
|
| 46 |
-
body = { model: modelId, messages, max_tokens:
|
| 47 |
} else {
|
| 48 |
-
body = { model: modelId, messages, max_tokens:
|
| 49 |
}
|
| 50 |
|
| 51 |
console.log("[API] Calling:", endpoint, "model:", modelId);
|
|
@@ -176,6 +176,14 @@ bot.command("new", async (ctx) => {
|
|
| 176 |
bot.on("message:text", async (ctx) => {
|
| 177 |
const chatId = ctx.chat.id;
|
| 178 |
const message = ctx.message.text;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
|
| 180 |
console.log("[LOG] Message from chat:", chatId, "type:", ctx.chat.type, "text:", message.substring(0, 50));
|
| 181 |
|
|
@@ -214,9 +222,11 @@ bot.on("message:text", async (ctx) => {
|
|
| 214 |
const chunks = response.match(/.{1,4000}/gs) || [response];
|
| 215 |
for (const chunk of chunks) {
|
| 216 |
await ctx.reply(chunk);
|
|
|
|
| 217 |
}
|
| 218 |
} else {
|
| 219 |
await ctx.reply(response);
|
|
|
|
| 220 |
}
|
| 221 |
} catch (error) {
|
| 222 |
console.error("[ERROR] Chat API failed:", error.message);
|
|
@@ -235,5 +245,7 @@ bot.start({
|
|
| 235 |
onStart: () => {
|
| 236 |
console.log("[OK] Telegram bot is running!");
|
| 237 |
console.log("[OK] Chat ID filter:", CHAT_ID || "none (accepting all)");
|
|
|
|
| 238 |
},
|
|
|
|
| 239 |
});
|
|
|
|
| 1 |
+
const { Bot, GrammyError, HttpError } = require("grammy");
|
| 2 |
|
| 3 |
// Environment variables
|
| 4 |
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
|
|
|
|
| 43 |
|
| 44 |
let body;
|
| 45 |
if (endpoint.includes("/messages")) {
|
| 46 |
+
body = { model: modelId, messages, max_tokens: 2048 };
|
| 47 |
} else {
|
| 48 |
+
body = { model: modelId, messages, max_tokens: 2048 };
|
| 49 |
}
|
| 50 |
|
| 51 |
console.log("[API] Calling:", endpoint, "model:", modelId);
|
|
|
|
| 176 |
bot.on("message:text", async (ctx) => {
|
| 177 |
const chatId = ctx.chat.id;
|
| 178 |
const message = ctx.message.text;
|
| 179 |
+
const msgTime = ctx.message.date;
|
| 180 |
+
const now = Math.floor(Date.now() / 1000);
|
| 181 |
+
|
| 182 |
+
// Skip old messages (sent more than 30 seconds ago)
|
| 183 |
+
if (now - msgTime > 30) {
|
| 184 |
+
console.log("[SKIP] Old message (", now - msgTime, "s ago):", message.substring(0, 30));
|
| 185 |
+
return;
|
| 186 |
+
}
|
| 187 |
|
| 188 |
console.log("[LOG] Message from chat:", chatId, "type:", ctx.chat.type, "text:", message.substring(0, 50));
|
| 189 |
|
|
|
|
| 222 |
const chunks = response.match(/.{1,4000}/gs) || [response];
|
| 223 |
for (const chunk of chunks) {
|
| 224 |
await ctx.reply(chunk);
|
| 225 |
+
console.log("[REPLY] Sent chunk:", chunk.length, "chars");
|
| 226 |
}
|
| 227 |
} else {
|
| 228 |
await ctx.reply(response);
|
| 229 |
+
console.log("[REPLY] Sent:", response.length, "chars");
|
| 230 |
}
|
| 231 |
} catch (error) {
|
| 232 |
console.error("[ERROR] Chat API failed:", error.message);
|
|
|
|
| 245 |
onStart: () => {
|
| 246 |
console.log("[OK] Telegram bot is running!");
|
| 247 |
console.log("[OK] Chat ID filter:", CHAT_ID || "none (accepting all)");
|
| 248 |
+
console.log("[OK] Waiting for NEW messages only...");
|
| 249 |
},
|
| 250 |
+
drop_pending_updates: true,
|
| 251 |
});
|