UNPKG

@jikey/fcazero

Version:

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

2 lines (1 loc) 4.92 kB
import{Clients as l}from"../utils/clients.js";import{Constants as _}from"../utils/constants.js";import c from"../utils/logger.js";class p{constructor(s,e,d){this.defaultFuncs=s;this.ctx=d}static formatEventReminders(s){return{reminderID:s.id,eventCreatorID:s.lightweight_event_creator.id,time:s.time,eventType:s.lightweight_event_type.toLowerCase(),locationName:s.location_name,locationCoordinates:s.location_coordinates,locationPage:s.location_page,eventStatus:s.lightweight_event_status.toLowerCase(),note:s.note,repeatMode:s.repeat_mode.toLowerCase(),eventTitle:s.event_title,triggerMessage:s.trigger_message,secondsToNotifyBefore:s.seconds_to_notify_before,allowsRsvp:s.allows_rsvp,relatedEvent:s.related_event,members:s.event_reminder_members.edges.map(function(e){return{memberID:e.node.id,state:e.guest_list_state.toLowerCase()}})}}static formatThreadGraphQLResponse(s){if(s.errors)return s.errors;const e=s.message_thread;if(!e)return null;const d=e.thread_key.thread_fbid?e.thread_key.thread_fbid:e.thread_key.other_user_id,a=e.last_message,r=a&&a.nodes&&a.nodes[0]&&a.nodes[0].message_sender&&a.nodes[0].message_sender.messaging_actor?a.nodes[0].message_sender.messaging_actor.id:null,i=a&&a.nodes&&a.nodes[0]?a.nodes[0].snippet:null,n=e.last_read_receipt,o=n&&n.nodes&&n.nodes[0]&&n.nodes[0].timestamp_precise?n.nodes[0].timestamp_precise:null;return{threadID:d,threadName:e.name,participantIDs:e.all_participants.edges.map(t=>t.node.messaging_actor.id),userInfo:e.all_participants.edges.map(t=>({id:t.node.messaging_actor.id,name:t.node.messaging_actor.name,firstName:t.node.messaging_actor.short_name,vanity:t.node.messaging_actor.username,url:t.node.messaging_actor.url,thumbSrc:t.node.messaging_actor.big_image_src.uri,profileUrl:t.node.messaging_actor.big_image_src.uri,gender:t.node.messaging_actor.gender,type:t.node.messaging_actor.__typename,isFriend:t.node.messaging_actor.is_viewer_friend,isBirthday:!!t.node.messaging_actor.is_birthday})),unreadCount:e.unread_count,messageCount:e.messages_count,timestamp:e.updated_time_precise,muteUntil:e.mute_until,isGroup:e.thread_type=="GROUP",isSubscribed:e.is_viewer_subscribed,isArchived:e.has_viewer_archived,folder:e.folder,cannotReplyReason:e.cannot_reply_reason,eventReminders:e.event_reminders?e.event_reminders.nodes.map(p.formatEventReminders):null,emoji:e.customization_info?e.customization_info.emoji:null,color:e.customization_info&&e.customization_info.outgoing_bubble_color?e.customization_info.outgoing_bubble_color.slice(2):null,threadTheme:e.thread_theme,nicknames:e.customization_info&&e.customization_info.participant_customizations?e.customization_info.participant_customizations.reduce(function(t,m){return m.nickname&&(t[m.participant_id]=m.nickname),t},{}):{},adminIDs:e.thread_admins,approvalMode:!!e.approval_mode,approvalQueue:e.group_approval_queue.nodes.map(t=>({inviterID:t.inviter.id,requesterID:t.requester.id,timestamp:t.request_timestamp,request_source:t.request_source})),reactionsMuteMode:e.reactions_mute_mode.toLowerCase(),mentionsMuteMode:e.mentions_mute_mode.toLowerCase(),isPinProtected:e.is_pin_protected,relatedPageThread:e.related_page_thread,name:e.name,snippet:i,snippetSender:r,snippetAttachments:[],serverTimestamp:e.updated_time_precise,imageSrc:e.image?e.image.uri:null,isCanonicalUser:e.is_canonical_neo_user,isCanonical:e.thread_type!="GROUP",recipientsLoadable:!0,hasEmailParticipant:!1,readOnly:!1,canReply:e.cannot_reply_reason==null,lastMessageTimestamp:e.last_message?e.last_message.timestamp_precise:null,lastMessageType:"message",lastReadTimestamp:o,threadType:e.thread_type=="GROUP"?2:1,inviteLink:{enable:e.joinable_mode?e.joinable_mode.mode==1:!1,link:e.joinable_mode?e.joinable_mode.link:null}}}call(s,e){return new Promise(async(d,a)=>{try{if(!s.threadID)throw new Error("please pass a threadID as a second argument.");if(_.getType(s.threadID)!=="Number"&&_.getType(s.threadID)!=="String")throw new Error("threadID must be a number or a string");const r=[s.threadID];let i={};return r.map(function(n,o){i["o"+o]={doc_id:"3449967031715030",query_params:{id:n,message_limit:0,load_messages:!1,load_read_receipts:!1,before:null}}}),i={queries:JSON.stringify(i),batch_name:"MessengerGraphQLThreadFetcher"},this.defaultFuncs.post("https://www.facebook.com/api/graphqlbatch/",this.ctx.jar,i).then(l.parseAndCheckLogin(this.ctx,this.defaultFuncs)).then(n=>{if(n&&n.error)throw n;const o={};if(n&&Array.isArray(n)){for(let t=n.length-2;t>=0;t--){const m=Object.keys(n[t])[0];let u=null;m!==void 0&&(u=p.formatThreadGraphQLResponse(n[t][m].data),o[u?.threadID||r[r.length-1-t]]=u)}return Object.values(o).length==1?(e&&typeof e=="function"&&e(null,Object.values(o)[0]),d(Object.values(o)[0])):(e&&typeof e=="function"&&e(null,o),d(o))}})}catch(r){return c.error({error:r},"GetThreadInfo: Error"),e&&typeof e=="function"&&(r instanceof Error?e(r):e(new Error(String(r)))),a(r)}})}}export{p as default};