const express = require('express'); const bodyParser = require('body-parser'); const axios = require('axios'); const { DateTime, Duration } = require('luxon'); const { format } = require("util"); const puppeteer = require("puppeteer"); const { GoogleGenerativeAI } = require("@google/generative-ai"); const GPT4js = require('gpt4js'); const app = express(); const port = 7860; app.use(bodyParser.json()); // Fungsi untuk mengonversi teks menjadi Base64 function convertTextToBase64(text) { return Buffer.from(text).toString('base64'); } // Fungsi untuk mengembalikan teks dari Base64 function convertBase64ToText(base64) { return Buffer.from(base64, 'base64').toString('utf-8'); } // Inisialisasi GoogleGenerativeAI const genAI = new GoogleGenerativeAI(convertBase64ToText("QUl6YVN5RGFVX1dTWlduWXF4ZG9fclJhbktCM0Z0elMtQTg2dzUw")); // Data sesi chat untuk versi 2 const db_chatSessions = {}; // Fungsi untuk menghasilkan UID function generateUID(length) { const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; let uid = ''; for (let i = 0; i < length; i++) { uid += characters.charAt(Math.floor(Math.random() * characters.length)); } return uid; } // Fungsi untuk mengirim pesan versi 2 async function sendChatMessageV2(data_chat, msg) { try { const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" }); const chat = model.startChat(data_chat); const result = await chat.sendMessage(msg); const response = await result.response; const text = response.text(); return text; } catch (error) { console.error(error); } } // Fungsi untuk membersihkan pengguna tidak aktif versi 2 async function clearInactiveUsersV2() { const currentTime = DateTime.local(); Object.keys(db_chatSessions).forEach((userId) => { const user = db_chatSessions[userId]; if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 20 })) < currentTime) { // Hapus pengguna yang tidak aktif lebih dari 20 menit dari db_chatSessions delete db_chatSessions[userId]; } }); // Jadwalkan eksekusi berikutnya setelah 3 menit setTimeout(clearInactiveUsersV2, 180000); } // Mulai eksekusi terus-menerus untuk membersihkan pengguna tidak aktif clearInactiveUsersV2(); // Endpoint untuk mengirim pesan versi 2 app.get('/v2/send', async (req, res) => { try { const { id, message } = req.query; if (!id || !message) { return res.status(400).json({ success: false, response: "Input parameter id and message are required." }); } let chatData = db_chatSessions[id]; if (!chatData) { // Jika data riwayat obrolan tidak ada, buat data baru chatData = { lastChat: DateTime.local(), data: { history: [ { role: "user", parts: "hello, can you answer my question with a cute kaomoji like this 。⁠◕⁠‿⁠◕⁠。" }, { role: "model", parts: "sure (⁠◠⁠‿⁠◕⁠) , so.... (⁠ ⁠/⁠^⁠ω⁠^⁠)⁠/⁠♪⁠♪ how can I help dear user (⁠ ⁠╹⁠▽⁠╹⁠ ⁠)?", }, ], generationConfig: { maxOutputTokens: 500, }, } }; } const botResponse = await sendChatMessageV2(chatData.data, message); // Simpan pesan user ke riwayat obrolan chatData.data.history.push({ role: 'user', parts: message }); // Simpan bot response ke riwayat obrolan chatData.data.history.push({ role: 'model', parts: botResponse }); // Update waktu terakhir obrolan chatData.lastChat = DateTime.local(); db_chatSessions[id] = chatData; res.status(200).json({ success: true, response: botResponse }); } catch (e) { res.status(400).json({ success: false, response: format(e) }); } }); // Endpoint untuk daftar pengguna versi 2 app.get('/v2/listuser', (req, res) => { const userList = Object.keys(db_chatSessions); res.status(200).json({ userList }); }); // Endpoint untuk mendapatkan pesan versi 2 app.get('/v2/get/message', (req, res) => { const id = req.query.id; if (!id) return res.status(400).json({ success: false, response: "input id" }); if (!db_chatSessions[id]) { return res.status(404).json({ error: 'User not found' }); } const userMessages = db_chatSessions[id].data.history; res.status(200).json({ messages: userMessages }); }); // Fungsi untuk ping website async function pingWebsite() { const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }); const page = await browser.newPage(); await page.setUserAgent("Mozilla/5.0 (Linux; Android 10; SM-G965U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/114.0.5735.141 Mobile Safari/537.36 [FB_IAB/FB4A;FBAV/420.0.0.32.61;]"); await page.goto('https://huggingface.co/spaces/Nexchan/gptdemo'); console.log("Ping"); await browser.close(); } // Ping website setiap 5 jam async function pingEvery5Hours() { await pingWebsite(); setInterval(async () => { await pingWebsite(); }, 5 * 60 * 60 * 1000); // 5 jam dalam milidetik } // Mulai ping pingEvery5Hours(); app.listen(port, () => { console.log(`Server is running on port ${port}`); });