Update codebase
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* Publish Post to Facebook Page via Meta Graph API v19.0
|
||||
*/
|
||||
|
||||
const ACCESS_TOKEN = process.env.META_ACCESS_TOKEN || 'EAAM8GXpb76wBSLELO5tWBvXM2NG6aSsZBjHUHKK35nATiw5GFgyEAJ5WoiK68NudGqpGlcl2mZB6bUpR5KbtSFPzIpaANhbpXsjDWo8UFCNbWOHKcnGcXM20G4umUoLsoyBNYg9la80wTEhVHFmQmubp7q7ZCVZCrec4BnzXk5iGDMZC38JJOZBRKFMMJHdwthy7';
|
||||
const PAGE_ID = process.env.META_PAGE_ID || '61588173848643';
|
||||
|
||||
async function publishPost(message, imageUrl = null) {
|
||||
if (!PAGE_ID) {
|
||||
throw new Error('META_PAGE_ID (Page ID) is required to publish a post.');
|
||||
}
|
||||
|
||||
const endpoint = imageUrl ? `${PAGE_ID}/photos` : `${PAGE_ID}/feed`;
|
||||
const url = new URL(`https://graph.facebook.com/v19.0/${endpoint}`);
|
||||
url.searchParams.append('access_token', ACCESS_TOKEN);
|
||||
|
||||
const payload = imageUrl ? { caption: message, url: imageUrl } : { message };
|
||||
|
||||
const res = await fetch(url.toString(), {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload)
|
||||
});
|
||||
|
||||
const data = await res.json();
|
||||
if (data.error) {
|
||||
throw new Error(`Meta Graph API Error: ${data.error.message}`);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
if (require.main === module) {
|
||||
const pageId = process.argv[2] || PAGE_ID;
|
||||
const msg = process.argv[3] || 'اختبار النشر التلقائي عبر Tripz AI Agent';
|
||||
if (!pageId) {
|
||||
console.error('Please provide Page ID as first parameter: node scripts/publish-facebook-post.js <PAGE_ID> "[Message]"');
|
||||
process.exit(1);
|
||||
}
|
||||
publishPost(msg).then(res => console.log('Post published successfully:', res)).catch(err => console.error(err.message));
|
||||
}
|
||||
|
||||
module.exports = { publishPost };
|
||||
Reference in New Issue
Block a user