n8n-nodes-evolution-message-search 1.0.7 → 1.0.8
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.
|
@@ -2,6 +2,83 @@
|
|
|
2
2
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
3
3
|
exports.SearchMessages = void 0;
|
|
4
4
|
const n8n_workflow_1 = require('n8n-workflow');
|
|
5
|
+
class SearchMessagesTool extends n8n_workflow_1.Tool {
|
|
6
|
+
constructor(ctx) {
|
|
7
|
+
super();
|
|
8
|
+
this.ctx = ctx;
|
|
9
|
+
this.name = (0, n8n_workflow_1.nodeNameToToolName)(ctx.getNode());
|
|
10
|
+
this.description = 'Search WhatsApp message history. Input should be an object with: keywords (string, optional), dateFrom (ISO date string, optional), dateTo (ISO date string, optional), page (number, default 1), pageSize (number, default 10), conversationId (string, optional).';
|
|
11
|
+
}
|
|
12
|
+
async _call(input) {
|
|
13
|
+
const params = typeof input === 'string' ? JSON.parse(input) : input;
|
|
14
|
+
const credentials = await this.ctx.getCredentials('evolutionApi');
|
|
15
|
+
const serverUrl = credentials['server-url'];
|
|
16
|
+
const apiKey = credentials.apikey;
|
|
17
|
+
const instanceName = this.ctx.getNodeParameter('instanceName', 0);
|
|
18
|
+
const userConversationId = this.ctx.getNodeParameter('conversationId', 0) || '';
|
|
19
|
+
const allowModelSetConversation = this.ctx.getNodeParameter('allowModelSetConversation', 0);
|
|
20
|
+
let conversationId = userConversationId;
|
|
21
|
+
if (!conversationId && allowModelSetConversation && params.conversationId) {
|
|
22
|
+
conversationId = params.conversationId;
|
|
23
|
+
}
|
|
24
|
+
const keywords = params.keywords || '';
|
|
25
|
+
const dateFrom = params.dateFrom || '';
|
|
26
|
+
const dateTo = params.dateTo || '';
|
|
27
|
+
const pageSize = params.pageSize || this.ctx.getNodeParameter('pageSize', 0) || 10;
|
|
28
|
+
const page = params.page || 1;
|
|
29
|
+
const skip = (page - 1) * pageSize;
|
|
30
|
+
const whereClause = {};
|
|
31
|
+
if (conversationId) {
|
|
32
|
+
whereClause['key.remote'] = conversationId;
|
|
33
|
+
}
|
|
34
|
+
if (keywords) {
|
|
35
|
+
whereClause['message.conversationMessage.text'] = {
|
|
36
|
+
contains: keywords,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
if (dateFrom || dateTo) {
|
|
40
|
+
whereClause['timestamp'] = {};
|
|
41
|
+
if (dateFrom) {
|
|
42
|
+
const fromTimestamp = new Date(dateFrom).getTime() / 1000;
|
|
43
|
+
whereClause['timestamp']['gte'] = fromTimestamp;
|
|
44
|
+
}
|
|
45
|
+
if (dateTo) {
|
|
46
|
+
const toTimestamp = new Date(dateTo).getTime() / 1000;
|
|
47
|
+
whereClause['timestamp']['lte'] = toTimestamp;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
const body = {
|
|
51
|
+
where: whereClause,
|
|
52
|
+
take: pageSize,
|
|
53
|
+
skip: skip,
|
|
54
|
+
orderBy: {
|
|
55
|
+
timestamp: 'desc',
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
const cleanUrl = serverUrl.replace(/\/$/, '');
|
|
59
|
+
const responseData = await this.ctx.helpers.request({
|
|
60
|
+
method: 'POST',
|
|
61
|
+
uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
|
|
62
|
+
body,
|
|
63
|
+
headers: {
|
|
64
|
+
'Content-Type': 'application/json',
|
|
65
|
+
'Accept': 'application/json',
|
|
66
|
+
'apikey': apiKey,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const messages = responseData.messages || responseData;
|
|
70
|
+
const total = messages.total || messages.length || 0;
|
|
71
|
+
const totalPages = Math.ceil(total / pageSize);
|
|
72
|
+
const result = {
|
|
73
|
+
total,
|
|
74
|
+
pages: totalPages,
|
|
75
|
+
currentPage: page,
|
|
76
|
+
pageSize,
|
|
77
|
+
records: Array.isArray(messages.records) ? messages.records : messages,
|
|
78
|
+
};
|
|
79
|
+
return JSON.stringify(result);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
5
82
|
class SearchMessages {
|
|
6
83
|
constructor() {
|
|
7
84
|
this.description = {
|
|
@@ -15,9 +92,9 @@ class SearchMessages {
|
|
|
15
92
|
defaults: {
|
|
16
93
|
name: 'Evolution API - Search Messages',
|
|
17
94
|
},
|
|
18
|
-
usableAsTool: true,
|
|
19
95
|
inputs: [n8n_workflow_1.NodeConnectionTypes.Main],
|
|
20
|
-
outputs: [n8n_workflow_1.NodeConnectionTypes.
|
|
96
|
+
outputs: [n8n_workflow_1.NodeConnectionTypes.AiTool],
|
|
97
|
+
outputNames: ['Tool'],
|
|
21
98
|
credentials: [
|
|
22
99
|
{
|
|
23
100
|
name: 'evolutionApi',
|
|
@@ -47,43 +124,6 @@ class SearchMessages {
|
|
|
47
124
|
default: false,
|
|
48
125
|
description: 'If enabled, the AI model can set the conversation ID. If disabled, model must use the value provided above.',
|
|
49
126
|
},
|
|
50
|
-
{
|
|
51
|
-
displayName: 'Model Conversation ID',
|
|
52
|
-
name: 'modelConversationId',
|
|
53
|
-
type: 'string',
|
|
54
|
-
required: false,
|
|
55
|
-
displayOptions: {
|
|
56
|
-
show: {
|
|
57
|
-
allowModelSetConversation: [true],
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
description: 'Conversation ID that the AI model can set (only used if allowed)',
|
|
61
|
-
placeholder: '5511999999999@s.whatsapp.net',
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
displayName: 'Keywords',
|
|
65
|
-
name: 'keywords',
|
|
66
|
-
type: 'string',
|
|
67
|
-
required: false,
|
|
68
|
-
default: '',
|
|
69
|
-
description: 'Search text in message content (model can set)',
|
|
70
|
-
},
|
|
71
|
-
{
|
|
72
|
-
displayName: 'Date From',
|
|
73
|
-
name: 'dateFrom',
|
|
74
|
-
type: 'dateTime',
|
|
75
|
-
required: false,
|
|
76
|
-
default: '',
|
|
77
|
-
description: 'Start date for message search (model can set)',
|
|
78
|
-
},
|
|
79
|
-
{
|
|
80
|
-
displayName: 'Date To',
|
|
81
|
-
name: 'dateTo',
|
|
82
|
-
type: 'dateTime',
|
|
83
|
-
required: false,
|
|
84
|
-
default: '',
|
|
85
|
-
description: 'End date for message search (model can set)',
|
|
86
|
-
},
|
|
87
127
|
{
|
|
88
128
|
displayName: 'Items per Page',
|
|
89
129
|
name: 'pageSize',
|
|
@@ -95,98 +135,13 @@ class SearchMessages {
|
|
|
95
135
|
},
|
|
96
136
|
description: 'Number of results per page (set by user, model cannot change)',
|
|
97
137
|
},
|
|
98
|
-
{
|
|
99
|
-
displayName: 'Page Number',
|
|
100
|
-
name: 'page',
|
|
101
|
-
type: 'number',
|
|
102
|
-
default: 1,
|
|
103
|
-
typeOptions: {
|
|
104
|
-
minValue: 1,
|
|
105
|
-
},
|
|
106
|
-
description: 'Page number to retrieve (model can navigate)',
|
|
107
|
-
},
|
|
108
138
|
],
|
|
109
139
|
};
|
|
110
140
|
}
|
|
111
|
-
async
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
let items = [];
|
|
115
|
-
if (executeFunctions.getInputData) {
|
|
116
|
-
items = executeFunctions.getInputData();
|
|
117
|
-
}
|
|
118
|
-
const returnData = [];
|
|
119
|
-
const i = itemIndex;
|
|
120
|
-
const credentials = await executeFunctions.getCredentials('evolutionApi');
|
|
121
|
-
const serverUrl = credentials['server-url'];
|
|
122
|
-
const apiKey = credentials.apikey;
|
|
123
|
-
const instanceName = executeFunctions.getNodeParameter('instanceName', i);
|
|
124
|
-
const userConversationId = executeFunctions.getNodeParameter('conversationId', i) || '';
|
|
125
|
-
const allowModelSetConversation = executeFunctions.getNodeParameter('allowModelSetConversation', i);
|
|
126
|
-
let conversationId = userConversationId;
|
|
127
|
-
if (!conversationId && allowModelSetConversation) {
|
|
128
|
-
conversationId = executeFunctions.getNodeParameter('modelConversationId', i) || '';
|
|
129
|
-
}
|
|
130
|
-
const keywords = executeFunctions.getNodeParameter('keywords', i) || '';
|
|
131
|
-
const dateFrom = executeFunctions.getNodeParameter('dateFrom', i) || '';
|
|
132
|
-
const dateTo = executeFunctions.getNodeParameter('dateTo', i) || '';
|
|
133
|
-
const pageSize = executeFunctions.getNodeParameter('pageSize', i) || 10;
|
|
134
|
-
const page = executeFunctions.getNodeParameter('page', i) || 1;
|
|
135
|
-
const skip = (page - 1) * pageSize;
|
|
136
|
-
const whereClause = {};
|
|
137
|
-
if (conversationId) {
|
|
138
|
-
whereClause['key.remote'] = conversationId;
|
|
139
|
-
}
|
|
140
|
-
if (keywords) {
|
|
141
|
-
whereClause['message.conversationMessage.text'] = {
|
|
142
|
-
contains: keywords,
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
if (dateFrom || dateTo) {
|
|
146
|
-
whereClause['timestamp'] = {};
|
|
147
|
-
if (dateFrom) {
|
|
148
|
-
const fromTimestamp = new Date(dateFrom).getTime() / 1000;
|
|
149
|
-
whereClause['timestamp']['gte'] = fromTimestamp;
|
|
150
|
-
}
|
|
151
|
-
if (dateTo) {
|
|
152
|
-
const toTimestamp = new Date(dateTo).getTime() / 1000;
|
|
153
|
-
whereClause['timestamp']['lte'] = toTimestamp;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
const body = {
|
|
157
|
-
where: whereClause,
|
|
158
|
-
take: pageSize,
|
|
159
|
-
skip: skip,
|
|
160
|
-
orderBy: {
|
|
161
|
-
timestamp: 'desc',
|
|
162
|
-
},
|
|
141
|
+
async supplyData() {
|
|
142
|
+
return {
|
|
143
|
+
response: new SearchMessagesTool(this),
|
|
163
144
|
};
|
|
164
|
-
const cleanUrl = serverUrl.replace(/\/$/, '');
|
|
165
|
-
const responseData = await executeFunctions.helpers.request({
|
|
166
|
-
method: 'POST',
|
|
167
|
-
uri: `${cleanUrl}/chat/findMessages/${instanceName}`,
|
|
168
|
-
body,
|
|
169
|
-
headers: {
|
|
170
|
-
'Content-Type': 'application/json',
|
|
171
|
-
'Accept': 'application/json',
|
|
172
|
-
'apikey': apiKey,
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
const messages = responseData.messages || responseData;
|
|
176
|
-
const total = messages.total || messages.length || 0;
|
|
177
|
-
const totalPages = Math.ceil(total / pageSize);
|
|
178
|
-
const result = {
|
|
179
|
-
total,
|
|
180
|
-
pages: totalPages,
|
|
181
|
-
currentPage: page,
|
|
182
|
-
pageSize,
|
|
183
|
-
records: Array.isArray(messages.records) ? messages.records : messages,
|
|
184
|
-
};
|
|
185
|
-
returnData.push({
|
|
186
|
-
json: result,
|
|
187
|
-
pairedItem: { item: i },
|
|
188
|
-
});
|
|
189
|
-
return [returnData];
|
|
190
145
|
}
|
|
191
146
|
}
|
|
192
147
|
exports.SearchMessages = SearchMessages;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "n8n-nodes-evolution-message-search",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.8",
|
|
4
4
|
"description": "Evolution API - Search Messages node for n8n. Search WhatsApp message history by keywords, dates, and conversation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"n8n-community-node-package",
|