This repository was archived by the owner on Aug 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (52 loc) · 1.88 KB
/
index.js
File metadata and controls
64 lines (52 loc) · 1.88 KB
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
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.use(bodyParser.json());
app.post("/bard", async (req, res) => {
const { question } = req.body;
const { default: fetch } = await import("node-fetch");
const BARD_URL = "https://bard.google.com/_/BardChatUi/data/assistant.lamda.BardFrontendService/StreamGenerate";
const SECURE1PSID = process.env.SECURE1PSID;
const AT_KEY = process.env.AT_KEY;
const params = new URLSearchParams({
bl: "boq_assistant-bard-web-server_20230613.09_p0",
_reqid: Number(Math.random().toString().slice(2, 8)),
rt: "c",
});
const messageRequest = [[question], null, ["", "", ""]];
const headers = {
"X-Same-Domain": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
"Sec-Fetch-Site": "same-origin",
"Sec-Fetch-Mode": "cors",
"Sec-Fetch-Dest": "empty",
Cookie: `__Secure-1PSID=${SECURE1PSID};`,
};
const urlencoded = new URLSearchParams();
urlencoded.append("at", AT_KEY);
urlencoded.append(
"f.req",
JSON.stringify([null, JSON.stringify(messageRequest)])
);
const requestOptions = {
method: "POST",
headers: headers,
body: urlencoded,
redirect: "follow",
};
try {
const request = await fetch(`${BARD_URL}?${params}`, requestOptions);
const response = await request.text();
const output = JSON.parse(response.split(/\r?\n/)[3])[0][2];
const content = JSON.parse(output)[0][0];
console.log(content);
res.json({ result: content });
} catch (error) {
console.error(error);
res.status(500).json({ error: "An error occurred" });
}
});
app.listen(process.env.PORT || 3000, () => {
console.log(`Server is running on port ${process.env.PORT || 3000}`);
});