File size: 2,389 Bytes
d04e364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// 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`);
  }
}