Skip to content

Commit a9c087c

Browse files
committed
correction of messages sent by the api and typebot not appearing in chatwoot
1 parent e1ac296 commit a9c087c

File tree

4 files changed

+55
-24
lines changed

4 files changed

+55
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* Remove rabbitmq queues when delete instances
1010
* Improvement in restart instance to completely redo the connection
1111
* Update node version: v20
12+
* Correction of messages sent by the api and typebot not appearing in chatwoot
1213

1314
# 1.5.2 (2023-09-28 17:56)
1415

src/whatsapp/services/chatwoot.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ export class ChatwootService {
12791279
return null;
12801280
}
12811281

1282-
if (event === 'messages.upsert') {
1282+
if (event === 'messages.upsert' || event === 'send.messages') {
12831283
this.logger.verbose('event messages.upsert');
12841284

12851285
if (body.key.remoteJid === 'status@broadcast') {

src/whatsapp/services/typebot.service.ts

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export class TypebotService {
9999
const remoteJid = data.remoteJid;
100100
const url = data.url;
101101
const typebot = data.typebot;
102+
const startSession = data.startSession;
102103
const variables = data.variables;
103104
const findTypebot = await this.find(instance);
104105
const sessions = (findTypebot.sessions as Session[]) ?? [];
@@ -116,37 +117,61 @@ export class TypebotService {
116117
prefilledVariables[variable.name] = variable.value;
117118
});
118119

119-
const response = await this.createNewSession(instance, {
120-
url: url,
121-
typebot: typebot,
122-
remoteJid: remoteJid,
123-
expire: expire,
124-
keyword_finish: keyword_finish,
125-
delay_message: delay_message,
126-
unknown_message: unknown_message,
127-
listening_from_me: listening_from_me,
128-
sessions: sessions,
129-
prefilledVariables: prefilledVariables,
130-
});
120+
if (startSession) {
121+
const response = await this.createNewSession(instance, {
122+
url: url,
123+
typebot: typebot,
124+
remoteJid: remoteJid,
125+
expire: expire,
126+
keyword_finish: keyword_finish,
127+
delay_message: delay_message,
128+
unknown_message: unknown_message,
129+
listening_from_me: listening_from_me,
130+
sessions: sessions,
131+
prefilledVariables: prefilledVariables,
132+
});
133+
134+
if (response.sessionId) {
135+
await this.sendWAMessage(instance, remoteJid, response.messages, response.input, response.clientSideActions);
136+
137+
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
138+
remoteJid: remoteJid,
139+
url: url,
140+
typebot: typebot,
141+
prefilledVariables: prefilledVariables,
142+
sessionId: `${response.sessionId}`,
143+
});
144+
} else {
145+
throw new Error('Session ID not found in response');
146+
}
147+
} else {
148+
const id = Math.floor(Math.random() * 10000000000).toString();
149+
150+
const reqData = {
151+
sessionId: id,
152+
startParams: {
153+
typebot: data.typebot,
154+
prefilledVariables: prefilledVariables,
155+
},
156+
};
157+
158+
const request = await axios.post(data.url + '/api/v1/sendMessage', reqData);
131159

132-
if (response.sessionId) {
133160
await this.sendWAMessage(
134161
instance,
135162
remoteJid,
136-
response.messages,
137-
response.input,
138-
response.clientSideActions,
163+
request.data.messages,
164+
request.data.input,
165+
request.data.clientSideActions,
139166
);
140167

141168
this.waMonitor.waInstances[instance.instanceName].sendDataWebhook(Events.TYPEBOT_START, {
142169
remoteJid: remoteJid,
143170
url: url,
144171
typebot: typebot,
145-
prefilledVariables: prefilledVariables,
146-
sessionId: `${response.sessionId}`,
172+
variables: variables,
173+
sessionId: id,
147174
});
148-
} else {
149-
throw new Error("Session ID not found in response");
150175
}
151176

152177
return {
@@ -227,7 +252,7 @@ export class TypebotService {
227252
remoteJid: data.remoteJid,
228253
pushName: data.pushName || 'Default Name',
229254
instanceName: instance.instanceName,
230-
}
255+
},
231256
});
232257

233258
const typebotData = {

src/whatsapp/services/whatsapp.service.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import EventEmitter2 from 'eventemitter2';
4040
import fs, { existsSync, readFileSync } from 'fs';
4141
import Long from 'long';
4242
import NodeCache from 'node-cache';
43+
import { getMIMEType } from 'node-mime-types';
4344
import { release } from 'os';
4445
import { join } from 'path';
4546
import P from 'pino';
@@ -2387,12 +2388,16 @@ export class WAStartupService {
23872388

23882389
if (mediaMessage.mediatype === 'image' && !mediaMessage.fileName) {
23892390
mediaMessage.fileName = 'image.png';
2390-
mimetype = 'image/png';
23912391
}
23922392

23932393
if (mediaMessage.mediatype === 'video' && !mediaMessage.fileName) {
23942394
mediaMessage.fileName = 'video.mp4';
2395-
mimetype = 'video/mp4';
2395+
}
2396+
2397+
if (isURL(mediaMessage.media)) {
2398+
mimetype = getMIMEType(mediaMessage.media);
2399+
} else {
2400+
mimetype = getMIMEType(mediaMessage.fileName);
23962401
}
23972402

23982403
this.logger.verbose('Mimetype: ' + mimetype);

0 commit comments

Comments
 (0)