UNPKG

@jikey/fcazero

Version:

Facebook Messenger bot, and is one of the most advanced next-generation Facebook Chat API (FCA)

2 lines (1 loc) 2.03 kB
"use strict";var A=Object.create;var i=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var C=Object.getOwnPropertyNames;var x=Object.getPrototypeOf,T=Object.prototype.hasOwnProperty;var j=(r,t)=>{for(var e in t)i(r,e,{get:t[e],enumerable:!0})},d=(r,t,e,s)=>{if(t&&typeof t=="object"||typeof t=="function")for(let a of C(t))!T.call(r,a)&&a!==e&&i(r,a,{get:()=>t[a],enumerable:!(s=g(t,a))||s.enumerable});return r};var I=(r,t,e)=>(e=r!=null?A(x(r)):{},d(t||!r||!r.__esModule?i(e,"default",{value:r,enumerable:!0}):e,r)),U=r=>d(i({},"__esModule",{value:!0}),r);var F={};j(F,{default:()=>y});module.exports=U(F);var l=require("../utils/clients.js"),u=require("../utils/constants.js"),f=require("../utils/formatters.js"),h=I(require("../utils/logger.js"),1);class y{constructor(t,e,s){this.defaultFuncs=t;this.ctx=s}call(t,e){const s=(a,p)=>{p=p||function(){};const n=[];for(let o=0;o<a.length;o++){if(!f.Formatters.isReadableStream(a[o]))throw{error:"Attachment should be a readable stream and not "+u.Constants.getType(a[o])+"."};const c={upload_1024:a[o],voice_clip:"true"};n.push(this.defaultFuncs.postFormData("https://upload.facebook.com/ajax/mercury/upload.php",this.ctx.jar,c).then(l.Clients.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(function(m){if(m&&m.error)throw m;return m&&m.payload.metadata[0]}))}Promise.all(n).then(function(o){p(null,o)}).catch(function(o){return h.default.error({err:o},"uploadAttachment"),p(o)})};return new Promise(async(a,p)=>{try{if(!t.attachments&&!f.Formatters.isReadableStream(t.attachments)&&u.Constants.getType(t.attachments)!=="Array"&&u.Constants.getType(t.attachments)==="Array"&&!t.attachments.length)throw{error:"Please pass an attachment or an array of attachments."};return u.Constants.getType(t.attachments)!=="Array"&&(t.attachments=[t.attachments]),s(t.attachments,(n,o)=>n?(e&&typeof e=="function"&&e(n),p(n)):(e&&typeof e=="function"&&e(null,o),a(o)))}catch(n){return h.default.error({error:n},"uploadAttachment"),e&&typeof e=="function"&&e(n),p(n)}})}}