Last active
December 8, 2022 13:47
-
-
Save mrgulshanyadav/543f75f8ca2542678aa821cfbc24411c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var carouselUploadRetries = 0; | |
var videoUploadRetries = 0; | |
var imageUploadRetries = 0; | |
var publishRetries = 0; | |
const maxUploadRetries = 2; | |
const maxPublishRetries = 2; | |
var caption = bundle.inputData.caption; | |
if(caption==='undefined' || caption===null || caption===''){ | |
caption = ''; | |
}else{ | |
caption = bundle.inputData.caption + ''; | |
} | |
// return z.request({ | |
// url: 'https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id='+bundle.inputData.client_id+'&client_secret='+bundle.inputData.client_secret+'&fb_exchange_token='+bundle.inputData.access_token, | |
// method: 'POST', | |
// headers: { | |
// 'Content-Type': 'application/json', | |
// 'Accept': 'application/json', | |
// }, | |
// }).then((oauthResponse) => { | |
// oauthResponse.throwForStatus(); | |
// const oauthResult = oauthResponse.json; | |
// const accessToken = oauthResult.access_token; | |
const accessToken = bundle.authData.access_token; | |
return z.request({ | |
url: 'https://graph.facebook.com/v15.0/'+bundle.inputData.fb_page_id+'?fields=instagram_business_account&access_token='+accessToken, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json', | |
'Accept': 'application/json', | |
}, | |
}).then((fbPageAPIResponse) => { | |
fbPageAPIResponse.throwForStatus(); | |
const fbPageAPIResult = fbPageAPIResponse.json; | |
const iGAccountId = fbPageAPIResult.instagram_business_account.id; | |
var str = bundle.inputData.media_urls.toString(); | |
var mediaURLs = str.split(','); | |
if(mediaURLs.length==1){ | |
if(str===""){ | |
return {"message": "Enter atleast one media URL"}; | |
}else{ | |
if((mediaURLs[0].indexOf('.mp4') > -1) || (mediaURLs[0].indexOf('.mov') > -1)){ | |
// video found | |
return uploadVideo(iGAccountId, accessToken, mediaURLs[0], caption); | |
} | |
if((mediaURLs[0].indexOf('.jpg') > -1) || (mediaURLs[0].indexOf('.jpeg') > -1) || (mediaURL.indexOf('.png') > -1) || (mediaURL.indexOf('.jpeg') > -1)){ | |
// image found | |
return uploadImage(iGAccountId, accessToken, mediaURLs[0], caption); | |
} | |
} | |
}else{ | |
var uploadingURLs = []; | |
for(var i = 0; i < mediaURLs.length; i++) { | |
mediaURLs[i] = mediaURLs[i].replace(/^\s*/, "").replace(/\s*$/, ""); | |
const mediaURL = mediaURLs[i]; | |
if((mediaURL.indexOf('.mp4') > -1) || (mediaURL.indexOf('.mov') > -1)){ | |
// video found | |
uploadingURLs.push('https://graph.facebook.com/v15.0/'+iGAccountId+'/media?access_token='+accessToken+'&is_carousel_item=true&media_type=VIDEO&video_url='+mediaURL); | |
} | |
if((mediaURL.indexOf('.jpg') > -1) || (mediaURL.indexOf('.jpeg') > -1) || (mediaURL.indexOf('.png') > -1) || (mediaURL.indexOf('.jpeg') > -1)){ | |
// image found | |
uploadingURLs.push('https://graph.facebook.com/v15.0/'+iGAccountId+'/media?access_token='+accessToken+'&is_carousel_item=true&image_url='+mediaURL); | |
} | |
} | |
return uploadAll(uploadingURLs).then((uploadResults)=> { | |
var res = {"data": uploadResults}; | |
// return uploadResults; | |
var idsArray = []; | |
for(var i = 0; i < res.data.length; i++) { | |
const id = res.data[i].id; | |
idsArray.push(id); | |
} | |
var children = idsArray.toString(); | |
// return {"ids": children}; | |
// return {"accessToken": accessToken}; | |
const publishMediaResponse = uploadCarousel(iGAccountId, accessToken, idsArray.toString()); | |
return publishMediaResponse; | |
}); | |
} | |
}); | |
// }); | |
function uploadVideo(iGAccountId, accessToken, url, caption){ | |
return z.request({ | |
url: 'https://graph.facebook.com/v15.0/'+iGAccountId+'/media?access_token='+accessToken+'&media_type=VIDEO&video_url='+url+'&caption='+caption, | |
method: 'POST', | |
}).then((uploadResponse) => { | |
uploadResponse.throwForStatus(); | |
const uploadResult = uploadResponse.json; | |
const creationId = uploadResult.id; | |
return publishMedia(iGAccountId, accessToken, creationId); | |
})//; | |
.catch((e)=>{ | |
if(videoUploadRetries<maxUploadRetries){ | |
return uploadVideo(iGAccountId, accessToken, url, caption); | |
}else{ | |
return {"uploadVideoError":e}; | |
} | |
videoUploadRetries = videoUploadRetries+1; | |
}); | |
} | |
function uploadImage(iGAccountId, accessToken, url, caption){ | |
return z.request({ | |
url: 'https://graph.facebook.com/v15.0/'+iGAccountId+'/media?access_token='+accessToken+'&image_url='+url+'&caption='+caption, | |
method: 'POST', | |
}).then((uploadResponse) => { | |
uploadResponse.throwForStatus(); | |
const uploadResult = uploadResponse.json; | |
const creationId = uploadResult.id; | |
return publishMedia(iGAccountId, accessToken, creationId); | |
})//; | |
.catch((e)=>{ | |
if(imageUploadRetries<maxUploadRetries){ | |
return uploadImage(iGAccountId, accessToken, url, caption); | |
}else{ | |
return {"uploadImageError":e}; | |
} | |
imageUploadRetries = imageUploadRetries+1; | |
}); | |
} | |
function uploadCarousel(iGAccountId, accessToken, children){ | |
return z.request({ | |
url: 'https://graph.facebook.com/v15.0/'+iGAccountId+'/media?children='+children+'&media_type=CAROUSEL&caption='+caption+'&access_token='+accessToken, | |
method: 'POST', | |
}).then((uploadResponse) => { | |
uploadResponse.throwForStatus(); | |
const uploadResult = uploadResponse.json; | |
const creationId = uploadResult.id; | |
return publishMedia(iGAccountId, accessToken, creationId); | |
})//; | |
.catch((e)=>{ | |
if(carouselUploadRetries<maxUploadRetries){ | |
return uploadCarousel(iGAccountId, accessToken, children); | |
}else{ | |
return {"uploadCarouselError":e}; | |
} | |
carouselUploadRetries = carouselUploadRetries+1; | |
}); | |
} | |
function publishMedia(iGAccountId, accessToken, creationId){ | |
return z.request({ | |
url: 'https://graph.facebook.com/v15.0/'+iGAccountId+'/media_publish?access_token='+accessToken+'&creation_id='+creationId, | |
method: 'POST', | |
}).then((publishResponse) => { | |
publishResponse.throwForStatus(); | |
const publishResult = publishResponse.json; | |
if(publishResult.id!==''){ | |
return {"message": "Post Uploaded Successfully"}; | |
}else{ | |
return publishResult; | |
} | |
})//; | |
.catch((e)=>{ | |
if(publishRetries<maxPublishRetries){ | |
return publishMedia(iGAccountId, accessToken, creationId); | |
}else{ | |
return {"publishMediaError":e}; | |
} | |
publishRetries = publishRetries+1; | |
}); | |
} | |
function uploadAll(urls){ | |
return new Promise.all( | |
urls.map(url => { | |
return z.request({ | |
url: url, | |
method: 'POST', | |
}).then((uploadResponse) => { | |
uploadResponse.throwForStatus(); | |
const uploadResult = uploadResponse.json; | |
return uploadResult; | |
}); | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment