@open-wa/wa-automate 4.32.14 → 4.32.15

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.
@@ -16,6 +16,14 @@ exports.setupChatwootOutgoingMessageHandler = exports.chatwootMiddleware = void
16
16
  const axios_1 = __importDefault(require("axios"));
17
17
  const form_data_1 = __importDefault(require("form-data"));
18
18
  const mime_types_1 = __importDefault(require("mime-types"));
19
+ const contactReg = {
20
+ //WID : chatwoot contact ID
21
+ "example@c.us": "1"
22
+ };
23
+ const convoReg = {
24
+ //WID : chatwoot conversation ID
25
+ "example@c.us": "1"
26
+ };
19
27
  const chatwootMiddleware = (cliConfig, client) => {
20
28
  return (req, res) => __awaiter(void 0, void 0, void 0, function* () {
21
29
  const processMesssage = () => __awaiter(void 0, void 0, void 0, function* () {
@@ -35,6 +43,8 @@ const chatwootMiddleware = (cliConfig, client) => {
35
43
  return;
36
44
  const { attachments, content } = m;
37
45
  const to = `${contact}@c.us`;
46
+ if (!convoReg[to])
47
+ convoReg[to] = body.conversation.id;
38
48
  if ((attachments === null || attachments === void 0 ? void 0 : attachments.length) > 0) {
39
49
  //has attachments
40
50
  const [firstAttachment, ...restAttachments] = attachments;
@@ -97,14 +107,6 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
97
107
  headers: Object.assign({ api_access_token }, _headers)
98
108
  });
99
109
  };
100
- const contactReg = {
101
- //WID : chatwoot contact ID
102
- "example@c.us": "1"
103
- };
104
- const convoReg = {
105
- //WID : chatwoot conversation ID
106
- "example@c.us": "1"
107
- };
108
110
  const { data: get_inbox } = yield cwReq(`inboxes/${resolvedInbox}`, 'get');
109
111
  // const inboxId = `openwa_${sessionId}`
110
112
  /**
@@ -132,7 +134,9 @@ const setupChatwootOutgoingMessageHandler = (cliConfig, client) => __awaiter(voi
132
134
  const getContactConversation = (number) => __awaiter(void 0, void 0, void 0, function* () {
133
135
  try {
134
136
  const { data } = yield cwReq(`contacts/${contactReg[number]}/conversations`, 'get');
135
- return data.payload.filter(c => c.inbox_id === resolvedInbox).sort((a, b) => a.id - b.id)[0];
137
+ const allContactConversations = data.payload.filter(c => c.inbox_id === resolvedInbox).sort((a, b) => a.id - b.id);
138
+ const [opened, notOpen] = [allContactConversations.filter(c => c.status === 'open'), allContactConversations.filter(c => c.status != 'open')];
139
+ return opened[0] || notOpen[0];
136
140
  }
137
141
  catch (error) {
138
142
  return;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@open-wa/wa-automate",
3
- "version": "4.32.14",
3
+ "version": "4.32.15",
4
4
  "licenseCheckUrl": "https://funcs.openwa.dev/license-check",
5
5
  "brokenMethodReportUrl": "https://funcs.openwa.dev/report-bm",
6
6
  "patches": "https://cdn.openwa.dev/patches.json",