-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstaindex.js
More file actions
796 lines (667 loc) · 30.3 KB
/
Copy pathinstaindex.js
File metadata and controls
796 lines (667 loc) · 30.3 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
import express from 'express';
import axios from 'axios';
import dotenv from 'dotenv';
import cors from 'cors';
import sharp from 'sharp';
dotenv.config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors());
// ─────────────────────────────────────────────
// Configuration
// ─────────────────────────────────────────────
const {
INSTAGRAM_APP_SECRET,
INSTAGRAM_ACCESS_TOKEN,
INSTAGRAM_BUSINESS_ACCOUNT_ID,
API_SECRET_KEY,
CONTENT_HUB_URL,
CONTENT_HUB_USERNAME,
CONTENT_HUB_PASSWORD,
} = process.env;
const INSTAGRAM_GRAPH_API_VERSION = 'v22.0';
const INSTAGRAM_GRAPH_URL = `https://graph.instagram.com/${INSTAGRAM_GRAPH_API_VERSION}`;
// In-memory token cache — avoids re-authenticating for every proxy request
const tokenCache = { token: null, expiry: 0 };
// ─────────────────────────────────────────────
// ROOT — mirrors LinkedIn '/'
// ─────────────────────────────────────────────
app.get('/', (req, res) => {
res.json({
status: '✅ Instagram Content Hub Middleware is running',
endpoints: {
publishImage: 'POST /api/instagram/publish-image',
publishVideo: 'POST /api/instagram/publish-video',
imageProxy: 'GET /api/instagram/image-proxy/:assetId',
insights: 'GET /api/instagram/insights',
refreshToken: 'POST /api/instagram/refresh-token',
webhook: 'GET /api/instagram/webhook',
health: 'GET /api/instagram/health',
}
});
});
// ─────────────────────────────────────────────
// Helper: Authenticate with Content Hub
// (identical to LinkedIn index.js)
// ─────────────────────────────────────────────
async function getContentHubToken(contentHubBaseUrl) {
try {
// Return cached token if still valid (cache for 15 mins)
const now = Date.now();
if (tokenCache.token && now < tokenCache.expiry) {
console.log('✅ Using cached Content Hub token');
return tokenCache.token;
}
console.log('🔐 Authenticating with Content Hub...');
const response = await axios.post(
`${contentHubBaseUrl}/api/authenticate`,
{
user_name: CONTENT_HUB_USERNAME,
password: CONTENT_HUB_PASSWORD,
},
{ headers: { 'Content-Type': 'application/json' } }
);
const token =
response.data.token ||
response.data.access_token ||
response.data;
if (typeof token !== 'string' || token.trim().length === 0) {
console.error('❌ Token extraction failed:', JSON.stringify(response.data));
return null;
}
// Cache for 15 minutes
tokenCache.token = token;
tokenCache.expiry = now + 15 * 60 * 1000;
console.log('✅ Content Hub token obtained, length:', token.length);
return token;
} catch (err) {
console.error('❌ Content Hub auth failed:', err.response?.status, err.message);
return null;
}
}
// ─────────────────────────────────────────────
// Helper: Get asset details from Content Hub
// Returns { title, imageUrl, videoUrl, imageToken, socialCaption, mediaType }
// ─────────────────────────────────────────────
async function getAssetDetails(assetId, contentHubBaseUrl) {
try {
console.log(`🔍 Fetching asset details for ID: ${assetId}`);
const token = await getContentHubToken(contentHubBaseUrl);
if (!token) {
return { title: null, imageUrl: null, videoUrl: null, imageToken: null, socialCaption: null, mediaType: 'IMAGE' };
}
const response = await axios.get(
`${contentHubBaseUrl}/api/entities/${assetId}`,
{
headers: {
'X-Auth-Token': token,
'Content-Type': 'application/json',
},
}
);
const entity = response.data;
const props = entity?.properties || {};
console.log('✅ Asset fetched successfully');
// ── Get Title ──
const title = props.Title || props.FileName || entity?.identifier || null;
console.log('✅ Title:', title);
// ── Get SocialPostCaption (multilingual — identical to LinkedIn) ──
let socialCaption = null;
const rawCaption = props.SocialPostCaption;
if (rawCaption && typeof rawCaption === 'object') {
socialCaption =
rawCaption['en-US'] ||
rawCaption['(Default)'] ||
Object.values(rawCaption)[0] ||
null;
}
console.log('✅ SocialPostCaption:', socialCaption || '(empty)');
// ── Detect media type from file extension ──
const fileName = (props.FileName || '').toLowerCase();
const videoExtensions = ['mp4', 'mov', 'avi', 'mkv', 'webm'];
const ext = fileName.split('.').pop();
const mediaType = videoExtensions.includes(ext) ? 'VIDEO' : 'IMAGE';
console.log('✅ Detected media type:', mediaType);
// ── Get rendition URL ──
// Priority: downloadOriginal → original → first available
let imageUrl = null;
let videoUrl = null;
const renditions = entity?.renditions;
if (renditions && typeof renditions === 'object') {
const renditionKey = renditions.downloadOriginal
? 'downloadOriginal'
: renditions.original
? 'original'
: Object.keys(renditions)[0];
const downloadHref = renditions[renditionKey]?.[0]?.href || null;
console.log(`✅ Rendition key used: ${renditionKey}`);
console.log(`✅ Rendition selected: ${downloadHref}`);
if (mediaType === 'VIDEO') {
videoUrl = downloadHref;
} else {
imageUrl = downloadHref;
}
}
if (!imageUrl && !videoUrl) {
console.log('⚠️ No suitable rendition found');
}
return { title, imageUrl, videoUrl, imageToken: token, socialCaption, mediaType };
} catch (err) {
console.error('❌ Failed to fetch asset:', err.response?.status, err.message);
return { title: null, imageUrl: null, videoUrl: null, imageToken: null, socialCaption: null, mediaType: 'IMAGE' };
}
}
// ─────────────────────────────────────────────
// Helper: Download + resize image buffer
// Returns resized JPEG buffer ready for streaming
// ─────────────────────────────────────────────
async function downloadAndResize(imageUrl, imageToken) {
console.log('📥 Downloading image from Content Hub...');
const imageResponse = await axios.get(imageUrl, {
responseType: 'arraybuffer',
headers: { 'X-Auth-Token': imageToken },
maxContentLength: 20 * 1024 * 1024,
timeout: 30000,
});
const imageBuffer = Buffer.from(imageResponse.data);
const imageContentType = imageResponse.headers['content-type'] || 'image/jpeg';
console.log('✅ Image downloaded:', imageBuffer.length, 'bytes');
console.log('✅ Content type:', imageContentType);
if (imageBuffer.length === 0) throw new Error('Downloaded image buffer is empty');
if (!imageContentType.startsWith('image/')) throw new Error(`Not an image: ${imageContentType}`);
// Check aspect ratio
const metadata = await sharp(imageBuffer).metadata();
const width = metadata.width || 1080;
const height = metadata.height || 1080;
const ratio = width / height;
console.log(`✅ Original dimensions: ${width}x${height} (ratio: ${ratio.toFixed(3)})`);
// Instagram valid range: 4:5 (0.8) to 1.91:1
const MIN_RATIO = 0.8;
const MAX_RATIO = 1.91;
let resizedBuffer;
if (ratio >= MIN_RATIO && ratio <= MAX_RATIO) {
console.log('✅ Aspect ratio valid — resizing to max 1080px wide');
resizedBuffer = await sharp(imageBuffer)
.resize(1080, null, { fit: 'inside', withoutEnlargement: true })
.jpeg({ quality: 90 })
.toBuffer();
} else {
console.log('⚠️ Aspect ratio out of range — cropping to 1:1 square (1080x1080)');
resizedBuffer = await sharp(imageBuffer)
.resize(1080, 1080, { fit: 'cover', position: 'centre' })
.jpeg({ quality: 90 })
.toBuffer();
}
console.log('✅ Processed image size:', resizedBuffer.length, 'bytes');
return resizedBuffer;
}
// ─────────────────────────────────────────────
// Helper: Wait for Instagram container to be ready
// (mirrors waitForLinkedInAsset in index.js)
// ─────────────────────────────────────────────
async function waitForInstagramContainer(containerId) {
for (let attempt = 1; attempt <= 10; attempt++) {
const statusResponse = await axios.get(
`${INSTAGRAM_GRAPH_URL}/${containerId}`,
{
params: {
fields: 'status_code,status',
access_token: INSTAGRAM_ACCESS_TOKEN,
},
timeout: 10000,
}
);
const statusCode = statusResponse.data?.status_code;
console.log(`⏳ Instagram container status attempt ${attempt}:`, statusCode || 'unknown');
if (statusCode === 'FINISHED' || statusCode === 'READY') return true;
if (statusCode === 'ERROR' || statusCode === 'EXPIRED') {
throw new Error(`Instagram container processing failed: ${statusCode}`);
}
await new Promise(resolve => setTimeout(resolve, 3000));
}
throw new Error('Instagram container did not become ready in time');
}
// ─────────────────────────────────────────────
// IMAGE PROXY ROUTE
// Instagram calls this URL to fetch the image.
// We download from Content Hub, resize with sharp,
// and stream back as a public JPEG — no second cloud needed.
//
// GET /api/instagram/image-proxy/:assetId
// ─────────────────────────────────────────────
app.get('/api/instagram/image-proxy/:assetId', async (req, res) => {
const { assetId } = req.params;
const sourceSystem = req.query.source || CONTENT_HUB_URL;
console.log(`🖼️ Image proxy request for asset: ${assetId}`);
try {
const token = await getContentHubToken(sourceSystem);
if (!token) {
return res.status(500).send('Failed to authenticate with Content Hub');
}
// Fetch entity to get rendition URL
const response = await axios.get(
`${sourceSystem}/api/entities/${assetId}`,
{ headers: { 'X-Auth-Token': token, 'Content-Type': 'application/json' } }
);
const entity = response.data;
const renditions = entity?.renditions;
if (!renditions) {
return res.status(404).send('No renditions found for asset');
}
const renditionKey = renditions.downloadOriginal
? 'downloadOriginal'
: renditions.original
? 'original'
: Object.keys(renditions)[0];
const imageUrl = renditions[renditionKey]?.[0]?.href;
if (!imageUrl) {
return res.status(404).send('No image URL found in renditions');
}
// Download and resize
const resizedBuffer = await downloadAndResize(imageUrl, token);
// Stream back as public JPEG — Instagram fetches this URL directly
res.set('Content-Type', 'image/jpeg');
res.set('Content-Length', resizedBuffer.length);
res.set('Cache-Control', 'public, max-age=3600'); // cache 1hr
res.send(resizedBuffer);
console.log(`✅ Image proxy served for asset ${assetId}: ${resizedBuffer.length} bytes`);
} catch (err) {
console.error('❌ Image proxy error:', err.message);
res.status(500).send('Failed to proxy image');
}
});
// ─────────────────────────────────────────────
// GET — test connection (mirrors LinkedIn GET /linkedin/publish)
// ─────────────────────────────────────────────
app.get('/api/instagram/publish-image', (req, res) => {
res.json({ status: '✅ Instagram publish-image endpoint is ready. Use POST to publish.' });
});
app.get('/api/instagram/publish-video', (req, res) => {
res.json({ status: '✅ Instagram publish-video endpoint is ready. Use POST to publish.' });
});
// ─────────────────────────────────────────────
// PUBLISH IMAGE — mirrors POST /linkedin/publish
// ─────────────────────────────────────────────
app.post('/api/instagram/publish-image', async (req, res) => {
console.log('📢 Incoming Instagram image publish request from Content Hub');
console.log('Body:', JSON.stringify(req.body));
// ── Security check (identical to LinkedIn) ──
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== API_SECRET_KEY) {
console.error('❌ Unauthorized - invalid x-api-key');
return res.status(401).json({ error: 'Unauthorized' });
}
if (!INSTAGRAM_ACCESS_TOKEN) return res.status(500).json({ error: 'INSTAGRAM_ACCESS_TOKEN not configured' });
if (!INSTAGRAM_BUSINESS_ACCOUNT_ID) return res.status(500).json({ error: 'INSTAGRAM_BUSINESS_ACCOUNT_ID not configured' });
// ── Read context from Content Hub (identical pattern to LinkedIn) ──
const context = req.body.context || {};
const saveMsg = req.body.saveEntityMessage || {};
const assetId = req.headers['target_id'] || saveMsg.TargetId;
const sourceSystem = req.headers['source_system'] || CONTENT_HUB_URL;
console.log('✅ Asset ID:', assetId);
console.log('✅ Source System:', sourceSystem);
if (!assetId) {
return res.status(400).json({ error: 'No asset ID provided', hint: 'Send target_id in headers' });
}
// ── Fetch asset details from Content Hub ──
let caption = context.caption || null;
const assetDetails = await getAssetDetails(assetId, sourceSystem);
// Priority: SocialPostCaption > Title (identical to LinkedIn)
if (assetDetails.socialCaption) {
caption = assetDetails.socialCaption;
console.log('✅ Using SocialPostCaption for caption');
} else if (!caption || caption === '{Title}') {
caption = assetDetails.title;
console.log('✅ Using Title for caption');
}
if (!caption) {
caption = 'New content published from Sitecore Content Hub';
}
console.log('✅ Final Caption:', caption);
if (!assetDetails.imageUrl) {
return res.status(400).json({
error: 'No image URL found for asset',
assetId,
hint: 'Ensure the asset has a downloadOriginal rendition and is an image file type',
});
}
try {
// ── Build proxy URL — Instagram fetches this to get the image ──
// Your Vercel function IS the public server, so we proxy through it.
const vercelBaseUrl = `https://${req.headers.host}`;
const proxyUrl = `${vercelBaseUrl}/api/instagram/image-proxy/${assetId}?source=${encodeURIComponent(sourceSystem)}`;
console.log('✅ Proxy URL for Instagram:', proxyUrl);
// ── Step 1: Create Instagram media container using proxy URL ──
console.log('📤 Creating Instagram media container...');
const containerResponse = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media`,
{
image_url: proxyUrl, // ✅ Public proxy URL — no second cloud
caption: caption,
media_type: 'IMAGE',
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const containerId = containerResponse.data.id;
console.log('✅ Container created:', containerId);
// ── Step 2: Wait for Instagram to process the image ──
console.log('⏳ Waiting for Instagram image container to be ready...');
await waitForInstagramContainer(containerId);
// ── Step 3: Publish the container ──
const publishResponse = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media_publish`,
{
creation_id: containerId,
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const postId = publishResponse.data.id;
console.log('✅ Successfully published to Instagram!');
console.log('✅ Post ID:', postId);
res.json({
success: true,
postId,
caption,
instagramUrl: `https://instagram.com/p/${postId}`,
message: 'Successfully published image to Instagram',
timestamp: new Date().toISOString(),
});
} catch (err) {
console.error('❌ Instagram image publish failed:', err.response?.data || err.message);
res.status(500).json({
error: 'Failed to publish image to Instagram',
details: err.response?.data?.error || err.message,
code: err.response?.status,
});
}
});
// ─────────────────────────────────────────────
// PUBLISH VIDEO — same pattern as publish-image
// ─────────────────────────────────────────────
app.post('/api/instagram/publish-video', async (req, res) => {
console.log('📢 Incoming Instagram video publish request from Content Hub');
console.log('Body:', JSON.stringify(req.body));
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== API_SECRET_KEY) {
console.error('❌ Unauthorized - invalid x-api-key');
return res.status(401).json({ error: 'Unauthorized' });
}
if (!INSTAGRAM_ACCESS_TOKEN) return res.status(500).json({ error: 'INSTAGRAM_ACCESS_TOKEN not configured' });
if (!INSTAGRAM_BUSINESS_ACCOUNT_ID) return res.status(500).json({ error: 'INSTAGRAM_BUSINESS_ACCOUNT_ID not configured' });
const context = req.body.context || {};
const saveMsg = req.body.saveEntityMessage || {};
const assetId = req.headers['target_id'] || saveMsg.TargetId;
const sourceSystem = req.headers['source_system'] || CONTENT_HUB_URL;
console.log('✅ Asset ID:', assetId);
console.log('✅ Source System:', sourceSystem);
let caption = context.caption || null;
const assetDetails = await getAssetDetails(assetId, sourceSystem);
if (assetDetails.socialCaption) {
caption = assetDetails.socialCaption;
console.log('✅ Using SocialPostCaption for caption');
} else if (!caption || caption === '{Title}') {
caption = assetDetails.title;
console.log('✅ Using Title for caption');
}
if (!caption) caption = 'New content published from Sitecore Content Hub';
console.log('✅ Final Caption:', caption);
console.log('✅ Video URL:', assetDetails.videoUrl || 'none');
if (!assetDetails.videoUrl) {
return res.status(400).json({
error: 'No video URL found for asset',
assetId,
hint: 'Ensure the asset has a downloadOriginal rendition and is a video file type (mp4, mov, etc.)',
});
}
try {
console.log('📤 Creating Instagram video media container...');
const containerResponse = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media`,
{
video_url: assetDetails.videoUrl,
caption: caption,
media_type: 'REELS',
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const containerId = containerResponse.data.id;
console.log('✅ Video container created:', containerId);
console.log('⏳ Waiting for Instagram to process video...');
await waitForInstagramContainer(containerId);
const publishResponse = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media_publish`,
{
creation_id: containerId,
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const postId = publishResponse.data.id;
console.log('✅ Successfully published video to Instagram!');
console.log('✅ Post ID:', postId);
res.json({
success: true,
postId,
caption,
instagramUrl: `https://instagram.com/p/${postId}`,
message: 'Successfully published video to Instagram',
timestamp: new Date().toISOString(),
});
} catch (err) {
console.error('❌ Instagram video publish failed:', err.response?.data || err.message);
res.status(500).json({
error: 'Failed to publish video to Instagram',
details: err.response?.data?.error || err.message,
code: err.response?.status,
});
}
});
// ─────────────────────────────────────────────
// INSIGHTS
// ─────────────────────────────────────────────
app.get('/api/instagram/insights', async (req, res) => {
try {
const { metric = 'impressions,reach,profile_views' } = req.query;
const insightsResponse = await axios.get(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/insights`,
{ params: { metric, period: 'day', access_token: INSTAGRAM_ACCESS_TOKEN } }
);
res.json({ success: true, insights: insightsResponse.data.data, timestamp: new Date().toISOString() });
} catch (err) {
console.error('❌ Instagram insights error:', err.response?.data || err.message);
res.status(500).json({ error: 'Failed to fetch Instagram insights', details: err.response?.data?.error || err.message });
}
});
// ─────────────────────────────────────────────
// REFRESH ACCESS TOKEN
// ─────────────────────────────────────────────
app.post('/api/instagram/refresh-token', async (req, res) => {
try {
const { userAccessToken } = req.body;
const refreshResponse = await axios.get(
`${INSTAGRAM_GRAPH_URL}/access_token`,
{
params: {
grant_type: 'ig_exchange_token',
client_secret: INSTAGRAM_APP_SECRET,
access_token: userAccessToken || INSTAGRAM_ACCESS_TOKEN,
},
}
);
console.log('✅ Instagram token refreshed successfully');
res.json({
success: true,
newAccessToken: refreshResponse.data.access_token,
expiresIn: refreshResponse.data.expires_in,
message: 'Token refreshed successfully. Valid for 60 days.',
});
} catch (err) {
console.error('❌ Token refresh error:', err.response?.data || err.message);
res.status(500).json({ error: 'Failed to refresh access token', details: err.response?.data?.error || err.message });
}
});
// ─────────────────────────────────────────────
// WEBHOOK VERIFICATION
// ─────────────────────────────────────────────
app.get('/api/instagram/webhook', (req, res) => {
const verifyToken = process.env.INSTAGRAM_WEBHOOK_VERIFY_TOKEN || 'sitecore_content_hub_webhook';
const token = req.query['hub.verify_token'];
const challenge = req.query['hub.challenge'];
if (token === verifyToken) {
console.log('✅ Instagram webhook verified');
res.send(challenge);
} else {
console.error('❌ Instagram webhook verification failed');
res.status(403).send('Verification failed');
}
});
// ─────────────────────────────────────────────
// WEBHOOK HANDLER
// ─────────────────────────────────────────────
app.post('/api/instagram/webhook', (req, res) => {
const { entry } = req.body;
if (entry) {
entry.forEach((item) => {
const { messaging } = item;
if (messaging) messaging.forEach((event) => console.log('Instagram webhook event:', event));
});
}
res.status(200).send('Event received');
});
// ─────────────────────────────────────────────
// HEALTH CHECK
// ─────────────────────────────────────────────
app.get('/api/instagram/health', (req, res) => {
console.log('✅ Instagram health check');
res.json({
status: '✅ healthy',
service: 'Instagram Integration Middleware',
timestamp: new Date().toISOString(),
accountId: INSTAGRAM_BUSINESS_ACCOUNT_ID,
});
});
// ─────────────────────────────────────────────
// ERROR HANDLING
// ─────────────────────────────────────────────
app.use((err, req, res, next) => {
console.error('❌ Unhandled error:', err);
res.status(500).json({ error: 'Internal server error', message: err.message });
});
// ─────────────────────────────────────────────
// PUBLISH CAROUSEL — multiple assets from campaign
// POST /api/instagram/publish-campaign
// ─────────────────────────────────────────────
app.post('/api/instagram/publish-campaign', async (req, res) => {
console.log('📢 Incoming Instagram campaign publish request');
console.log('Body:', JSON.stringify(req.body));
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== API_SECRET_KEY) {
return res.status(401).json({ error: 'Unauthorized' });
}
if (!INSTAGRAM_ACCESS_TOKEN) return res.status(500).json({ error: 'INSTAGRAM_ACCESS_TOKEN not configured' });
if (!INSTAGRAM_BUSINESS_ACCOUNT_ID) return res.status(500).json({ error: 'INSTAGRAM_BUSINESS_ACCOUNT_ID not configured' });
const saveMsg = req.body.saveEntityMessage || {};
const campaignId = req.headers['target_id'] || saveMsg.TargetId;
const sourceSystem = req.headers['source_system'] || CONTENT_HUB_URL;
console.log('✅ Campaign ID:', campaignId);
if (!campaignId) {
return res.status(400).json({ error: 'No campaign ID provided' });
}
try {
// Step 1: Auth with Content Hub
const token = await getContentHubToken(sourceSystem);
if (!token) return res.status(500).json({ error: 'Content Hub auth failed' });
// Step 2: Fetch asset IDs from campaign selection pool
const selectionResponse = await axios.get(
`${sourceSystem}/api/selection/SelectionPool.ContentCampaignDetail/`,
{
params: {
ignorePermissions: false,
definitionNames: 'M.Content,M.Asset,M.Deliverable',
subPoolId: campaignId
},
headers: { 'X-Auth-Token': token, 'Content-Type': 'application/json' }
}
);
const assetIds = selectionResponse.data?.['M.Asset']?.items || [];
console.log('✅ Found asset IDs from selection pool:', assetIds);
if (assetIds.length === 0) {
return res.status(400).json({ error: 'No assets found in campaign selection pool' });
}
// Step 3: Get campaign title for caption
const campaignResponse = await axios.get(
`${sourceSystem}/api/entities/${campaignId}`,
{ headers: { 'X-Auth-Token': token, 'Content-Type': 'application/json' } }
);
const rawCaption = campaignResponse.data?.properties?.SocialPostCaption;
let caption = 'Campaign post from Sitecore Content Hub';
if (rawCaption && typeof rawCaption === 'object') {
caption =
rawCaption['en-US'] ||
rawCaption['(Default)'] ||
Object.values(rawCaption)[0] ||
caption;
} else if (typeof rawCaption === 'string') {
caption = rawCaption;
}
// Step 4: Build proxy URLs for each asset (max 10 for Instagram)
const vercelBaseUrl = `https://${req.headers.host}`;
const assetSlice = assetIds.slice(0, 10);
// Step 5: Create individual media containers for each asset
const childContainerIds = [];
for (const assetId of assetSlice) {
const proxyUrl = `${vercelBaseUrl}/api/instagram/image-proxy/${assetId}?source=${encodeURIComponent(sourceSystem)}`;
console.log(`📤 Creating container for asset ${assetId}`);
const containerRes = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media`,
{
image_url: proxyUrl,
is_carousel_item: true,
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
childContainerIds.push(containerRes.data.id);
console.log(`✅ Container created: ${containerRes.data.id}`);
}
// Step 6: Create carousel container
console.log('📤 Creating carousel container...');
const carouselRes = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media`,
{
media_type: 'CAROUSEL',
caption: caption,
children: childContainerIds.join(','),
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const carouselContainerId = carouselRes.data.id;
console.log('✅ Carousel container created:', carouselContainerId);
// Step 7: Wait for carousel to be ready
await waitForInstagramContainer(carouselContainerId);
// Step 8: Publish
const publishRes = await axios.post(
`${INSTAGRAM_GRAPH_URL}/${INSTAGRAM_BUSINESS_ACCOUNT_ID}/media_publish`,
{
creation_id: carouselContainerId,
access_token: INSTAGRAM_ACCESS_TOKEN,
}
);
const postId = publishRes.data.id;
console.log('✅ Instagram carousel published! Post ID:', postId);
res.json({
success: true,
postId,
caption,
assetCount: childContainerIds.length,
message: 'Successfully published carousel to Instagram',
timestamp: new Date().toISOString(),
});
} catch (err) {
console.error('❌ Instagram carousel publish failed:', err.response?.data || err.message);
res.status(500).json({
error: 'Failed to publish carousel to Instagram',
details: err.response?.data?.error || err.message,
});
}
});
export default app;