UNPKG

@jikey/fcazero

Version:

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

2 lines (1 loc) 5.65 kB
"use strict";var f=Object.create;var p=Object.defineProperty;var y=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var v=Object.getPrototypeOf,h=Object.prototype.hasOwnProperty;var w=(s,t)=>{for(var e in t)p(s,e,{get:t[e],enumerable:!0})},l=(s,t,e,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of b(t))!h.call(s,r)&&r!==e&&p(s,r,{get:()=>t[r],enumerable:!(o=y(t,r))||o.enumerable});return s};var L=(s,t,e)=>(e=s!=null?f(v(s)):{},l(t||!s||!s.__esModule?p(e,"default",{value:s,enumerable:!0}):e,s)),C=s=>l(p({},"__esModule",{value:!0}),s);var I={};w(I,{default:()=>_});module.exports=C(I);var c=require("../utils/clients.js"),m=require("../utils/constants.js"),d=L(require("../utils/logger.js"),1);class _{constructor(t,e,o){this.defaultFuncs=t;this.ctx=o}static formatEventReminders(t){return{reminderID:t.id,eventCreatorID:t.lightweight_event_creator.id,time:t.time,eventType:t.lightweight_event_type.toLowerCase(),locationName:t.location_name,locationCoordinates:t.location_coordinates,locationPage:t.location_page,eventStatus:t.lightweight_event_status.toLowerCase(),note:t.note,repeatMode:t.repeat_mode.toLowerCase(),eventTitle:t.event_title,triggerMessage:t.trigger_message,secondsToNotifyBefore:t.seconds_to_notify_before,allowsRsvp:t.allows_rsvp,relatedEvent:t.related_event,members:t.event_reminder_members.edges.map(function(e){return{memberID:e.node.id,state:e.guest_list_state.toLowerCase()}})}}static formatThreadGraphQLResponse(t){const e=t.thread_key.thread_fbid?t.thread_key.thread_fbid:t.thread_key.other_user_id,o=t.last_message,r=o&&o.nodes&&o.nodes[0]&&o.nodes[0].message_sender&&o.nodes[0].message_sender.messaging_actor?o.nodes[0].message_sender.messaging_actor.id:null,a=o&&o.nodes&&o.nodes[0]?o.nodes[0].snippet:null,n=t.last_read_receipt,g=n&&n.nodes&&n.nodes[0]&&n.nodes[0].timestamp_precise?n.nodes[0].timestamp_precise:null;return{threadID:e,threadName:t.name,participantIDs:t.all_participants.edges.map(i=>i.node.messaging_actor.id),userInfo:t.all_participants.edges.map(i=>({id:i.node.messaging_actor.id,name:i.node.messaging_actor.name,firstName:i.node.messaging_actor.short_name,vanity:i.node.messaging_actor.username,url:i.node.messaging_actor.url,thumbSrc:i.node.messaging_actor.big_image_src.uri,profileUrl:i.node.messaging_actor.big_image_src.uri,gender:i.node.messaging_actor.gender,type:i.node.messaging_actor.__typename,isFriend:i.node.messaging_actor.is_viewer_friend,isBirthday:!!i.node.messaging_actor.is_birthday})),unreadCount:t.unread_count,messageCount:t.messages_count,timestamp:t.updated_time_precise,muteUntil:t.mute_until,isGroup:t.thread_type=="GROUP",isSubscribed:t.is_viewer_subscribed,isArchived:t.has_viewer_archived,folder:t.folder,cannotReplyReason:t.cannot_reply_reason,eventReminders:t.event_reminders?t.event_reminders.nodes.map(_.formatEventReminders):null,emoji:t.customization_info?t.customization_info.emoji:null,color:t.customization_info&&t.customization_info.outgoing_bubble_color?t.customization_info.outgoing_bubble_color.slice(2):null,threadTheme:t.thread_theme,nicknames:t.customization_info&&t.customization_info.participant_customizations?t.customization_info.participant_customizations.reduce(function(i,u){return u.nickname&&(i[u.participant_id]=u.nickname),i},{}):{},adminIDs:t.thread_admins,approvalMode:!!t.approval_mode,approvalQueue:t.group_approval_queue.nodes.map(i=>({inviterID:i.inviter.id,requesterID:i.requester.id,timestamp:i.request_timestamp,request_source:i.request_source})),reactionsMuteMode:t.reactions_mute_mode.toLowerCase(),mentionsMuteMode:t.mentions_mute_mode.toLowerCase(),isPinProtected:t.is_pin_protected,relatedPageThread:t.related_page_thread,name:t.name,snippet:a,snippetSender:r,snippetAttachments:[],serverTimestamp:t.updated_time_precise,imageSrc:t.image?t.image.uri:null,isCanonicalUser:t.is_canonical_neo_user,isCanonical:t.thread_type!="GROUP",recipientsLoadable:!0,hasEmailParticipant:!1,readOnly:!1,canReply:t.cannot_reply_reason==null,lastMessageTimestamp:t.last_message?t.last_message.timestamp_precise:null,lastMessageType:"message",lastReadTimestamp:g,threadType:t.thread_type=="GROUP"?2:1,inviteLink:{enable:t.joinable_mode?t.joinable_mode.mode==1:!1,link:t.joinable_mode?t.joinable_mode.link:null}}}static formatThreadList(t){return t.map(e=>_.formatThreadGraphQLResponse(e))}call(t,e){return new Promise((o,r)=>{try{if(m.Constants.getType(t.limit)!=="number"||!Number.isFinite(t.limit)||t.limit<0)throw new Error("Limit must be a positive number");if(m.Constants.getType(t.timestamp)!=="number"||!Number.isFinite(t.timestamp)||t.timestamp<0)throw new Error("Timestamp must be a positive number");if(!Array.isArray(t.tags))throw new Error("Tags must be an array");const a={queries:JSON.stringify({o0:{doc_id:"3426149104143726",query_params:{limit:t.limit+(t.timestamp?1:0),before:t.timestamp,tags:t.tags,includeDeliveryReceipts:!0,includeSeqID:!1}}}),batch_name:"MessengerGraphQLThreadlistFetcher"};return this.defaultFuncs.post("https://www.facebook.com/api/graphqlbatch/",this.ctx.jar,a).then(c.Clients.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(n=>{if(n&&n[n.length-1].error_results>0)throw n[0].o0.errors;if(n&&n[n.length-1].successful_results===0)throw{error:"getThreadList: there was no successful_results",res:n};return t.timestamp&&n&&n[0].o0.data.viewer.message_threads.nodes.shift(),e&&typeof e=="function"&&e(null,_.formatThreadList(n&&n[0].o0.data.viewer.message_threads.nodes)),o(_.formatThreadList(n&&n[0].o0.data.viewer.message_threads.nodes))})}catch(a){return d.default.error({error:a},"GetThreadList: Failed to process request"),e&&typeof e=="function"&&e(a),r(a)}})}}