Skip to content

Commit 2aad993

Browse files
authored
1.0
1 parent 2521dad commit 2aad993

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

index.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
const express = require('express');
2+
const axios = require('axios');
3+
const cors = require('cors');
4+
5+
const app = express();
6+
app.use(express.json());
7+
app.use(cors({
8+
origin: '*',
9+
methods: ['GET', 'POST'],
10+
allowedHeaders: ['Content-Type']
11+
}));
12+
13+
const endpoint = 'https://us-central1-chat-for-chatgpt.cloudfunctions.net/basicUserRequestBeta';
14+
15+
function sendResponse(res, status, code, message) {
16+
res.setHeader('Content-Type', 'application/json');
17+
res.status(status).send(JSON.stringify({ status, code, message }, null, 2));
18+
}
19+
20+
app.get('/', async (req, res) => {
21+
const text = req.query.text;
22+
23+
if (!text) {
24+
sendResponse(res, 400, 400, 'Please enter text parameter');
25+
return;
26+
}
27+
28+
try {
29+
const response = await axios.post(endpoint, {
30+
data: {
31+
message: text
32+
}
33+
}, {
34+
headers: {
35+
'Host': 'us-central1-chat-for-chatgpt.cloudfunctions.net',
36+
'Connection': 'keep-alive',
37+
'Accept': '*/*',
38+
'User-Agent': 'com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5',
39+
'Accept-Language': 'ar',
40+
'Content-Type': 'application/json; charset=UTF-8'
41+
}
42+
});
43+
44+
const result = response.data.result.choices[0].text;
45+
sendResponse(res, 200, 200, result);
46+
} catch (error) {
47+
sendResponse(res, 403, 403, 'Error connecting to openai');
48+
}
49+
});
50+
51+
app.post('/', async (req, res) => {
52+
const text = req.body.text;
53+
54+
if (!text) {
55+
sendResponse(res, 400, 400, 'Please enter text parameter');
56+
return;
57+
}
58+
59+
try {
60+
const response = await axios.post(endpoint, {
61+
data: {
62+
message: text
63+
}
64+
}, {
65+
headers: {
66+
'Host': 'us-central1-chat-for-chatgpt.cloudfunctions.net',
67+
'Connection': 'keep-alive',
68+
'Accept': '*/*',
69+
'User-Agent': 'com.tappz.aichat/1.2.2 iPhone/16.3.1 hw/iPhone12_5',
70+
'Accept-Language': 'ar',
71+
'Content-Type': 'application/json; charset=UTF-8'
72+
}
73+
});
74+
75+
const result = response.data.result.choices[0].text;
76+
sendResponse(res, 200, 200, result);
77+
} catch (error) {
78+
sendResponse(res, 403, 403, 'Error connecting to openai');
79+
}
80+
});
81+
82+
app.listen(3000, () => {
83+
console.log('ChatGPT API is running on port 3000');
84+
});

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"dependencies": {
3+
"axios": "^1.4.0",
4+
"cors": "^2.8.5",
5+
"express": "^4.18.2"
6+
}
7+
}

0 commit comments

Comments
 (0)