@jikey/fcazero
Version:
Facebook Messenger bot, and is one of the most advanced next-generation Facebook Chat API (FCA)
2 lines (1 loc) • 4.08 kB
JavaScript
;var y=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var h=Object.getOwnPropertyNames;var g=Object.prototype.hasOwnProperty;var k=(c,e)=>{for(var r in e)y(c,r,{get:e[r],enumerable:!0})},C=(c,e,r,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let n of h(e))!g.call(c,n)&&n!==r&&y(c,n,{get:()=>e[n],enumerable:!(i=l(e,n))||i.enumerable});return c};var b=c=>C(y({},"__esModule",{value:!0}),c);var A={};k(A,{Clients:()=>d});module.exports=b(A);var f=require("./axios.js"),m=require("./constants.js");class d{static formatCookie(e,r){return e[0]+"="+e[1]+"; Path="+e[3]+"; Domain="+r+".com"}static parseAndCheckLogin(e,r,i=0){const n=o=>new Promise(function(u,s){try{u(o())}catch(t){s(t)}});return function(o){function u(){if(o.statusCode>=500&&o.statusCode<600){if(i>=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}i++;const t=Math.floor(Math.random()*5e3),p=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"?m.Constants.delay(t).then(()=>r.postFormData(p,e.jar,o.request.formData,o.request.form,e.globalOptions,e)).then(d.parseAndCheckLogin(e,r,i)):m.Constants.delay(t).then(()=>r.post(p,e.jar,o.request.form,e.globalOptions,e)).then(d.parseAndCheckLogin(e,r,i))}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 t=m.Constants.makeParsable(o.body);s=JSON.parse(Array.isArray(t)?t.join(""):t)}catch(t){const p=new Error("JSON.parse error. Check the `detail` property on this error.");throw p.error="JSON.parse error. Check the `detail` property on this error.",p.detail=t,p.res=o.body,p}else throw new Error("Unknown response body type: "+typeof o.body);if(s&&s.redirect&&o.request.method==="GET")return r.get(s.redirect,e.jar).then(d.parseAndCheckLogin(e,r));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 t=s.jsmods.require[0][3];e.jar.setCookie(d.formatCookie(t,"facebook"),"https://www.facebook.com"),e.jar.setCookie(d.formatCookie(t,"messenger"),"https://www.messenger.com")}if(s&&s.jsmods&&Array.isArray(s.jsmods.require)){const t=s.jsmods.require;for(const p in t)if(t[p]&&t[p][0]==="DTSG"&&t[p][1]==="setToken"&&(e.fb_dtsg=t[p][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 t=new Error("Facebook blocked the login");throw t.error="Not logged in.",t}return s??void 0}return n(u)}}static async checkLiveCookie(e,r){if((await r.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(r){return(Array.isArray(r.headers["set-cookie"])?r.headers["set-cookie"]:typeof r.headers["set-cookie"]=="string"?[r.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")}),r}}static getAccessFromBusiness(e,r){return async function(i){const n=i?i.body:null;try{const o=await f.Axios.get("https://business.facebook.com/content_management",e,void 0,r,void 0,{noRef:!0}),u=/"accessToken":"([^.]+)","clientID":/g.exec(o.body),s=u?u[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"))}}0&&(module.exports={Clients});