UNPKG

@jikey/fcazero

Version:

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

2 lines (1 loc) 6.83 kB
import{Clients as _}from"../utils/clients.js";import{Constants as d}from"../utils/constants.js";import{Formatters as M}from"../utils/formatters.js";import f from"../utils/logger.js";const y=class y{constructor(a,s,h){this.defaultFuncs=a;this.ctx=h}static removeSpecialChar(a){if(typeof a!="string")return a;const s=Buffer.from(a,"utf8");let h=Buffer.alloc(0);for(let c=0;c<s.length;c++)s[c]===239&&s[c+1]===184&&s[c+2]===143?c+=2:h=Buffer.concat([h,s.slice(c,c+1)]);return h.toString("utf8")}call(a,s){const h=(e,t)=>{const o=[];for(let r=0;r<e.length;r++){if(!M.isReadableStream(e[r]))throw{error:"Attachment should be a readable stream and not "+d.getType(e[r])+"."};const i={upload_1024:e[r],voice_clip:"true"};o.push(this.defaultFuncs.postFormData("https://upload.facebook.com/ajax/mercury/upload.php",this.ctx.jar,i,{}).then(_.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(function(n){if(n&&n.error)throw n;return n&&n.payload.metadata[0]}))}Promise.all(o).then(function(r){t(null,r)}).catch(function(r){return f.error({error:r},"Failed to upload attachment"),t(r)})},m=(e,t)=>{const o={image_height:960,image_width:960,uri:e};this.defaultFuncs.post("https://www.facebook.com/message_share_attachment/fromURI/",this.ctx.jar,o).then(_.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(function(r){if(r&&r.error)return t(r);if(r&&!r.payload)return t({error:"Invalid url"});t(null,r&&r.payload.share_data.share_params)}).catch(function(r){return f.error({error:r},"Failed to get url"),t(r)})},c=(e,t,o,r,i)=>{if(d.getType(t)==="Array"){for(let n=0;n<t.length;n++)e["specific_to_list["+n+"]"]="fbid:"+t[n];e["specific_to_list["+t.length+"]"]="fbid:"+this.ctx.userID,e.client_thread_id="root:"+r,f.info(`sendMessage: Sending message to multiple users: ${t}`)}else o?e.thread_fbid=t:(e["specific_to_list[0]"]="fbid:"+t,e["specific_to_list[1]"]="fbid:"+this.ctx.userID,e.other_user_fbid=t);this.ctx.globalOptions.pageID&&(e.author="fbid:"+this.ctx.globalOptions.pageID,e["specific_to_list[1]"]="fbid:"+this.ctx.globalOptions.pageID,e["creator_info[creatorID]"]=this.ctx.userID,e["creator_info[creatorType]"]="direct_admin",e["creator_info[labelType]"]="sent_message",e["creator_info[pageID]"]=this.ctx.globalOptions.pageID,e.request_user_id=this.ctx.globalOptions.pageID,e["creator_info[profileURI]"]="https://www.facebook.com/profile.php?id="+this.ctx.userID),this.defaultFuncs.post("https://www.facebook.com/messaging/send/",this.ctx.jar,e).then(_.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(n=>{if(!n)return i({error:"Send message failed."});if(n&&n.error)return n.error===1545012?f.warn(`sendMessage: Got error 1545012. This might mean that you're not part of the conversation ${t}`):f.error({error:n},"Failed to send message"),i(n);const l=n&&n.payload.actions.reduce(function(u,p){return{threadID:p.thread_fbid,messageID:p.message_id,timestamp:p.timestamp}||u},null);return i(null,l)}).catch(n=>(f.error("sendMessage",n),d.getType(n)=="Object"&&n.error==="Not logged in."&&(this.ctx.loggedIn=!1),i(n)))},b=(e,t,o,r,i)=>{if(d.getType(t)==="Array")c(e,t,!1,o,r);else{if(d.getType(i)!="Boolean")throw{error:"isGroup must be a boolean"};c(e,t,i,o,r)}},I=(e,t,o,r)=>{e.url?(t["shareable_attachment[share_type]"]="100",m(e.url,function(i,n){if(i)return o(i);t["shareable_attachment[share_params]"]=n,r()})):r()},x=(e,t,o,r)=>{if(e.location){if(e.location.latitude==null||e.location.longitude==null)return o({error:"location property needs both latitude and longitude"});t["location_attachment[coordinates][latitude]"]=e.location.latitude,t["location_attachment[coordinates][longitude]"]=e.location.longitude,t["location_attachment[is_current_location]"]=!!e.location.current}r()},w=(e,t,o,r)=>{e.sticker&&(t.sticker_id=e.sticker),r()},S=(e,t,o,r)=>{if(e.emojiSize!=null&&e.emoji==null)return o({error:"emoji property is empty"});if(e.emoji){if(e.emojiSize==null&&(e.emojiSize="medium"),e.emojiSize!="small"&&e.emojiSize!="medium"&&e.emojiSize!="large")return o({error:"emojiSize property is invalid"});if(t.body!=null&&t.body!="")return o({error:"body is not empty"});t.body=e.emoji,t["tags[0]"]="hot_emoji_size:"+e.emojiSize}r()},j=(e,t,o,r)=>{e.attachment?(t.image_ids=[],t.gif_ids=[],t.file_ids=[],t.video_ids=[],t.audio_ids=[],d.getType(e.attachment)!=="Array"&&(e.attachment=[e.attachment]),h(e.attachment,function(i,n){if(i)return o(i);n.forEach(function(l){const p=Object.keys(l)[0];t[""+p+"s"].push(l[p])}),r()})):r()},v=(e,t,o,r)=>{if(e.mentions)for(let i=0;i<e.mentions.length;i++){const n=e.mentions[i],l=n&&n.tag;if(typeof l!="string")return o({error:"Mention tags must be strings."});const u=e.body&&e.body.indexOf(l,n&&n.fromIndex||0);u&&u<0&&f.warn(`handleMention: SMention for '${l}' not found in message string.`),n&&n.id==null&&f.warn("handleMention: Mention id should be non-null.");const p=n&&n.id||0;t["profile_xmd["+i+"][offset]"]=u,t["profile_xmd["+i+"][length]"]=l.length,t["profile_xmd["+i+"][id]"]=p,t["profile_xmd["+i+"][type]"]="p"}r()};return new Promise((e,t)=>{try{if(!a.threadID)throw new Error("please pass a threadID as a second argument.");if(d.getType(a.threadID)!=="Number"&&d.getType(a.threadID)!=="String"&&d.getType(a.threadID)!=="Array")throw new Error("threadID must be a number, a string, or an array of them");d.getType(a.body)==="String"&&(a.body=y.removeSpecialChar(a.body??""));const o=Object.keys(a).filter(l=>!y.allowedProperties.hasOwnProperty(l));if(o.length>0)throw{error:`Dissallowed props: \`${o.join(", ")}\``};const r=d.generateOfflineThreadingID();let i={client:"mercury",action_type:"ma-type:user-generated-message",author:"fbid:"+this.ctx.userID,timestamp:Date.now(),timestamp_absolute:"Today",timestamp_relative:d.generateTimestampRelative(),timestamp_time_passed:"0",is_unread:!1,is_cleared:!1,is_forward:!1,is_filtered_content:!1,is_filtered_content_bh:!1,is_filtered_content_account:!1,is_filtered_content_quasar:!1,is_filtered_content_invalid_app:!1,is_spoof_warning:!1,source:"source:chat:web","source_tags[0]":"source:chat",body:a.body?a.body.toString():"",html_body:!1,ui_push_phase:"V3",status:"0",offline_threading_id:r,message_id:r,threading_id:d.generateThreadingID(this.ctx.clientID||""),"ephemeral_ttl_mode:":"0",manual_retry_cnt:"0",has_attachment:!!(a.attachment||a.url||a.sticker),signatureID:d.getSignatureID(),...a.messageID?{reply_message_id:a.messageID}:{}},n=(l,u)=>l?(s&&typeof s=="function"&&s(l),t(l)):(s&&typeof s=="function"&&s(null,u),e(u));x(a,i,n,()=>w(a,i,n,()=>j(a,i,n,()=>I(a,i,n,()=>S(a,i,n,()=>v(a,i,n,()=>b(i,a.threadID,r,n,a.isGroup??!1)))))))}catch(o){return f.error({error:o},"Failed to send message"),s&&typeof s=="function"&&s(o),t(o)}})}};y.allowedProperties={attachment:!0,url:!0,sticker:!0,emoji:!0,emojiSize:!0,body:!0,mentions:!0,location:!0,threadID:!0,messageID:!0,isGroup:!0};let g=y;export{g as default};