Spaces:
Sleeping
Sleeping
File size: 5,339 Bytes
e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 91d2cae e4deac6 fdf2e1b e4deac6 91d2cae fdf2e1b e4deac6 |
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const axios = require('axios');
const { DateTime } = require('luxon');
const { format } = require("util")
const port = 7860;
app.use(bodyParser.json());
const chatHistory = {};
const generateRandomString = (length) => {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
};
const generateRandomIP = () => {
const octet = () => Math.floor(Math.random() * 256);
return `${octet()}.${octet()}.${octet()}.${octet()}`;
};
async function createNewChat() {
try {
const randomUserId = generateRandomString(17);
const randomIP = generateRandomIP();
const response = await axios.post('https://chat.chatgptdemo.net/new_chat', {
user_id: randomUserId
}, {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
'X-Forwarded-For': randomIP
}
});
return response.data.id_;
} catch (error) {
console.error(error);
return false;
}
}
async function updateChatName(id, chatName) {
try {
const response = await axios.post('https://chat.chatgptdemo.net/update_chat_name', {
chat_id: id,
chat_name: chatName
}, {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
'X-Forwarded-For': generateRandomIP()
}
});
} catch (error) {
console.error(error);
}
}
async function sendMessage(question, id, retry) {
try {
const response = await axios.post('https://chat.chatgptdemo.net/chat_api_stream', {
question: question,
chat_id: id,
timestamp: Date.now(),
retry: retry
}, {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
'X-Forwarded-For': generateRandomIP()
}
});
return response.data;
} catch (error) {
console.error(error);
}
}
async function updateMessages(id, message) {
const url = 'https://chat.chatgptdemo.net/update_messages';
const data = {
chat_id: id,
bot_response: message,
timestamp: Date.now(),
};
try {
const response = await axios.post(url, data, {
headers: {
'Content-Type': 'application/json',
'User-Agent': 'Mozilla/5.0 (iPhone14,3; U; CPU iPhone OS 15_0 like Mac OS X) AppleWebKit/602.1.50 (KHTML, like Gecko) Version/10.0 Mobile/19A346 Safari/602.1',
'X-Forwarded-For': generateRandomIP()
}
});
return response.data;
} catch (error) {
console.error(error);
}
}
async function cleanInactiveUsers() {
const currentTime = DateTime.local();
Object.keys(chatHistory).forEach((userId) => {
const user = chatHistory[userId];
if (user && user.lastChat && user.lastChat.plus(Duration.fromObject({ minutes: 10 })) < currentTime) {
// User's last message was more than 10 minutes ago, remove the user from chatHistory
delete chatHistory[userId];
}
});
// Schedule the next execution after 1 minute
setTimeout(cleanInactiveUsers, 30000);
}
// Start the continuous execution of cleanInactiveUsers
cleanInactiveUsers();
app.get('/send', async (req, res) => {
try {
const { id, message } = req.query;
if(!id)return res.status(400).json({ success: false, response: "input param id & input id user"});
if(!message)return res.status(400).json({ success: false, response: "input param message & input a message from user"});
if (!chatHistory[id]) {
const newid = await createNewChat();
chatHistory[id] = {
lastChat: DateTime.local(),
chat_id: newid,
message: [],
};
}
const role = 'user';
chatHistory[id].message.push({ role, message });
chatHistory[id].lastChat = DateTime.local();
id_room = chatHistory[id].chat_id
const botResponse = await sendMessage(message, id_room, false);
const regex = /"content":"(.*?)"/g;
let matches;
let contents = '';
while ((matches = regex.exec(botResponse)) !== null) {
if (typeof matches[1] !== 'undefined') {
contents += matches[1];
}
}
chatHistory[id].message.push({ role: 'Assistant', message: contents });
chatHistory[id].lastChat = DateTime.local();
await updateMessages(id_room, contents);
res.status(200).json({ success: true, response: contents });
} catch (e) {
res.status(400).json({ success: false, response: format(e) });
}
});
app.get('/listuser', (req, res) => {
const userList = Object.keys(chatHistory);
res.status(200).json({ userList });
});
app.get('/get/message/:id', (req, res) => {
const id = req.params.id;
if(!id)return res.status(400).json({ success: false, response: "input id"});
if (!chatHistory[id]) {
return res.status(404).json({ error: 'User not found' });
}
const userMessages = chatHistory[id].message;
const userRoom = chatHistory[id].chat_id;
res.status(200).json({ id, messages: userMessages });
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
}); |