// code from goatstore // api: https://store.agi.bd module.exports.config = { name: "story", version: "4.1.0", author: "Zihad", role: 0, category: "fun", guide: "{pn}" }; const https = require("https"); const API = "https://shangkhachil.com/v1"; const CATEGORIES = [ ["ভূতের গল্প 👻", "ভূত"], ["ভয়ংকর গল্প 😱", "ভয়"], ["রহস্য 🔍", "রহস্য"], ["প্রেমের গল্প ❤️", "প্রেম"], ["কষ্টের গল্প 💔", "দুঃখ"], ["মজার গল্প 😂", "হাসি"], ["অ্যাডভেঞ্চার 🏔️", "পাহাড়"], ["শিক্ষামূলক 📚", "শিক্ষা"], ["গোয়েন্দা 🕵️", "গোয়েন্দা"], ["ইসলামিক শিক্ষামূলক 🕌", "ইসলাম"], ["অনুপ্রেরণামূলক 🔥", "সাহস"] ]; const SEARCH_WORDS = [ "আমি", "সে", "তাহা", "ছিল", "হইল", "কহিল", "তাহার", "একদিন" ]; function request(url) { return new Promise((resolve, reject) => { const req = https.get( url, { headers: { "User-Agent": "GoatBot-Story/4.1" } }, res => { let body = ""; res.setEncoding("utf8"); res.on("data", chunk => { body += chunk; }); res.on("end", () => { if (res.statusCode < 200 || res.statusCode >= 300) { return reject( new Error("HTTP " + res.statusCode) ); } try { resolve(JSON.parse(body)); } catch (e) { reject(new Error("Invalid JSON")); } }); } ); req.setTimeout(15000, () => { req.destroy(new Error("Timeout")); }); req.on("error", reject); }); } async function searchStory(query) { return request( API + "/search?q=" + encodeURIComponent(query) + "&limit=20" ); } async function getWorks() { return request( API + "/works?country=BD" ); } function findWork(works, id) { return works.find( item => String(item.id) === String(id) ); } function cleanText(text) { return String(text || "") .replace(/\r/g, "") .replace(/\n{3,}/g, "\n\n") .trim(); } function splitText(text, size) { const parts = []; let current = ""; const paragraphs = text.split(/\n+/); for (const paragraph of paragraphs) { const p = paragraph.trim(); if (!p) { continue; } if ( current.length + p.length + 2 <= size ) { current += (current ? "\n\n" : "") + p; } else { if (current) { parts.push(current); } current = p; } } if (current) { parts.push(current); } return parts; } function menuText() { return CATEGORIES .map((item, index) => { return ( "┃ " + (index + 1) + ". " + item[0] ); }) .join("\n"); } /* * ========================================== * UPDATE / REFRESH LIST * ========================================== */ async function sendUpdates(api, event) { try { await api.sendMessage( "╭━━〔 🔄 STORY UPDATE 〕━━╮\n" + "┃\n" + "┃ 🌐 অনলাইন গল্পের ডাটাবেস\n" + "┃ 🔎 নতুন করে চেক করা হচ্ছে...\n" + "┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯", event.threadID ); const result = await getWorks(); let works = Array.isArray(result.works) ? result.works : []; if (!works.length) { return api.sendMessage( "❌ বর্তমানে কোনো গল্পের তথ্য পাওয়া যায়নি।", event.threadID ); } /* * নতুন প্রকাশনার বছর আগে দেখানো। * API-তে আলাদা upload/update timestamp নেই। */ works = works .filter(item => item.available !== false) .sort((a, b) => { const ay = Number( a.first_pub_year || 0 ); const by = Number( b.first_pub_year || 0 ); return by - ay; }) .slice(0, 15); const list = works .map((work, index) => { const title = work.title || "বাংলা সাহিত্য"; const author = work.author_bn || work.author || "অজানা লেখক"; const year = work.first_pub_year ? " (" + work.first_pub_year + ")" : ""; return ( "┃ " + (index + 1) + ". " + title + year + "\n" + "┃ ✍️ " + author ); }) .join("\n"); const msg = "╭━━〔 📰 গল্প আপডেট 〕━━╮\n" + "┃\n" + "┃ 🌐 Online Literature\n" + "┃ 🔄 Database refreshed\n" + "┃\n" + list + "\n┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯\n\n" + "📖 গল্প পড়তে `story` লিখুন।"; return api.sendMessage( msg, event.threadID ); } catch (error) { console.error( "STORY UPDATE ERROR:", error ); return api.sendMessage( "╭━━〔 ❌ UPDATE ERROR 〕━━╮\n" + "┃\n" + "┃ অনলাইন গল্পের ডাটাবেস\n" + "┃ থেকে আপডেট আনা যায়নি।\n" + "┃\n" + "┃ 🔄 কিছুক্ষণ পরে আবার চেষ্টা করুন।\n" + "╰━━━━━━━━━━━━━━━━━━━━╯", event.threadID ); } } /* * ========================================== * MAIN * ========================================== */ module.exports.onStart = async function ({ api, event }) { const command = String(event.body || "") .trim() .toLowerCase(); /* * story update * story আপডেট * story refresh */ if ( command === "story update" || command === "story আপডেট" || command === "story refresh" ) { return sendUpdates(api, event); } const msg = "╭━━〔 📖 বাংলা গল্প পত্রিকা 〕━━╮\n" + "┃\n" + menuText() + "\n┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯\n\n" + "🔢 গল্পের ধরন নির্বাচন করুন।\n\n" + "🔄 নতুন অনলাইন তালিকা দেখতে:\n" + "`story update`"; const sent = await api.sendMessage( msg, event.threadID ); global.GoatBot.onReply.set( sent.messageID, { commandName: "story", step: 1 } ); }; /* * ========================================== * REPLY * ========================================== */ module.exports.onReply = async function ({ api, event, Reply }) { const input = String( event.body || "" ).trim(); /* * NEXT PAGE */ if ( Reply.step === 3 && ( input.toLowerCase() === "next" || input === "পরের অংশ" || input === "পরের" || input.toLowerCase() === "n" ) ) { const pages = Reply.pages || []; const page = Number( Reply.page || 0 ); const nextPage = page + 1; if ( nextPage >= pages.length ) { return api.sendMessage( "╭━━〔 📖 গল্প শেষ 〕━━╮\n" + "┃\n" + "┃ এই গল্পের সংগৃহীত\n" + "┃ অংশ শেষ হয়েছে।\n" + "┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯", event.threadID ); } const msg = "╭━━〔 📰 " + Reply.title + " 〕━━╮\n" + "┃ ✍️ " + Reply.author + "\n" + "┃ 🏷️ " + Reply.category + "\n" + "┃ 📖 অংশ " + (nextPage + 1) + "/" + pages.length + "\n" + "╰━━━━━━━━━━━━━━━━━━━━╯\n\n" + pages[nextPage] + "\n\n" + "╭━━〔 📖 পরবর্তী অংশ 〕━━╮\n" + "┃ 👉 `next` লিখুন\n" + "╰━━━━━━━━━━━━━━━━━━━━╯"; const sent = await api.sendMessage( msg, event.threadID ); global.GoatBot.onReply.set( sent.messageID, { commandName: "story", step: 3, title: Reply.title, author: Reply.author, category: Reply.category, pages: pages, page: nextPage } ); return; } const number = parseInt( input, 10 ); if ( !Number.isInteger(number) || number < 1 ) { return api.sendMessage( "❌ সঠিক নম্বর দিন।", event.threadID ); } /* * CATEGORY */ if (Reply.step === 1) { if ( number > CATEGORIES.length ) { return api.sendMessage( "❌ 1 থেকে " + CATEGORIES.length + " এর মধ্যে নম্বর দিন।", event.threadID ); } const category = CATEGORIES[number - 1]; await api.sendMessage( "╭━━〔 🔎 গল্প খোঁজা হচ্ছে 〕━━╮\n" + "┃\n" + "┃ 🏷️ " + category[0] + "\n" + "┃ 🌐 Online API\n" + "┃ 📚 তথ্য সংগ্রহ করা হচ্ছে...\n" + "┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯", event.threadID ); try { const result = await searchStory(category[1]); const worksResult = await getWorks(); const hits = Array.isArray(result.hits) ? result.hits : []; const works = Array.isArray( worksResult.works ) ? worksResult.works : []; if (!hits.length) { return api.sendMessage( "❌ এই বিভাগে এখন কোনো গল্প পাওয়া যায়নি।", event.threadID ); } const list = hits .map((hit, index) => { const work = findWork( works, hit.work_id ); const title = work && work.title ? work.title : "বাংলা সাহিত্য #" + (index + 1); const author = work && ( work.author_bn || work.author ) ? ( work.author_bn || work.author ) : "অজানা লেখক"; return ( "┃ " + (index + 1) + ". " + title + "\n" + "┃ ✍️ " + author ); }) .join("\n"); const msg = "╭━━〔 📰 গল্পের তালিকা 〕━━╮\n" + "┃ 🏷️ " + category[0] + "\n" + "┃\n" + list + "\n┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯\n\n" + "🔢 গল্প পড়তে নম্বর লিখুন।"; const sent = await api.sendMessage( msg, event.threadID ); global.GoatBot.onReply.set( sent.messageID, { commandName: "story", step: 2, category: category[0], hits: hits, works: works } ); } catch (error) { console.error( "STORY CATEGORY ERROR:", error ); return api.sendMessage( "❌ অনলাইন গল্প সার্ভার থেকে তথ্য আনা যাচ্ছে না।\n\n🔄 কিছুক্ষণ পরে আবার চেষ্টা করুন।", event.threadID ); } return; } /* * STORY SELECT */ if (Reply.step === 2) { const hits = Array.isArray(Reply.hits) ? Reply.hits : []; const works = Array.isArray(Reply.works) ? Reply.works : []; if ( number > hits.length ) { return api.sendMessage( "❌ 1 থেকে " + hits.length + " এর মধ্যে নম্বর দিন।", event.threadID ); } const selected = hits[number - 1]; const work = findWork( works, selected.work_id ); if (!work) { return api.sendMessage( "❌ এই গল্পের তথ্য পাওয়া যায়নি।", event.threadID ); } const title = work.title || "বাংলা সাহিত্য"; const author = work.author_bn || work.author || "অজানা লেখক"; await api.sendMessage( "📚 `" + title + "` সংগ্রহ করা হচ্ছে...\n\n" + "⏳ অনলাইন থেকে বড় অংশ আনা হচ্ছে...", event.threadID ); try { const queries = [ title, ...SEARCH_WORDS ]; const allHits = []; for (const query of queries) { try { const result = await searchStory(query); const list = Array.isArray(result.hits) ? result.hits : []; for (const hit of list) { if ( String(hit.work_id) === String(selected.work_id) ) { allHits.push(hit); } } } catch (e) { console.error( "SEARCH ERROR:", e.message ); } } const unique = []; const seen = new Set(); for (const hit of allHits) { const id = hit.passage_id || ( String(hit.work_id) + ":" + String( hit.section_index || 0 ) + ":" + JSON.stringify( hit.offsets || [] ) ); if (!seen.has(id)) { seen.add(id); unique.push(hit); } } unique.sort((a, b) => { const sa = Number( a.section_index || 0 ); const sb = Number( b.section_index || 0 ); if (sa !== sb) { return sa - sb; } const oa = Array.isArray(a.offsets) ? Number(a.offsets[0] || 0) : 0; const ob = Array.isArray(b.offsets) ? Number(b.offsets[0] || 0) : 0; return oa - ob; }); let fullText = ""; for (const hit of unique) { const text = cleanText(hit.text); if (!text) { continue; } if ( fullText.includes(text) ) { continue; } fullText += (fullText ? "\n\n" : "") + text; } if (!fullText) { fullText = cleanText( selected.text ); } if (!fullText) { return api.sendMessage( "❌ এই গল্পের কোনো পাঠ্যাংশ পাওয়া যায়নি।", event.threadID ); } const pages = splitText( fullText, 3500 ); if (!pages.length) { return api.sendMessage( "❌ গল্প প্রস্তুত করা যায়নি।", event.threadID ); } const msg = "╭━━〔 📰 বাংলা গল্প পত্রিকা 〕━━╮\n" + "┃\n" + "┃ 📝 " + title + "\n" + "┃ ✍️ " + author + "\n" + "┃ 🏷️ " + Reply.category + "\n" + "┃ 📖 অংশ 1/" + pages.length + "\n" + "┃\n" + "╰━━━━━━━━━━━━━━━━━━━━╯\n\n" + pages[0] + "\n\n" + "╭━━〔 📖 পরবর্তী অংশ 〕━━╮\n" + "┃ 👉 `next` লিখুন\n" + "╰━━━━━━━━━━━━━━━━━━━━╯"; const sent = await api.sendMessage( msg, event.threadID ); global.GoatBot.onReply.set( sent.messageID, { commandName: "story", step: 3, title: title, author: author, category: Reply.category, pages: pages, page: 0 } ); } catch (error) { console.error( "FULL STORY ERROR:", error ); return api.sendMessage( "❌ গল্পের বড় অংশ সংগ্রহ করা যায়নি।\n\n🔄 আবার চেষ্টা করুন।", event.threadID ); } } };