44 lines
1.7 KiB
JavaScript
44 lines
1.7 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* Publish Post to Facebook Page via Meta Graph API v19.0
|
|
*/
|
|
|
|
const ACCESS_TOKEN = process.env.META_ACCESS_TOKEN || 'EAAM8GXpb76wBSCcthLkKMySDBEMP6mCxl1yIeNnPUQ4oSmO15L56TRWPh5LZBfAPJmtYYzPIul5QeogjTvcVrwg4FrTxfZAkxB0kEgjONeUYH5QzTHUYkbfW9N2GyUDsPIke0esrxvfnLXU5Vm4wdJ7pCpyNZB3nnN00Ek4ngriwWsGWZBP9HZCPAMMPFVfH6LVdZAcyl8f31rNYKsL2vp0IZClMVWuNoif6Jlr3SRmfD1tnPIuFz3BnPDp2ymXXVAqsLc2c5rX5EgtkrdMdGdu';
|
|
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 };
|