|
|
|
|
|
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') { |
|
|
|
const { prompt } = req.query; |
|
|
|
|
|
const url = "https://chatgpt4online.org/wp-json/mwai-ui/v1/chats/submit"; |
|
|
|
|
|
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]; |
|
|
|
|
|
const payload = { |
|
botId: "chatbot-qm966k", |
|
newMessage: prompt || "Hi", |
|
newFileId: null, |
|
stream: false |
|
}; |
|
|
|
|
|
const headers = { |
|
'X-Wp-Nonce': restNonce |
|
}; |
|
|
|
|
|
const postResponse = await axios.post(url, payload, { headers }); |
|
|
|
console.log("Chat message submitted successfully."); |
|
|
|
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); |
|
|
|
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`); |
|
} |
|
} |
|
|