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