Fix bot: add debug logging, improve API response handling
Browse files
bot.js
CHANGED
|
@@ -14,6 +14,9 @@ if (!ZEN_API_KEY) {
|
|
| 14 |
process.exit(1);
|
| 15 |
}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
const ZEN_BASE = "https://opencode.ai/zen/v1";
|
| 18 |
const bot = new Bot(BOT_TOKEN);
|
| 19 |
|
|
@@ -45,6 +48,8 @@ async function callZenAPI(messages, modelId) {
|
|
| 45 |
body = { model: modelId, messages, max_tokens: 4096 };
|
| 46 |
}
|
| 47 |
|
|
|
|
|
|
|
| 48 |
const response = await fetch(endpoint, {
|
| 49 |
method: "POST",
|
| 50 |
headers,
|
|
@@ -53,19 +58,26 @@ async function callZenAPI(messages, modelId) {
|
|
| 53 |
|
| 54 |
if (!response.ok) {
|
| 55 |
const errText = await response.text();
|
|
|
|
| 56 |
throw new Error(`API ${response.status}: ${errText.substring(0, 300)}`);
|
| 57 |
}
|
| 58 |
|
| 59 |
const data = await response.json();
|
|
|
|
| 60 |
|
|
|
|
| 61 |
if (data.content) {
|
| 62 |
-
|
|
|
|
| 63 |
}
|
|
|
|
| 64 |
if (data.choices && data.choices[0]) {
|
| 65 |
const msg = data.choices[0].message;
|
| 66 |
if (msg.content && msg.content.trim()) return msg.content;
|
| 67 |
-
if (msg.reasoning_content && msg.reasoning_content.trim())
|
| 68 |
-
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
return JSON.stringify(data).substring(0, 2000);
|
| 71 |
}
|
|
@@ -73,6 +85,7 @@ async function callZenAPI(messages, modelId) {
|
|
| 73 |
// /start
|
| 74 |
bot.command("start", async (ctx) => {
|
| 75 |
const chatId = ctx.chat.id;
|
|
|
|
| 76 |
if (CHAT_ID && chatId.toString() !== CHAT_ID.toString()) return;
|
| 77 |
|
| 78 |
userSessions.set(chatId, { model: "deepseek-v4-flash-free", history: [] });
|
|
@@ -164,8 +177,11 @@ bot.on("message:text", async (ctx) => {
|
|
| 164 |
const chatId = ctx.chat.id;
|
| 165 |
const message = ctx.message.text;
|
| 166 |
|
|
|
|
|
|
|
| 167 |
// Check allowed chat
|
| 168 |
if (CHAT_ID && chatId.toString() !== CHAT_ID.toString()) {
|
|
|
|
| 169 |
await ctx.reply("Sorry, I'm only available in the designated group.");
|
| 170 |
return;
|
| 171 |
}
|
|
@@ -203,7 +219,7 @@ bot.on("message:text", async (ctx) => {
|
|
| 203 |
await ctx.reply(response);
|
| 204 |
}
|
| 205 |
} catch (error) {
|
| 206 |
-
console.error("[ERROR]", error.message);
|
| 207 |
await ctx.reply(`❌ Error: ${error.message}`);
|
| 208 |
}
|
| 209 |
});
|
|
|
|
| 14 |
process.exit(1);
|
| 15 |
}
|
| 16 |
|
| 17 |
+
console.log("[CONFIG] CHAT_ID filter:", CHAT_ID || "DISABLED (accepting all chats)");
|
| 18 |
+
console.log("[CONFIG] Zen API Key:", ZEN_API_KEY ? ZEN_API_KEY.substring(0, 12) + "..." : "NOT SET");
|
| 19 |
+
|
| 20 |
const ZEN_BASE = "https://opencode.ai/zen/v1";
|
| 21 |
const bot = new Bot(BOT_TOKEN);
|
| 22 |
|
|
|
|
| 48 |
body = { model: modelId, messages, max_tokens: 4096 };
|
| 49 |
}
|
| 50 |
|
| 51 |
+
console.log("[API] Calling:", endpoint, "model:", modelId);
|
| 52 |
+
|
| 53 |
const response = await fetch(endpoint, {
|
| 54 |
method: "POST",
|
| 55 |
headers,
|
|
|
|
| 58 |
|
| 59 |
if (!response.ok) {
|
| 60 |
const errText = await response.text();
|
| 61 |
+
console.error("[API ERROR]", response.status, errText.substring(0, 500));
|
| 62 |
throw new Error(`API ${response.status}: ${errText.substring(0, 300)}`);
|
| 63 |
}
|
| 64 |
|
| 65 |
const data = await response.json();
|
| 66 |
+
console.log("[API] Response keys:", Object.keys(data));
|
| 67 |
|
| 68 |
+
// Handle /messages format (Anthropic-style)
|
| 69 |
if (data.content) {
|
| 70 |
+
const result = data.content.map((c) => c.text || "").join("");
|
| 71 |
+
if (result.trim()) return result;
|
| 72 |
}
|
| 73 |
+
// Handle /chat/completions format (OpenAI-style)
|
| 74 |
if (data.choices && data.choices[0]) {
|
| 75 |
const msg = data.choices[0].message;
|
| 76 |
if (msg.content && msg.content.trim()) return msg.content;
|
| 77 |
+
if (msg.reasoning_content && msg.reasoning_content.trim()) {
|
| 78 |
+
return msg.reasoning_content;
|
| 79 |
+
}
|
| 80 |
+
return "(empty response from model - try another model)";
|
| 81 |
}
|
| 82 |
return JSON.stringify(data).substring(0, 2000);
|
| 83 |
}
|
|
|
|
| 85 |
// /start
|
| 86 |
bot.command("start", async (ctx) => {
|
| 87 |
const chatId = ctx.chat.id;
|
| 88 |
+
console.log("[LOG] /start from chat:", chatId, "type:", ctx.chat.type);
|
| 89 |
if (CHAT_ID && chatId.toString() !== CHAT_ID.toString()) return;
|
| 90 |
|
| 91 |
userSessions.set(chatId, { model: "deepseek-v4-flash-free", history: [] });
|
|
|
|
| 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 |
+
|
| 182 |
// Check allowed chat
|
| 183 |
if (CHAT_ID && chatId.toString() !== CHAT_ID.toString()) {
|
| 184 |
+
console.log("[BLOCKED] Chat", chatId, "not in allowed list. CHAT_ID:", CHAT_ID);
|
| 185 |
await ctx.reply("Sorry, I'm only available in the designated group.");
|
| 186 |
return;
|
| 187 |
}
|
|
|
|
| 219 |
await ctx.reply(response);
|
| 220 |
}
|
| 221 |
} catch (error) {
|
| 222 |
+
console.error("[ERROR] Chat API failed:", error.message);
|
| 223 |
await ctx.reply(`❌ Error: ${error.message}`);
|
| 224 |
}
|
| 225 |
});
|