fca-utils
Advanced tools
Comparing version 0.0.6 to 0.0.7
/// <reference types="node" /> | ||
import { IFCAU_API, IFCAU_Options, IFCAU_ListenMessage, MessageObject } from '@xaviabot/fca-unofficial'; | ||
import { EventEmitter } from 'events'; | ||
import { Express } from 'express'; | ||
import EventEmitter from 'events'; | ||
export declare const DEFAULT_USER_AGENT = "Mozilla/5.0 (Linux; Android 10; SM-G996U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36"; | ||
export declare const DEFAULT_OPTIONS: Partial<IFCAU_Options>; | ||
export type AttachmentObject = { | ||
title: string; | ||
url_or_path: string | string[]; | ||
}; | ||
export type SendMessage = (message: string | MessageObject) => Promise<{ | ||
@@ -12,6 +15,6 @@ threadID: string; | ||
}>; | ||
export type SendAttachmentWR = (attachment: string | { | ||
title: string; | ||
url_or_path: string; | ||
}, reply: boolean) => Promise<{ | ||
export type SendAttachmentWR = (attachment: string | string[] | AttachmentObject, options: { | ||
skipFailed: boolean; | ||
reply: boolean; | ||
}) => Promise<{ | ||
threadID: string; | ||
@@ -21,5 +24,4 @@ messageID: string; | ||
}>; | ||
export type SendAttachment = (attachment: string | { | ||
title: string; | ||
url_or_path: string; | ||
export type SendAttachment = (attachment: string | string[] | AttachmentObject, options: { | ||
skipFailed: boolean; | ||
}) => Promise<{ | ||
@@ -37,34 +39,33 @@ threadID: string; | ||
export type ReactionMessage = Pick<IFCAU_ListenMessage, Extract<keyof IFCAU_ListenMessage, 'type'>> extends infer R ? Extract<IFCAU_ListenMessage, { | ||
type: "reaction"; | ||
type: "message_reaction"; | ||
}> extends infer S ? R & S : never : never; | ||
export type UnsendMessage = Pick<IFCAU_ListenMessage, Extract<keyof IFCAU_ListenMessage, 'type'>> extends infer R ? Extract<IFCAU_ListenMessage, { | ||
type: "unsend"; | ||
type: "message_unsend"; | ||
}> extends infer S ? R & S : never : never; | ||
export type OtherMessage = Exclude<IFCAU_ListenMessage, EventMessage | TextMessage | ReactionMessage | UnsendMessage>; | ||
export type TextMessageExtended = TextMessage & { | ||
send: SendMessage; | ||
reply: SendMessage; | ||
sendAttachment: SendAttachmentWR; | ||
}; | ||
export type ReactionMessageExtended = ReactionMessage & { | ||
send: SendMessage; | ||
sendAttachment: SendAttachment; | ||
}; | ||
export type UnsendMessageExtended = UnsendMessage & { | ||
send: SendMessage; | ||
sendAttachment: SendAttachment; | ||
}; | ||
export type CommandsProps = { | ||
name: string; | ||
commandArgs: string[]; | ||
message: TextMessage & { | ||
send: SendMessage; | ||
reply: SendMessage; | ||
sendAttachment: SendAttachmentWR; | ||
}; | ||
message: TextMessageExtended; | ||
}; | ||
export declare interface Client { | ||
on(event: 'ready', listener: (api: IFCAU_API, userID: string) => void): this; | ||
on(event: 'logged', listener: (api: IFCAU_API, userID: string) => void): this; | ||
on(event: 'error', listener: (err: Error) => void): this; | ||
on(event: 'message', listener: (message: TextMessage & { | ||
send: SendMessage; | ||
reply: SendMessage; | ||
sendAttachment: SendAttachmentWR; | ||
}) => void): this; | ||
on(event: 'message', listener: (message: TextMessageExtended) => void): this; | ||
on(event: 'command', listener: (props: CommandsProps) => void): this; | ||
on(event: 'reaction', listener: (message: ReactionMessage & { | ||
send: SendMessage; | ||
sendAttachment: SendAttachment; | ||
}) => void): this; | ||
on(event: 'unsend', listener: (message: UnsendMessage & { | ||
send: SendMessage; | ||
sendAttachment: SendAttachment; | ||
}) => void): this; | ||
on(event: 'reaction', listener: (message: ReactionMessageExtended) => void): this; | ||
on(event: 'unsend', listener: (message: UnsendMessageExtended) => void): this; | ||
on(event: 'others', listener: (message: OtherMessage) => void): this; | ||
@@ -89,5 +90,7 @@ on(event: 'event', listener: (event: EventMessage) => void): this; | ||
getApi(): IFCAU_API | null; | ||
openServer(port?: number, app?: Express): void; | ||
loginWithAppState(EState: string, options: Partial<IFCAU_Options>): void; | ||
loginWithFbState(EState: string, options: Partial<IFCAU_Options>): Promise<IFCAU_API>; | ||
listen(): (EventEmitter & { | ||
stopListening: (callback?: (() => void) | undefined) => void; | ||
}) | undefined; | ||
} | ||
//# sourceMappingURL=index.d.ts.map |
205
lib/index.js
import login from '@xaviabot/fca-unofficial'; | ||
import axios from 'axios'; | ||
import { createReadStream, existsSync } from 'fs'; | ||
import { EventEmitter } from 'events'; | ||
import createHttpsServer from './server.js'; | ||
import EventEmitter from 'events'; | ||
import isCommand from './tools/isCommand.js'; | ||
import { getAppstate } from './utils.js'; | ||
import { eventParser, reactionMessageParser, textMessageParser, unsendMessageParser } from './tools/parser.js'; | ||
export const DEFAULT_USER_AGENT = 'Mozilla/5.0 (Linux; Android 10; SM-G996U Build/QP1A.190711.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Mobile Safari/537.36'; | ||
@@ -27,166 +26,46 @@ export const DEFAULT_OPTIONS = { | ||
#isCommand(message) { | ||
if ((message.type === "message" || message.type === "message_reply") && this.#prefix !== null) { | ||
if (message.body.startsWith(this.#prefix) && this.#api !== null) { | ||
const commandArgs = message.body.slice(this.#prefix.length).trim().split(/ +/g); | ||
const name = commandArgs.shift()?.toLowerCase() ?? ''; | ||
const { threadID, messageID } = message; | ||
return { | ||
name, | ||
commandArgs, | ||
message: { | ||
...message, | ||
send: (message) => this.#api.sendMessage(message, threadID), | ||
reply: (message) => this.#api.sendMessage(message, threadID, messageID), | ||
sendAttachment: async (attachment, reply = false) => { | ||
const imageSource = typeof attachment === "string" ? attachment : attachment.url_or_path; | ||
const msgObj = { | ||
body: typeof attachment === "string" ? "" : attachment.title, | ||
}; | ||
const isExists = existsSync(imageSource); | ||
if (!isExists) { | ||
try { | ||
new URL(imageSource); | ||
msgObj.attachment = await axios.get(imageSource, { responseType: 'stream' }).then(res => res.data); | ||
} | ||
catch (err) { | ||
if (err instanceof Error && err.message.includes('Invalid URL')) | ||
throw new Error("Invalid image source, must be a valid URL or path to the file"); | ||
else | ||
throw err; | ||
} | ||
} | ||
else { | ||
msgObj.attachment = createReadStream(imageSource); | ||
} | ||
return this.#api.sendMessage(msgObj, threadID, reply ? messageID : undefined); | ||
} | ||
} | ||
}; | ||
} | ||
} | ||
return false; | ||
return isCommand(message, this.#prefix, this.#api); | ||
} | ||
openServer(port = 3001, app) { | ||
createHttpsServer(port, app); | ||
async #loginWithEmail(email, password, options) { | ||
return login({ email, password }, { ...DEFAULT_OPTIONS, ...options }) | ||
.then(api => { | ||
}); | ||
} | ||
// loginWithEmail(email: string, password: string, options: Partial<IFCAU_Options>) { | ||
// login({ email, password }, { ...DEFAULT_OPTIONS, ...options }) | ||
// .then(api => { | ||
// }); | ||
// } | ||
loginWithAppState(EState, options) { | ||
async loginWithFbState(EState, options) { | ||
const appState = getAppstate(EState); | ||
login({ appState }, { ...DEFAULT_OPTIONS, ...options }) | ||
.then(api => { | ||
this.#api = api; | ||
this.emit('ready', api, api.getCurrentUserID()); | ||
api.listenMqtt((err, message) => { | ||
if (!message) | ||
return this.emit('error', err ?? new Error('Fbstate expired/Account checkpointed/banned')); | ||
if (message.type === "event") | ||
this.emit('event', message); | ||
else if (message.type === "message" || message.type === "message_reply") { | ||
const command = this.#isCommand(message); | ||
if (command !== false) | ||
this.emit('command', command); | ||
if (command !== false && this.#ignoreMessageInCommandEvent) | ||
return; | ||
const { threadID, messageID } = message; | ||
this.emit('message', { | ||
...message, | ||
send: (message) => api.sendMessage(message, threadID), | ||
reply: (message) => api.sendMessage(message, threadID, messageID), | ||
sendAttachment: async (attachment, reply = false) => { | ||
const imageSource = typeof attachment === "string" ? attachment : attachment.url_or_path; | ||
const msgObj = { | ||
body: typeof attachment === "string" ? "" : attachment.title, | ||
}; | ||
const isExists = existsSync(imageSource); | ||
if (!isExists) { | ||
try { | ||
new URL(imageSource); | ||
msgObj.attachment = await axios.get(imageSource, { responseType: 'stream' }).then(res => res.data); | ||
} | ||
catch (err) { | ||
if (err instanceof Error && err.message.includes('Invalid URL')) | ||
throw new Error("Invalid image source, must be a valid URL or path to the file"); | ||
else | ||
throw err; | ||
} | ||
} | ||
else { | ||
msgObj.attachment = createReadStream(imageSource); | ||
} | ||
return this.#api.sendMessage(msgObj, threadID, reply ? messageID : undefined); | ||
} | ||
}); | ||
} | ||
else if (message.type === "message_reaction") { | ||
const { threadID } = message; | ||
this.emit('reaction', { | ||
...message, | ||
send: (message) => api.sendMessage(message, threadID), | ||
sendAttachment: async (attachment) => { | ||
const imageSource = typeof attachment === "string" ? attachment : attachment.url_or_path; | ||
const msgObj = { | ||
body: typeof attachment === "string" ? "" : attachment.title, | ||
}; | ||
const isExists = existsSync(imageSource); | ||
if (!isExists) { | ||
try { | ||
new URL(imageSource); | ||
msgObj.attachment = await axios.get(imageSource, { responseType: 'stream' }).then(res => res.data); | ||
} | ||
catch (err) { | ||
if (err instanceof Error && err.message.includes('Invalid URL')) | ||
throw new Error("Invalid image source, must be a valid URL or path to the file"); | ||
else | ||
throw err; | ||
} | ||
} | ||
else { | ||
msgObj.attachment = createReadStream(imageSource); | ||
} | ||
return this.#api.sendMessage(msgObj, threadID); | ||
} | ||
}); | ||
} | ||
else if (message.type === "message_unsend") { | ||
const { threadID } = message; | ||
this.emit('unsend', { | ||
...message, | ||
send: (message) => api.sendMessage(message, threadID), | ||
sendAttachment: async (attachment) => { | ||
const imageSource = typeof attachment === "string" ? attachment : attachment.url_or_path; | ||
const msgObj = { | ||
body: typeof attachment === "string" ? "" : attachment.title, | ||
}; | ||
const isExists = existsSync(imageSource); | ||
if (!isExists) { | ||
try { | ||
new URL(imageSource); | ||
msgObj.attachment = await axios.get(imageSource, { responseType: 'stream' }).then(res => res.data); | ||
} | ||
catch (err) { | ||
if (err instanceof Error && err.message.includes('Invalid URL')) | ||
throw new Error("Invalid image source, must be a valid URL or path to the file"); | ||
else | ||
throw err; | ||
} | ||
} | ||
else { | ||
msgObj.attachment = createReadStream(imageSource); | ||
} | ||
return this.#api.sendMessage(msgObj, threadID); | ||
} | ||
}); | ||
} | ||
else | ||
this.emit('others', message); | ||
}); | ||
}) | ||
.catch(err => { | ||
this.emit('error', err); | ||
const api = await login({ appState }, { ...DEFAULT_OPTIONS, ...options }); | ||
this.#api = api; | ||
process.nextTick(() => { | ||
this.emit('logged', api, api.getCurrentUserID()); | ||
}); | ||
return api; | ||
} | ||
listen() { | ||
const api = this.#api; | ||
if (!api) { | ||
this.emit('error', new Error('API not initialized')); | ||
return; | ||
} | ||
return api.listenMqtt((err, message) => { | ||
if (!message) | ||
return this.emit('error', err ?? new Error('Fbstate expired/Account checkpointed/banned')); | ||
if (message.type === "event") | ||
return this.emit('event', eventParser(message, api)); | ||
if (message.type === "message" || message.type === "message_reply") { | ||
const command = this.#isCommand(message); | ||
if (command.status === true) | ||
this.emit('command', command.data); | ||
if (command.status === true && this.#ignoreMessageInCommandEvent === true) | ||
return; | ||
return this.emit('message', textMessageParser(message, api)); | ||
} | ||
if (message.type === "message_reaction") { | ||
return this.emit('reaction', reactionMessageParser(message, api)); | ||
} | ||
if (message.type === "message_unsend") { | ||
return this.emit('unsend', unsendMessageParser(message, api)); | ||
} | ||
return this.emit('others', message); | ||
}); | ||
} | ||
} |
@@ -0,3 +1,5 @@ | ||
/// <reference types="node" /> | ||
import { Express } from 'express'; | ||
export default function createHttpServer(port: number, app?: Express): void; | ||
import http from 'http'; | ||
export default function createHttpServer(port: number, app?: Express): http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>; | ||
//# sourceMappingURL=server.d.ts.map |
@@ -29,2 +29,3 @@ import express from 'express'; | ||
}); | ||
return tServer; | ||
} |
{ | ||
"name": "fca-utils", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "A NodeJS package to help interacting with Facebook Messenger API (fca-unofficial)", | ||
@@ -34,9 +34,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@xaviabot/fca-unofficial": "^1.3.18", | ||
"axios": "^1.3.4", | ||
"cors": "^2.8.5", | ||
"express": "^4.18.2", | ||
"express-rate-limit": "^6.7.0", | ||
"helmet": "^6.0.1" | ||
"@xaviabot/fca-unofficial": "^1.3.21", | ||
"axios": "^1.3.4" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
32681
2
33
548
0
2
- Removedcors@^2.8.5
- Removedexpress@^4.18.2
- Removedexpress-rate-limit@^6.7.0
- Removedhelmet@^6.0.1
- Removedaccepts@1.3.8(transitive)
- Removedarray-flatten@1.1.1(transitive)
- Removedbody-parser@1.20.2(transitive)
- Removedbytes@3.1.2(transitive)
- Removedcall-bind@1.0.7(transitive)
- Removedcontent-disposition@0.5.4(transitive)
- Removedcontent-type@1.0.5(transitive)
- Removedcookie@0.6.0(transitive)
- Removedcookie-signature@1.0.6(transitive)
- Removedcors@2.8.5(transitive)
- Removeddebug@2.6.9(transitive)
- Removeddefine-data-property@1.1.4(transitive)
- Removeddepd@2.0.0(transitive)
- Removeddestroy@1.2.0(transitive)
- Removedee-first@1.1.1(transitive)
- Removedencodeurl@1.0.2(transitive)
- Removedes-define-property@1.0.0(transitive)
- Removedes-errors@1.3.0(transitive)
- Removedescape-html@1.0.3(transitive)
- Removedetag@1.8.1(transitive)
- Removedexpress@4.19.2(transitive)
- Removedexpress-rate-limit@6.11.2(transitive)
- Removedfinalhandler@1.2.0(transitive)
- Removedforwarded@0.2.0(transitive)
- Removedfresh@0.5.2(transitive)
- Removedfunction-bind@1.1.2(transitive)
- Removedget-intrinsic@1.2.4(transitive)
- Removedgopd@1.0.1(transitive)
- Removedhas-property-descriptors@1.0.2(transitive)
- Removedhas-proto@1.0.3(transitive)
- Removedhas-symbols@1.0.3(transitive)
- Removedhasown@2.0.2(transitive)
- Removedhelmet@6.2.0(transitive)
- Removedhttp-errors@2.0.0(transitive)
- Removediconv-lite@0.4.24(transitive)
- Removedipaddr.js@1.9.1(transitive)
- Removedmedia-typer@0.3.0(transitive)
- Removedmerge-descriptors@1.0.1(transitive)
- Removedmethods@1.1.2(transitive)
- Removedmime@1.6.0(transitive)
- Removedms@2.0.02.1.3(transitive)
- Removednegotiator@0.6.3(transitive)
- Removedobject-assign@4.1.1(transitive)
- Removedobject-inspect@1.13.1(transitive)
- Removedon-finished@2.4.1(transitive)
- Removedparseurl@1.3.3(transitive)
- Removedpath-to-regexp@0.1.7(transitive)
- Removedproxy-addr@2.0.7(transitive)
- Removedqs@6.11.0(transitive)
- Removedrange-parser@1.2.1(transitive)
- Removedraw-body@2.5.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsend@0.18.0(transitive)
- Removedserve-static@1.15.0(transitive)
- Removedset-function-length@1.2.2(transitive)
- Removedsetprototypeof@1.2.0(transitive)
- Removedside-channel@1.0.6(transitive)
- Removedstatuses@2.0.1(transitive)
- Removedtoidentifier@1.0.1(transitive)
- Removedtype-is@1.6.18(transitive)
- Removedunpipe@1.0.0(transitive)
- Removedutils-merge@1.0.1(transitive)
- Removedvary@1.1.2(transitive)