ts-api / pages /api /chatgpt4.js
Shuddho's picture
Update pages/api/chatgpt4.js
935faaf verified
// pages/api/chat.js
import axios from 'axios';
export const config = {
"name": "chatgpt4",
"url": "/api/chatgpt4",
"description": "Get response from Chatgpt4. Chatgpt4 is a ChatGPT API. It can be used to get responses from ChatGPT. It can provide you with the latest news, weather, and more.",
"query": "prompt",
"response": "text",
"testURL": "./api/chatgpt4?prompt=hello"
}
function generateUserAgent() {
const osList = [
"Windows NT 10.0; Win64; x64",
"Windows NT 10.0; WOW64",
"Windows NT 6.1; Win64; x64",
"Windows NT 6.1; WOW64",
"Macintosh; Intel Mac OS X 10_15_7",
"Macintosh; Intel Mac OS X 10_14_6",
"X11; Ubuntu; Linux x86_64",
"X11; Linux x86_64"
];
const webkitVersion = `${getRandomInt(500, 600)}.${getRandomInt(0, 99)}.${getRandomInt(0, 999)}`;
const chromeVersion = `Chrome/${getRandomInt(80, 90)}.${getRandomInt(0, 9999)}.${getRandomInt(0, 999)}`;
const safariVersion = `Safari/${getRandomInt(500, 600)}.${getRandomInt(0, 99)}.${getRandomInt(0, 999)}`;
const firefoxVersion = `Gecko/20100101 Firefox/${getRandomInt(80, 90)}.${getRandomInt(0, 99)}`;
const os = randomChoice(osList);
const browser = randomChoice(['Chrome', 'Safari']);
const engine = randomChoice(['Edge', 'OPR']) || 'Gecko/20100101 Firefox';
return `Mozilla/5.0 (${os}) AppleWebKit/${webkitVersion} (KHTML, like Gecko) ${browser}/${browser === 'Chrome' ? chromeVersion : safariVersion} ${engine}/${browser === 'Chrome' ? chromeVersion : firefoxVersion}`;
}
const userAgent = generateUserAgent();
const navigator = {
userAgent: userAgent,
};
console.log(userAgent);
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomChoice(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
export default async function handler(req, res) {
if (req.method === 'GET') {
// Extract prompt from the query parameters
const { prompt } = req.query;
// Define the endpoint URL
const url = "https://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit";
// Get the HTML content of the chat page to extract the X-Wp-Nonce
try {
const response = await axios.get('https://chatgpt4online.org/#chat');
const string = response.data;
const regex = /restNonce":"([^&]+)"/;
const match = string.match(regex);
if (match) {
const restNonce = match[1];
// Define the payload data
const payload = {
botId: "default",
newMessage: prompt || "Hi", // Using prompt or "Hi" if prompt is not provided
newFileId: null,
stream: false
};
// Define the headers
const headers = {
'X-Wp-Nonce': restNonce,
'User-Agent': generateUserAgent()
};
// Send POST request with headers
const postResponse = await axios.post(url, payload, { headers });
console.log("Chat message submitted successfully.");
// Optionally, you can log the response data
console.log("Response data:", postResponse.data);
res.status(200).json({ message: "Chat message submitted successfully", responseData: postResponse.data });
} else {
console.log('No match found');
res.status(500).json({ message: "Failed to fetch X-Wp-Nonce" });
}
} catch (error) {
console.error("Failed to submit chat message:", error.message);
// Optionally, you can log the error response data
if (error.response) {
console.error("Error response data:", error.response.data);
}
res.status(error.response?.status || 500).json({ message: "Failed to submit chat message" });
}
} else {
res.setHeader('Allow', ['GET']);
res.status(405).end(`Method ${req.method} Not Allowed`);
}
}