@jikey/fcazero
Version:
Facebook Messenger bot, and is one of the most advanced next-generation Facebook Chat API (FCA)
2 lines (1 loc) • 3.58 kB
JavaScript
import{Axios as m}from"./axios.js";import{Constants as u}from"./constants.js";class d{static formatCookie(e,t){return e[0]+"="+e[1]+"; Path="+e[3]+"; Domain="+t+".com"}static parseAndCheckLogin(e,t,p=0){const n=o=>new Promise(function(c,s){try{c(o())}catch(r){s(r)}});return function(o){function c(){if(o.statusCode>=500&&o.statusCode<600){if(p>=5){const a=new Error("Request retry failed. Check the `res` and `statusCode` property on this error.");throw a.statusCode=o.statusCode,a.res=o.body,a.error="Request retry failed. Check the `res` and `statusCode` property on this error.",a}p++;const r=Math.floor(Math.random()*5e3),i=o.request.uri.protocol+"//"+o.request.uri.hostname+o.request.uri.pathname;return typeof o.request.headers["content-type"]=="string"&&o.request.headers["content-type"].split(";")[0]==="multipart/form-data"?u.delay(r).then(()=>t.postFormData(i,e.jar,o.request.formData,o.request.form,e.globalOptions,e)).then(d.parseAndCheckLogin(e,t,p)):u.delay(r).then(()=>t.post(i,e.jar,o.request.form,e.globalOptions,e)).then(d.parseAndCheckLogin(e,t,p))}if(o.statusCode===404)return;if(o.statusCode!==200)throw new Error("parseAndCheckLogin got status code: "+o.statusCode+". Bailing out of trying to parse response.");let s=null;if(typeof o.body=="object"&&o.body!==null)s=o.body;else if(typeof o.body=="string")try{const r=u.makeParsable(o.body);s=JSON.parse(Array.isArray(r)?r.join(""):r)}catch(r){const i=new Error("JSON.parse error. Check the `detail` property on this error.");throw i.error="JSON.parse error. Check the `detail` property on this error.",i.detail=r,i.res=o.body,i}else throw new Error("Unknown response body type: "+typeof o.body);if(s&&s.redirect&&o.request.method==="GET")return t.get(s.redirect,e.jar).then(d.parseAndCheckLogin(e,t));if(s&&s.jsmods&&s.jsmods.require&&Array.isArray(s.jsmods.require[0])&&s.jsmods.require[0][0]==="Cookie"){s.jsmods.require[0][3][0]=s.jsmods.require[0][3][0].replace("_js_","");const r=s.jsmods.require[0][3];e.jar.setCookie(d.formatCookie(r,"facebook"),"https://www.facebook.com"),e.jar.setCookie(d.formatCookie(r,"messenger"),"https://www.messenger.com")}if(s&&s.jsmods&&Array.isArray(s.jsmods.require)){const r=s.jsmods.require;for(const i in r)if(r[i]&&r[i][0]==="DTSG"&&r[i][1]==="setToken"&&(e.fb_dtsg=r[i][3][0],e.ttstamp="2",e.fb_dtsg))for(let a=0;a<e.fb_dtsg.length;a++)e.ttstamp+=e.fb_dtsg.charCodeAt(a)}if(s&&s.error===1357001){const r=new Error("Facebook blocked the login");throw r.error="Not logged in.",r}return s??void 0}return n(c)}}static async checkLiveCookie(e,t){if((await t.get("https://m.facebook.com/me",e.jar)).body.indexOf(e.userID)===-1){const n=new Error("Not logged in.");throw n.message="Not logged in.",n.error="Not logged in.",n}return!0}static saveCookies(e){return function(t){return(Array.isArray(t.headers["set-cookie"])?t.headers["set-cookie"]:typeof t.headers["set-cookie"]=="string"?[t.headers["set-cookie"]]:[]).forEach(function(n){n.indexOf(".facebook.com")>-1&&e.setCookie(n,"https://www.facebook.com");const o=n.replace(/domain=\.facebook\.com/,"domain=.messenger.com");e.setCookie(o,"https://www.messenger.com")}),t}}static getAccessFromBusiness(e,t){return async function(p){const n=p?p.body:null;try{const o=await m.get("https://business.facebook.com/content_management",e,void 0,t,void 0,{noRef:!0}),c=/"accessToken":"([^.]+)","clientID":/g.exec(o.body),s=c?c[1]??null:null;return[n,s]}catch{return[n,null]}}}static getAppState(e){return e.getCookiesSync("https://www.facebook.com").concat(e.getCookiesSync("https://www.messenger.com"))}}export{d as Clients};