ts-api / pages /api /chatgpt3.js
Shuddho's picture
Upload 42 files
d04e364 verified
// pages/api/chat.js
import axios from 'axios';
export const config = {
"name": "chatgpt3",
"url": "/api/chatgpt3",
"description": "Get response from Chatgpt3. Chatgpt3 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/chatgpt3?prompt=hello"
}
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: "chatbot-qm966k",
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
};
// 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`);
}
}