-
Notifications
You must be signed in to change notification settings - Fork 0
/
threads.js
57 lines (50 loc) · 1.76 KB
/
threads.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
const axios = require('axios');
require('dotenv').config();
// Document-level scoped variables
const USER_ID = process.env.USER_ID;
const ACCESS_TOKEN = process.env.ACCESS_TOKEN;
async function createThreadsMediaContainer(content) {
try {
const response = await axios.post(`https://graph.threads.net/v1.0/${USER_ID}/threads`, {
media_type: "TEXT",
text: content,
access_token: ACCESS_TOKEN
});
return response.data.id; // This is the media container ID
} catch (error) {
console.error('Error creating media container:', error.message);
throw error;
}
}
async function publishThreadsMediaContainer(containerIds) {
try {
const response = await axios.post(`https://graph.threads.net/v1.0/${USER_ID}/threads_publish`, {
creation_id: containerIds,
access_token: ACCESS_TOKEN
});
return response.data.id; // This is the published Threads Media ID
} catch (error) {
console.error('Error publishing media container:', error);
throw error;
}
}
async function postToThreads(content) {
try {
// Create media container
console.log(content);
console.log("Waiting to start container...");
const containerId = await createThreadsMediaContainer(content);
// Wait for 30 seconds (you might want to adjust this)
await new Promise(resolve => setTimeout(resolve, 3000));
console.log("Waiting for container...");
// Publish the media container
const publishedId = await publishThreadsMediaContainer(containerId);
console.log("waiting to publish...");
console.log('Successfully posted to Threads. Media ID:', publishedId);
return publishedId;
} catch (error) {
console.error('Error posting to Threads:', error);
throw error;
}
}
module.exports = { postToThreads };