-
Notifications
You must be signed in to change notification settings - Fork 34
/
api.js
37 lines (32 loc) · 999 Bytes
/
api.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
const openai = require("openai");
const openaiClient = new openai.OpenAI(process.env.OPENAI_API_KEY);
const model = process.env.OPENAI_MODEL || "gpt-4-1106-preview";
const createWordpressDraft = async (title, content) => {
const wordpressApiUrl = "https://7.dev/wp-json/wp/v2/posts";
const username = process.env.WP_USERNAME;
const password = process.env.WP_PASSWORD;
const token = Buffer.from(`${username}:${password}`).toString("base64");
try {
const response = await fetch(wordpressApiUrl, {
method: "POST",
headers: {
"Authorization": `Basic ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
title: title,
content: content,
status: "draft",
}),
});
const data = await response.json();
console.log("Draft created:", data);
} catch (error) {
console.error("Error creating draft:", error);
}
};
module.exports = {
createWordpressDraft,
openaiClient,
model,
};