File size: 3,909 Bytes
d04e364
 
 
 
 
 
 
 
 
 
 
 
935faaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d04e364
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
935faaf
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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`);
  }
}