-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
93 lines (78 loc) · 3.2 KB
/
script.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* An example of automatically posting to linked social media accounts in Airtable using Ayrshare.
* This example allows you to post to either a single profile or multiple client profiles and their linked social networks.
*
* Get your free API Key at https://www.ayrshare.com and enter in the script the key API_KEY = "API Key";
*
* The script expects a table with fields: Post, Platforms, Images, Profile Keys, and Status.
* Please see for an example: https://airtable.com/shrCWY0oA1ghB42tI/tblpnTEiPwyuViqBo
*
* When status is "pending" the post will be processed and sent. The resulting post status will be updated in the Status field.
* Post to one or more client profiles by entering the client Profile Keys, comma separated without spaces
*
* Copyright ⓒ Ayrshare & Nevermind Solutions, LLC 2021
*/
const API_KEY = 'Your API Key'; // Get a free key at ayrshare.com
const sendPost = async (data) => {
const { post, platforms, media_uls, profileKeys, scheduleDate } = data;
const body = Object.assign({},
post && { post },
platforms && { platforms },
profileKeys && { profileKeys: profileKeys.split(",") },
media_uls && Array.isArray(media_uls) && media_uls.length > 0 && { media_urls: media_uls.map(image => image.url) },
scheduleDate && { scheduleDate }
);
if (media_uls && Array.isArray(media_uls) && media_uls.length > 0) {
body.media_urls = media_uls.map(image => image.url);
}
if (profileKeys) {
body.profileKeys = profileKeys.split(",");
}
const response = await fetch("https://app.ayrshare.com/api/post", {
method: 'POST',
body: JSON.stringify(body),
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${API_KEY}`
}
}).then(res => res.json());
return response;
};
const table = base.getTable("Posts");
const query = await table.selectRecordsAsync();
const filteredRecords = query.records.filter(record => {
const status = record.getCellValue('Status');
const post = record.getCellValue('Post');
const platforms = record.getCellValue('Platforms');
return (post && platforms && (status === "pending" || !status));
});
if (filteredRecords.length === 0) {
console.log("No records found with status `pending` or empty");
}
for (let record of filteredRecords) {
const post = record.getCellValue('Post');
const images = record.getCellValue('Images');
const platforms = record.getCellValue('Platforms');
const profileKeys = record.getCellValue('Profile Keys');
const scheduleDate = record.getCellValue('Schedule Date');
const response = await sendPost(
{
post,
platforms: platforms.map(x => x.name),
media_uls: images,
profileKeys,
scheduleDate,
});
if (response) {
let status;
if (Array.isArray(response)) {
status = response.map(x => x.status).every(x => x === "success" || x === "scheduled") ? "success" : "error";
} else {
status = response.status;
}
console.log(response);
await table.updateRecordAsync(record, {
"Status": status
});
}
}