| 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)); |
|
|
| |
| 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; |
| } |
|
|
| |
| 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..."); |
| }, |
| }); |
|
|