Spaces:
Sleeping
Sleeping
| const axios = require('axios'); | |
| const TEMP_MAIL_API = 'https://api.internal.temp-mail.io/api/v3'; | |
| const CHATAI_API = 'https://chataibot.pro/api'; | |
| const headers = { | |
| 'Content-Type': 'application/json', | |
| 'Accept-Language': 'en' | |
| }; | |
| const tempMailHeaders = { | |
| ...headers, | |
| 'Application-Name': 'web', | |
| 'Application-Version': '4.0.0', | |
| 'X-CORS-Header': 'iaWg3pchvFx48fY' | |
| }; | |
| const handler = async (req, res) => { | |
| try { | |
| const { text } = req.query; | |
| if (!text) { | |
| return res.status(400).json({ | |
| success: false, | |
| error: 'Missing required parameter: text' | |
| }); | |
| } | |
| const result = await gpt4(text, chatId = null); | |
| res.json({ | |
| author: "Herza", | |
| success: true, | |
| data: { | |
| msg: result.response, | |
| chatID: result.chatId | |
| } | |
| }); | |
| } catch (error) { | |
| res.status(500).json({ | |
| success: false, | |
| error: error.message | |
| }); | |
| } | |
| }; | |
| module.exports = { | |
| name: 'ChatGPT V4', | |
| description: 'Generate responses using GPT4 OpenAI Model v4', | |
| type: 'GET', | |
| routes: ['api/AI/gpt4'], | |
| tags: ['ai', 'OpenAI', 'GPT4'], | |
| main: ['AI'], | |
| parameters: ['text', 'chatId', 'key'], | |
| enabled: true, | |
| limit: 8, | |
| handler | |
| }; | |
| async function createTempEmail() { | |
| const { data } = await axios.post(`${TEMP_MAIL_API}/email/new`, { | |
| min_name_length: 10, | |
| max_name_length: 10 | |
| }, { headers: tempMailHeaders }); | |
| return data; | |
| } | |
| async function registerAccount(email) { | |
| const { data } = await axios.post(`${CHATAI_API}/register`, { | |
| email, | |
| password: 'Mxlineeecero5632@_++((+))9098+-+-!;::::#$', | |
| isAdvertisingAccepted: false, | |
| mainSiteUrl: 'https://chataibot.pro/api', | |
| utmSource: '', | |
| utmCampaign: '', | |
| connectBusiness: '' | |
| }, { headers }); | |
| return data; | |
| } | |
| async function getMessages(email) { | |
| const { data } = await axios.get(`${TEMP_MAIL_API}/email/${email}/messages`, { | |
| headers: tempMailHeaders | |
| }); | |
| return data; | |
| } | |
| async function waitForVerificationCode(email, maxAttempts = 10) { | |
| for (let i = 0; i < maxAttempts; i++) { | |
| await new Promise(r => setTimeout(r, 3000)); | |
| const msgs = await getMessages(email); | |
| const chatAiMsg = msgs.find(m => m.from.includes('chataibot.pro')); | |
| if (chatAiMsg) { | |
| const match = chatAiMsg.body_text.match(/Your code: (\d+)/); | |
| if (match) return match[1]; | |
| } | |
| } | |
| throw new Error('Verification code not received'); | |
| } | |
| async function verifyAccount(email, token) { | |
| const { data, headers: responseHeaders } = await axios.post(`${CHATAI_API}/register/verify`, { | |
| email, | |
| token, | |
| connectBusiness: '' | |
| }, { headers }); | |
| const cookies = responseHeaders['set-cookie'] || []; | |
| return { jwtToken: data.jwtToken, cookies }; | |
| } | |
| async function createChat(jwtToken, cookies, title = 'New Chat') { | |
| const cookieString = cookies.map(c => c.split(';')[0]).join('; '); | |
| await axios.post(`${CHATAI_API}/message/change-context-model`, { | |
| chatId: 0, | |
| title, | |
| isInternational: true | |
| }, { | |
| headers: { | |
| ...headers, | |
| 'Authorization': `Bearer ${jwtToken}`, | |
| 'Cookie': cookieString | |
| } | |
| }); | |
| const { data } = await axios.post(`${CHATAI_API}/message/context`, { | |
| title, | |
| chatModel: 'gpt-4.1-nano' | |
| }, { | |
| headers: { | |
| ...headers, | |
| 'Authorization': `Bearer ${jwtToken}`, | |
| 'Cookie': cookieString | |
| } | |
| }); | |
| return data.id; | |
| } | |
| async function sendMessage(jwtToken, cookies, text, chatId) { | |
| const cookieString = cookies.map(c => c.split(';')[0]).join('; '); | |
| const { data } = await axios.post(`${CHATAI_API}/message/streaming`, { | |
| text, | |
| chatId, | |
| withPotentialQuestions: true, | |
| linksToParse: [] | |
| }, { | |
| headers: { | |
| ...headers, | |
| 'Authorization': `Bearer ${jwtToken}`, | |
| 'Cookie': cookieString | |
| }, | |
| responseType: 'text' | |
| }); | |
| const lines = data.split('}{').map((line, i, arr) => { | |
| if (i === 0) return line + '}'; | |
| if (i === arr.length - 1) return '{' + line; | |
| return '{' + line + '}'; | |
| }); | |
| let result = ''; | |
| for (const line of lines) { | |
| try { | |
| const json = JSON.parse(line); | |
| if (json.type === 'chunk') { | |
| result += json.data; | |
| } else if (json.type === 'finalResult') { | |
| return json.data.mainText; | |
| } | |
| } catch (e) {} | |
| } | |
| return result; | |
| } | |
| let cachedToken = null; | |
| let cachedCookies = null; | |
| let cachedChatId = null; | |
| async function gpt4(query, chatId = null) { | |
| try { | |
| if (!cachedToken) { | |
| const { email } = await createTempEmail(); | |
| await registerAccount(email); | |
| const code = await waitForVerificationCode(email); | |
| const verifyResult = await verifyAccount(email, code); | |
| cachedToken = verifyResult.jwtToken; | |
| cachedCookies = verifyResult.cookies; | |
| } | |
| if (!chatId && !cachedChatId) { | |
| cachedChatId = await createChat(cachedToken, cachedCookies, query); | |
| } | |
| const targetChatId = chatId || cachedChatId; | |
| const response = await sendMessage(cachedToken, cachedCookies, query, targetChatId); | |
| return { response, chatId: targetChatId }; | |
| } catch (err) { | |
| cachedToken = null; | |
| cachedCookies = null; | |
| cachedChatId = null; | |
| throw err; | |
| } | |
| } |