@jep182/n8n-nodes-whatsthat 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WhatsThatTargets = void 0;
4
+ const n8n_workflow_1 = require("n8n-workflow");
5
+ const access_1 = require("../../shared/access");
6
+ const runtime_1 = require("../../shared/runtime");
7
+ class WhatsThatTargets {
8
+ constructor() {
9
+ this.description = {
10
+ displayName: 'WhatsThat Targets',
11
+ name: 'whatsThatTargets',
12
+ icon: 'file:../WhatsThatSession/whatsthat.svg',
13
+ group: ['transform'],
14
+ version: 1,
15
+ description: 'Discover, link, and manage chats and groups for WhatsThat',
16
+ defaults: { name: 'WhatsThat Targets' },
17
+ inputs: ['main'],
18
+ outputs: ['main'],
19
+ credentials: [{ name: 'whatsThatRuntime', required: true }],
20
+ properties: [
21
+ {
22
+ displayName: 'Operation',
23
+ name: 'operation',
24
+ type: 'options',
25
+ default: 'listDiscovered',
26
+ options: [
27
+ { name: 'List Discovered Targets', value: 'listDiscovered' },
28
+ { name: 'List Linked Targets', value: 'listLinked' },
29
+ { name: 'Link Target', value: 'link' },
30
+ { name: 'Unlink Target', value: 'unlink' },
31
+ ],
32
+ },
33
+ {
34
+ displayName: 'Session ID',
35
+ name: 'sessionId',
36
+ type: 'string',
37
+ default: '',
38
+ },
39
+ {
40
+ displayName: 'Target JID',
41
+ name: 'jid',
42
+ type: 'string',
43
+ default: '',
44
+ displayOptions: {
45
+ show: { operation: ['link'] },
46
+ },
47
+ },
48
+ {
49
+ displayName: 'Alias',
50
+ name: 'alias',
51
+ type: 'string',
52
+ default: '',
53
+ displayOptions: {
54
+ show: { operation: ['link', 'unlink'] },
55
+ },
56
+ },
57
+ ],
58
+ };
59
+ }
60
+ async execute() {
61
+ const items = this.getInputData();
62
+ const returnData = [];
63
+ const access = await (0, access_1.buildAccess)(this);
64
+ for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
65
+ try {
66
+ const operation = this.getNodeParameter('operation', itemIndex);
67
+ const sessionId = this.getNodeParameter('sessionId', itemIndex);
68
+ let json;
69
+ switch (operation) {
70
+ case 'listDiscovered':
71
+ json = await runtime_1.registry.listTargets(access, sessionId);
72
+ break;
73
+ case 'listLinked':
74
+ json = await runtime_1.registry.listLinkedTargets(access, sessionId);
75
+ break;
76
+ case 'link':
77
+ json = await runtime_1.registry.connectTarget(access, sessionId, this.getNodeParameter('alias', itemIndex), this.getNodeParameter('jid', itemIndex));
78
+ break;
79
+ case 'unlink':
80
+ json = {
81
+ removed: await runtime_1.registry.unlinkTarget(access, sessionId, this.getNodeParameter('alias', itemIndex)),
82
+ };
83
+ break;
84
+ default:
85
+ throw new Error(`Unsupported operation ${operation}`);
86
+ }
87
+ returnData.push({ json: json, pairedItem: itemIndex });
88
+ }
89
+ catch (error) {
90
+ if (this.continueOnFail()) {
91
+ returnData.push({ json: { error: error.message }, pairedItem: itemIndex });
92
+ continue;
93
+ }
94
+ throw new n8n_workflow_1.NodeOperationError(this.getNode(), error, { itemIndex });
95
+ }
96
+ }
97
+ return [returnData];
98
+ }
99
+ }
100
+ exports.WhatsThatTargets = WhatsThatTargets;
@@ -0,0 +1,5 @@
1
+ import type { INodeType, INodeTypeDescription, ITriggerFunctions, ITriggerResponse } from 'n8n-workflow';
2
+ export declare class WhatsThatTrigger implements INodeType {
3
+ description: INodeTypeDescription;
4
+ trigger(this: ITriggerFunctions): Promise<ITriggerResponse>;
5
+ }
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WhatsThatTrigger = void 0;
4
+ const runtime_1 = require("../../shared/runtime");
5
+ class WhatsThatTrigger {
6
+ constructor() {
7
+ this.description = {
8
+ displayName: 'WhatsThat Trigger',
9
+ name: 'whatsThatTrigger',
10
+ icon: 'file:../WhatsThatSession/whatsthat.svg',
11
+ group: ['trigger'],
12
+ version: 1,
13
+ description: 'Listen to session, message, and group events from WhatsThat',
14
+ defaults: { name: 'WhatsThat Trigger' },
15
+ inputs: [],
16
+ outputs: ['main'],
17
+ credentials: [{ name: 'whatsThatRuntime', required: true }],
18
+ properties: [
19
+ { displayName: 'Session ID', name: 'sessionId', type: 'string', default: '' },
20
+ {
21
+ displayName: 'Event',
22
+ name: 'eventName',
23
+ type: 'options',
24
+ default: 'message.received',
25
+ options: [
26
+ { name: 'Message Received', value: 'message.received' },
27
+ { name: 'Message From Me', value: 'message.from_me' },
28
+ { name: 'Message Sent', value: 'message.sent' },
29
+ { name: 'Session Pairing', value: 'session.pairing' },
30
+ { name: 'Session Connected', value: 'session.connected' },
31
+ { name: 'Session Disconnected', value: 'session.disconnected' },
32
+ { name: 'Group Updated', value: 'group.updated' },
33
+ { name: 'Group Participants', value: 'group.participants' },
34
+ { name: 'Any Event', value: '*' }
35
+ ],
36
+ }
37
+ ],
38
+ };
39
+ }
40
+ async trigger() {
41
+ const sessionId = this.getNodeParameter('sessionId');
42
+ const eventName = this.getNodeParameter('eventName');
43
+ const handler = (event) => {
44
+ if (event.sessionId !== sessionId)
45
+ return;
46
+ if (eventName !== '*' && event.event !== eventName)
47
+ return;
48
+ this.emit([[{ json: event }]]);
49
+ };
50
+ runtime_1.registry.on('event', handler);
51
+ return {
52
+ closeFunction: async () => {
53
+ runtime_1.registry.off('event', handler);
54
+ },
55
+ };
56
+ }
57
+ }
58
+ exports.WhatsThatTrigger = WhatsThatTrigger;
@@ -0,0 +1,18 @@
1
+ import type { NodeContext } from './context';
2
+ import type { DedupRecord, DiscoveredTarget, LinkedTarget, SessionRecord } from './types';
3
+ export declare function buildAccess(context: NodeContext): Promise<{
4
+ config: import("./context").RuntimeConfig;
5
+ paths: {
6
+ root: string;
7
+ authRoot: string;
8
+ tablesFallback: string;
9
+ };
10
+ listSessions: () => Promise<SessionRecord[]>;
11
+ saveSessions: (data: SessionRecord[]) => Promise<void>;
12
+ listLinkedTargets: () => Promise<LinkedTarget[]>;
13
+ saveLinkedTargets: (data: LinkedTarget[]) => Promise<void>;
14
+ listDiscoveredTargets: () => Promise<DiscoveredTarget[]>;
15
+ saveDiscoveredTargets: (data: DiscoveredTarget[]) => Promise<void>;
16
+ listDedup: () => Promise<DedupRecord[]>;
17
+ saveDedup: (data: DedupRecord[]) => Promise<void>;
18
+ }>;
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildAccess = buildAccess;
4
+ const context_1 = require("./context");
5
+ const fs_1 = require("./fs");
6
+ const store_1 = require("./store");
7
+ async function buildAccess(context) {
8
+ const config = await (0, context_1.getRuntimeConfig)(context);
9
+ const paths = (0, context_1.runtimePaths)(config);
10
+ await (0, fs_1.ensureDir)(paths.root);
11
+ await (0, fs_1.ensureDir)(paths.authRoot);
12
+ return {
13
+ config,
14
+ paths,
15
+ listSessions: () => (0, store_1.listRecords)(context, config, 'sessions'),
16
+ saveSessions: (data) => (0, store_1.saveRecords)(context, config, 'sessions', data),
17
+ listLinkedTargets: () => (0, store_1.listRecords)(context, config, 'linked_targets'),
18
+ saveLinkedTargets: (data) => (0, store_1.saveRecords)(context, config, 'linked_targets', data),
19
+ listDiscoveredTargets: () => (0, store_1.listRecords)(context, config, 'discovered_targets'),
20
+ saveDiscoveredTargets: (data) => (0, store_1.saveRecords)(context, config, 'discovered_targets', data),
21
+ listDedup: () => (0, store_1.listRecords)(context, config, 'dedup'),
22
+ saveDedup: (data) => (0, store_1.saveRecords)(context, config, 'dedup', data),
23
+ };
24
+ }
@@ -0,0 +1,15 @@
1
+ import type { IDataObject, IExecuteFunctions, ILoadOptionsFunctions, IDataTableProjectAggregateService, IDataTableProjectService } from 'n8n-workflow';
2
+ export type NodeContext = IExecuteFunctions | ILoadOptionsFunctions;
3
+ export interface RuntimeConfig {
4
+ storagePath: string;
5
+ useDataTables: boolean;
6
+ }
7
+ export declare function getRuntimeConfig(context: NodeContext): Promise<RuntimeConfig>;
8
+ export declare function runtimePaths(config: RuntimeConfig): {
9
+ root: string;
10
+ authRoot: string;
11
+ tablesFallback: string;
12
+ };
13
+ export declare function getAggregateProxy(context: NodeContext): Promise<IDataTableProjectAggregateService | null>;
14
+ export declare function getTableProxy(context: NodeContext, tableId: string): Promise<IDataTableProjectService | null>;
15
+ export declare function asDataObject(value: unknown): IDataObject;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getRuntimeConfig = getRuntimeConfig;
7
+ exports.runtimePaths = runtimePaths;
8
+ exports.getAggregateProxy = getAggregateProxy;
9
+ exports.getTableProxy = getTableProxy;
10
+ exports.asDataObject = asDataObject;
11
+ const node_path_1 = __importDefault(require("node:path"));
12
+ async function getRuntimeConfig(context) {
13
+ const credentials = await context.getCredentials('whatsThatRuntime');
14
+ const storagePath = String(credentials.storagePath || '/home/node/.n8n/whatsthat');
15
+ return {
16
+ storagePath,
17
+ useDataTables: Boolean(credentials.useDataTables ?? true),
18
+ };
19
+ }
20
+ function runtimePaths(config) {
21
+ return {
22
+ root: config.storagePath,
23
+ authRoot: node_path_1.default.join(config.storagePath, 'auth'),
24
+ tablesFallback: node_path_1.default.join(config.storagePath, 'tables'),
25
+ };
26
+ }
27
+ async function getAggregateProxy(context) {
28
+ if (!context.helpers.getDataTableAggregateProxy) {
29
+ return null;
30
+ }
31
+ return context.helpers.getDataTableAggregateProxy();
32
+ }
33
+ async function getTableProxy(context, tableId) {
34
+ if (!context.helpers.getDataTableProxy) {
35
+ return null;
36
+ }
37
+ return context.helpers.getDataTableProxy(tableId);
38
+ }
39
+ function asDataObject(value) {
40
+ return value;
41
+ }
@@ -0,0 +1,4 @@
1
+ export declare function ensureDir(dirPath: string): Promise<void>;
2
+ export declare function ensureDirFor(filePath: string): Promise<void>;
3
+ export declare function readJson<T>(filePath: string, fallback: T): Promise<T>;
4
+ export declare function writeJson<T>(filePath: string, payload: T): Promise<void>;
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ensureDir = ensureDir;
7
+ exports.ensureDirFor = ensureDirFor;
8
+ exports.readJson = readJson;
9
+ exports.writeJson = writeJson;
10
+ const promises_1 = __importDefault(require("node:fs/promises"));
11
+ const node_path_1 = __importDefault(require("node:path"));
12
+ async function ensureDir(dirPath) {
13
+ await promises_1.default.mkdir(dirPath, { recursive: true });
14
+ }
15
+ async function ensureDirFor(filePath) {
16
+ await ensureDir(node_path_1.default.dirname(filePath));
17
+ }
18
+ async function readJson(filePath, fallback) {
19
+ try {
20
+ const raw = await promises_1.default.readFile(filePath, 'utf8');
21
+ return JSON.parse(raw);
22
+ }
23
+ catch {
24
+ await ensureDirFor(filePath);
25
+ await promises_1.default.writeFile(filePath, JSON.stringify(fallback, null, 2), 'utf8');
26
+ return fallback;
27
+ }
28
+ }
29
+ async function writeJson(filePath, payload) {
30
+ await ensureDirFor(filePath);
31
+ await promises_1.default.writeFile(filePath, JSON.stringify(payload, null, 2), 'utf8');
32
+ }
@@ -0,0 +1,68 @@
1
+ import { EventEmitter } from 'node:events';
2
+ import { proto } from '@whiskeysockets/baileys';
3
+ import type { DiscoveredTarget, LinkedTarget, SessionRecord } from './types';
4
+ type DataAccess = {
5
+ listSessions(): Promise<SessionRecord[]>;
6
+ saveSessions(data: SessionRecord[]): Promise<void>;
7
+ listLinkedTargets(): Promise<LinkedTarget[]>;
8
+ saveLinkedTargets(data: LinkedTarget[]): Promise<void>;
9
+ listDiscoveredTargets(): Promise<DiscoveredTarget[]>;
10
+ saveDiscoveredTargets(data: DiscoveredTarget[]): Promise<void>;
11
+ };
12
+ type SendRequest = {
13
+ sessionId: string;
14
+ channelAlias?: string;
15
+ jid?: string;
16
+ type: 'text' | 'image' | 'video' | 'audio' | 'document' | 'reaction' | 'location' | 'contact' | 'poll';
17
+ sendAsDocument?: boolean;
18
+ message?: string;
19
+ mediaUrl?: string;
20
+ mimetype?: string;
21
+ fileName?: string;
22
+ caption?: string;
23
+ replyToMessageId?: string;
24
+ reactionText?: string;
25
+ location?: {
26
+ degreesLatitude: number;
27
+ degreesLongitude: number;
28
+ name?: string;
29
+ address?: string;
30
+ };
31
+ contact?: {
32
+ displayName: string;
33
+ vcard: string;
34
+ };
35
+ poll?: {
36
+ name: string;
37
+ values: string[];
38
+ selectableCount?: number;
39
+ };
40
+ };
41
+ declare class WhatsThatRegistry extends EventEmitter {
42
+ private sockets;
43
+ ensureSession(storageRoot: string, access: DataAccess, input: {
44
+ sessionId: string;
45
+ label: string;
46
+ phoneNumberForPairing?: string;
47
+ }): Promise<SessionRecord>;
48
+ connectSession(storageRoot: string, access: DataAccess, sessionId: string): Promise<SessionRecord>;
49
+ listSessions(access: DataAccess): Promise<SessionRecord[]>;
50
+ getSession(access: DataAccess, sessionId: string): Promise<SessionRecord | undefined>;
51
+ disconnectSession(access: DataAccess, sessionId: string): Promise<SessionRecord | undefined>;
52
+ removeSession(access: DataAccess, sessionId: string): Promise<boolean>;
53
+ listTargets(access: DataAccess, sessionId: string): Promise<DiscoveredTarget[]>;
54
+ listLinkedTargets(access: DataAccess, sessionId: string): Promise<LinkedTarget[]>;
55
+ connectTarget(access: DataAccess, sessionId: string, alias: string, jid: string): Promise<LinkedTarget>;
56
+ unlinkTarget(access: DataAccess, sessionId: string, alias: string): Promise<boolean>;
57
+ sendMessage(access: DataAccess, request: SendRequest): Promise<Record<string, unknown>>;
58
+ private buildContent;
59
+ private syncGroups;
60
+ private rememberTarget;
61
+ private upsertSession;
62
+ private upsertDiscoveredTarget;
63
+ private emitRuntime;
64
+ private required;
65
+ }
66
+ export declare const registry: WhatsThatRegistry;
67
+ export declare function extractMessageText(message: proto.IMessage | null | undefined): string | undefined;
68
+ export {};