Skip to content

Commit f475391

Browse files
committed
fix: Ajusts in sticker message to chatwoot
1 parent b77f227 commit f475391

File tree

4 files changed

+135
-130
lines changed

4 files changed

+135
-130
lines changed

src/whatsapp/controllers/instance.controller.ts

Lines changed: 126 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -41,77 +41,136 @@ export class InstanceController {
4141
chatwoot_url,
4242
chatwoot_sign_msg,
4343
}: InstanceDto) {
44-
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
44+
try {
45+
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
4546

46-
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
47-
throw new BadRequestException(
48-
'The instance name must be lowercase and without special characters',
47+
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
48+
throw new BadRequestException(
49+
'The instance name must be lowercase and without special characters',
50+
);
51+
}
52+
53+
this.logger.verbose('checking duplicate token');
54+
await this.authService.checkDuplicateToken(token);
55+
56+
this.logger.verbose('creating instance');
57+
const instance = new WAStartupService(
58+
this.configService,
59+
this.eventEmitter,
60+
this.repository,
61+
this.cache,
4962
);
50-
}
63+
instance.instanceName = instanceName
64+
.toLowerCase()
65+
.replace(/[^a-z0-9]/g, '')
66+
.replace(' ', '');
5167

52-
this.logger.verbose('checking duplicate token');
53-
await this.authService.checkDuplicateToken(token);
54-
55-
this.logger.verbose('creating instance');
56-
const instance = new WAStartupService(
57-
this.configService,
58-
this.eventEmitter,
59-
this.repository,
60-
this.cache,
61-
);
62-
instance.instanceName = instanceName
63-
.toLowerCase()
64-
.replace(/[^a-z0-9]/g, '')
65-
.replace(' ', '');
66-
67-
this.logger.verbose('instance: ' + instance.instanceName + ' created');
68-
69-
this.waMonitor.waInstances[instance.instanceName] = instance;
70-
this.waMonitor.delInstanceTime(instance.instanceName);
71-
72-
this.logger.verbose('generating hash');
73-
const hash = await this.authService.generateHash(
74-
{
75-
instanceName: instance.instanceName,
76-
},
77-
token,
78-
);
68+
this.logger.verbose('instance: ' + instance.instanceName + ' created');
69+
70+
this.waMonitor.waInstances[instance.instanceName] = instance;
71+
this.waMonitor.delInstanceTime(instance.instanceName);
72+
73+
this.logger.verbose('generating hash');
74+
const hash = await this.authService.generateHash(
75+
{
76+
instanceName: instance.instanceName,
77+
},
78+
token,
79+
);
80+
81+
this.logger.verbose('hash: ' + hash + ' generated');
82+
83+
let getEvents: string[];
84+
85+
if (webhook) {
86+
if (!isURL(webhook, { require_tld: false })) {
87+
throw new BadRequestException('Invalid "url" property in webhook');
88+
}
7989

80-
this.logger.verbose('hash: ' + hash + ' generated');
90+
this.logger.verbose('creating webhook');
91+
try {
92+
this.webhookService.create(instance, {
93+
enabled: true,
94+
url: webhook,
95+
events,
96+
webhook_by_events,
97+
});
8198

82-
let getEvents: string[];
99+
getEvents = (await this.webhookService.find(instance)).events;
100+
} catch (error) {
101+
this.logger.log(error);
102+
}
103+
}
104+
105+
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
106+
let getQrcode: wa.QrCode;
107+
108+
if (qrcode) {
109+
this.logger.verbose('creating qrcode');
110+
await instance.connectToWhatsapp(number);
111+
await delay(5000);
112+
getQrcode = instance.qrCode;
113+
}
114+
115+
const result = {
116+
instance: {
117+
instanceName: instance.instanceName,
118+
status: 'created',
119+
},
120+
hash,
121+
webhook,
122+
webhook_by_events,
123+
events: getEvents,
124+
qrcode: getQrcode,
125+
};
126+
127+
this.logger.verbose('instance created');
128+
this.logger.verbose(result);
129+
130+
return result;
131+
}
132+
133+
if (!chatwoot_account_id) {
134+
throw new BadRequestException('account_id is required');
135+
}
83136

84-
if (webhook) {
85-
if (!isURL(webhook, { require_tld: false })) {
86-
throw new BadRequestException('Invalid "url" property in webhook');
137+
if (!chatwoot_token) {
138+
throw new BadRequestException('token is required');
87139
}
88140

89-
this.logger.verbose('creating webhook');
141+
if (!chatwoot_url) {
142+
throw new BadRequestException('url is required');
143+
}
144+
145+
if (!isURL(chatwoot_url, { require_tld: false })) {
146+
throw new BadRequestException('Invalid "url" property in chatwoot');
147+
}
148+
149+
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
150+
90151
try {
91-
this.webhookService.create(instance, {
152+
this.chatwootService.create(instance, {
92153
enabled: true,
93-
url: webhook,
94-
events,
95-
webhook_by_events,
154+
account_id: chatwoot_account_id,
155+
token: chatwoot_token,
156+
url: chatwoot_url,
157+
sign_msg: chatwoot_sign_msg || false,
158+
name_inbox: instance.instanceName,
159+
number,
96160
});
97161

98-
getEvents = (await this.webhookService.find(instance)).events;
162+
this.chatwootService.initInstanceChatwoot(
163+
instance,
164+
instance.instanceName,
165+
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
166+
qrcode,
167+
number,
168+
);
99169
} catch (error) {
100170
this.logger.log(error);
101171
}
102-
}
103172

104-
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
105-
let getQrcode: wa.QrCode;
106-
107-
if (qrcode) {
108-
this.logger.verbose('creating qrcode');
109-
await instance.connectToWhatsapp(number);
110-
await delay(5000);
111-
getQrcode = instance.qrCode;
112-
}
113-
114-
const result = {
173+
return {
115174
instance: {
116175
instanceName: instance.instanceName,
117176
status: 'created',
@@ -120,75 +179,21 @@ export class InstanceController {
120179
webhook,
121180
webhook_by_events,
122181
events: getEvents,
123-
qrcode: getQrcode,
182+
chatwoot: {
183+
enabled: true,
184+
account_id: chatwoot_account_id,
185+
token: chatwoot_token,
186+
url: chatwoot_url,
187+
sign_msg: chatwoot_sign_msg || false,
188+
number,
189+
name_inbox: instance.instanceName,
190+
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
191+
},
124192
};
125-
126-
this.logger.verbose('instance created');
127-
this.logger.verbose(result);
128-
129-
return result;
130-
}
131-
132-
if (!chatwoot_account_id) {
133-
throw new BadRequestException('account_id is required');
134-
}
135-
136-
if (!chatwoot_token) {
137-
throw new BadRequestException('token is required');
138-
}
139-
140-
if (!chatwoot_url) {
141-
throw new BadRequestException('url is required');
142-
}
143-
144-
if (!isURL(chatwoot_url, { require_tld: false })) {
145-
throw new BadRequestException('Invalid "url" property in chatwoot');
146-
}
147-
148-
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
149-
150-
try {
151-
this.chatwootService.create(instance, {
152-
enabled: true,
153-
account_id: chatwoot_account_id,
154-
token: chatwoot_token,
155-
url: chatwoot_url,
156-
sign_msg: chatwoot_sign_msg || false,
157-
name_inbox: instance.instanceName,
158-
number,
159-
});
160-
161-
this.chatwootService.initInstanceChatwoot(
162-
instance,
163-
instance.instanceName,
164-
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
165-
qrcode,
166-
number,
167-
);
168193
} catch (error) {
169-
this.logger.log(error);
194+
console.log(error);
195+
return { error: true, message: error.toString() };
170196
}
171-
172-
return {
173-
instance: {
174-
instanceName: instance.instanceName,
175-
status: 'created',
176-
},
177-
hash,
178-
webhook,
179-
webhook_by_events,
180-
events: getEvents,
181-
chatwoot: {
182-
enabled: true,
183-
account_id: chatwoot_account_id,
184-
token: chatwoot_token,
185-
url: chatwoot_url,
186-
sign_msg: chatwoot_sign_msg || false,
187-
number,
188-
name_inbox: instance.instanceName,
189-
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
190-
},
191-
};
192197
}
193198

194199
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {

src/whatsapp/repository/repository.manager.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ export class RepositoryBroker {
102102
this.logger.verbose('creating store path: ' + storePath);
103103

104104
const tempDir = join(storePath, 'temp');
105+
const chatwootDir = join(storePath, 'chatwoot');
105106

107+
if (!fs.existsSync(chatwootDir)) {
108+
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
109+
fs.mkdirSync(chatwootDir, { recursive: true });
110+
}
106111
if (!fs.existsSync(tempDir)) {
107112
this.logger.verbose('creating temp dir: ' + tempDir);
108113
fs.mkdirSync(tempDir, { recursive: true });

src/whatsapp/services/chatwoot.service.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ export class ChatwootService {
11791179
videoMessage: msg.videoMessage?.caption,
11801180
extendedTextMessage: msg.extendedTextMessage?.text,
11811181
messageContextInfo: msg.messageContextInfo?.stanzaId,
1182-
stickerMessage: msg.stickerMessage?.fileSha256.toString('base64'),
1182+
stickerMessage: undefined,
11831183
documentMessage: msg.documentMessage?.caption,
11841184
documentWithCaptionMessage:
11851185
msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
@@ -1199,10 +1199,6 @@ export class ChatwootService {
11991199

12001200
const result = typeKey ? types[typeKey] : undefined;
12011201

1202-
if (typeKey === 'stickerMessage') {
1203-
return null;
1204-
}
1205-
12061202
if (typeKey === 'contactMessage') {
12071203
const vCardData = result.split('\n');
12081204
const contactInfo = {};

src/whatsapp/services/whatsapp.service.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,15 @@ import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-d
116116
import Long from 'long';
117117
import { WebhookRaw } from '../models/webhook.model';
118118
import { ChatwootRaw } from '../models/chatwoot.model';
119+
import { SettingsRaw } from '../models';
119120
import { dbserver } from '../../db/db.connect';
120121
import NodeCache from 'node-cache';
121122
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
122123
import sharp from 'sharp';
123124
import { RedisCache } from '../../db/redis.client';
124125
import { Log } from '../../config/env.config';
125-
import ProxyAgent from 'proxy-agent';
126126
import { ChatwootService } from './chatwoot.service';
127127
import { waMonitor } from '../whatsapp.module';
128-
import { SettingsRaw } from '../models';
129128

130129
export class WAStartupService {
131130
constructor(
@@ -382,7 +381,7 @@ export class WAStartupService {
382381

383382
if (!data) {
384383
this.logger.verbose('Settings not found');
385-
throw new NotFoundException('Settings not found');
384+
return null;
386385
}
387386

388387
this.logger.verbose(`Settings url: ${data.reject_call}`);
@@ -1129,7 +1128,7 @@ export class WAStartupService {
11291128
received.messageTimestamp = received.messageTimestamp?.toNumber();
11301129
}
11311130

1132-
if (settings.groups_ignore && received.key.remoteJid.includes('@g.us')) {
1131+
if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
11331132
this.logger.verbose('group ignored');
11341133
return;
11351134
}

0 commit comments

Comments
 (0)