File size: 2,069 Bytes
a2fb4d4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | const { Bot } = require("grammy");
const BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;
const ZEN_API_KEY = process.env.OPENCODE_ZEN_KEY;
console.log("[TEST] Starting debug bot...");
console.log("[TEST] Token:", BOT_TOKEN ? BOT_TOKEN.substring(0, 10) + "..." : "MISSING");
console.log("[TEST] Key:", ZEN_API_KEY ? ZEN_API_KEY.substring(0, 12) + "..." : "MISSING");
const bot = new Bot(BOT_TOKEN);
bot.on("message", async (ctx) => {
const info = {
chatId: ctx.chat.id,
chatType: ctx.chat.type,
text: ctx.message.text || "(non-text)",
fromId: ctx.from?.id,
fromUser: ctx.from?.username,
};
console.log("[MSG] Received:", JSON.stringify(info));
// Step 1: Echo test
try {
await ctx.reply("Echo: " + (ctx.message.text || "no text"));
console.log("[MSG] Echo reply sent OK");
} catch (e) {
console.error("[MSG] Echo reply FAILED:", e.message);
return;
}
// Step 2: API test
if (ctx.message.text && ctx.message.text.startsWith("/")) return;
try {
console.log("[API] Calling Zen API...");
const resp = await fetch("https://opencode.ai/zen/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + ZEN_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "deepseek-v4-flash-free",
messages: [{ role: "user", content: ctx.message.text }],
max_tokens: 4096,
}),
});
console.log("[API] Status:", resp.status);
const data = await resp.json();
const msg = data.choices?.[0]?.message;
let reply = msg?.content || msg?.reasoning_content || "(empty)";
console.log("[API] Reply length:", reply.length);
await ctx.reply(reply.substring(0, 4000));
console.log("[API] Reply sent OK");
} catch (e) {
console.error("[API] FAILED:", e.message);
await ctx.reply("API Error: " + e.message);
}
});
bot.catch((err) => console.error("[BOT ERROR]", err));
bot.start({
onStart: () => {
console.log("[BOT] Running! Send a message to @jotish66bot NOW...");
},
});
|