-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
96 lines (83 loc) · 2.63 KB
/
index.js
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
const express = require("express");
const axios = require("axios");
const cors = require("cors");
const app = express();
app.use(express.json());
app.use(cors());
const endpoint =
"https://us-central1-chat-for-chatgpt.cloudfunctions.net/basicUserRequestBeta";
function sendResponse(res, status, message) {
res.setHeader("Content-Type", "application/json");
res.status(status).send(JSON.stringify({ status, message }, null, 2));
}
app.get("/", async (req, res) => {
const text = req.query.text;
if (!text) {
sendResponse(res, 400, "Please enter text parameter");
return;
}
try {
const response = await axios.post(
endpoint,
{
data: {
message: text,
},
},
{
headers: {
Host: "us-central1-chat-for-chatgpt.cloudfunctions.net",
Connection: "keep-alive",
Accept: "*/*",
"User-Agent":
"com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5",
"Accept-Language": "en",
"Content-Type": "application/json; charset=UTF-8",
},
}
);
const result = response.data.result.choices[0].text;
sendResponse(res, 200, result);
} catch (error) {
sendResponse(res, 403, "Error connecting to openai");
}
});
app.post("/", async (req, res) => {
const text = req.body.text;
if (!text) {
sendResponse(res, 400, "Please enter text parameter");
return;
}
try {
const response = await axios.post(
endpoint,
{
data: {
message: text,
},
},
{
headers: {
Host: "us-central1-chat-for-chatgpt.cloudfunctions.net",
Connection: "keep-alive",
Accept: "*/*",
"User-Agent":
"com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5",
"Accept-Language": "en",
"Content-Type": "application/json; charset=UTF-8",
},
}
);
const result = response.data.result.choices[0].text;
sendResponse(res, 200, result);
} catch (error) {
sendResponse(res, 403, "Error connecting to openai");
}
});
app.use((err, req, res, next) => {
console.error(err.stack);
sendResponse(res, 500, "Something broke!");
});
app.listen(3000, () => {
console.log("ChatGPT API is running on port 3000");
});