fix: Ajusts in sticker message to chatwoot

This commit is contained in:
Davidson Gomes 2023-07-24 20:32:15 -03:00
parent b77f22790b
commit f475391ba6
4 changed files with 141 additions and 136 deletions

View File

@ -41,77 +41,136 @@ export class InstanceController {
chatwoot_url, chatwoot_url,
chatwoot_sign_msg, chatwoot_sign_msg,
}: InstanceDto) { }: InstanceDto) {
this.logger.verbose('requested createInstance from ' + instanceName + ' instance'); try {
this.logger.verbose('requested createInstance from ' + instanceName + ' instance');
if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) { if (instanceName !== instanceName.toLowerCase().replace(/[^a-z0-9]/g, '')) {
throw new BadRequestException( throw new BadRequestException(
'The instance name must be lowercase and without special characters', 'The instance name must be lowercase and without special characters',
); );
}
this.logger.verbose('checking duplicate token');
await this.authService.checkDuplicateToken(token);
this.logger.verbose('creating instance');
const instance = new WAStartupService(
this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName
.toLowerCase()
.replace(/[^a-z0-9]/g, '')
.replace(' ', '');
this.logger.verbose('instance: ' + instance.instanceName + ' created');
this.waMonitor.waInstances[instance.instanceName] = instance;
this.waMonitor.delInstanceTime(instance.instanceName);
this.logger.verbose('generating hash');
const hash = await this.authService.generateHash(
{
instanceName: instance.instanceName,
},
token,
);
this.logger.verbose('hash: ' + hash + ' generated');
let getEvents: string[];
if (webhook) {
if (!isURL(webhook, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in webhook');
} }
this.logger.verbose('creating webhook'); this.logger.verbose('checking duplicate token');
try { await this.authService.checkDuplicateToken(token);
this.webhookService.create(instance, {
enabled: true, this.logger.verbose('creating instance');
url: webhook, const instance = new WAStartupService(
events, this.configService,
this.eventEmitter,
this.repository,
this.cache,
);
instance.instanceName = instanceName
.toLowerCase()
.replace(/[^a-z0-9]/g, '')
.replace(' ', '');
this.logger.verbose('instance: ' + instance.instanceName + ' created');
this.waMonitor.waInstances[instance.instanceName] = instance;
this.waMonitor.delInstanceTime(instance.instanceName);
this.logger.verbose('generating hash');
const hash = await this.authService.generateHash(
{
instanceName: instance.instanceName,
},
token,
);
this.logger.verbose('hash: ' + hash + ' generated');
let getEvents: string[];
if (webhook) {
if (!isURL(webhook, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in webhook');
}
this.logger.verbose('creating webhook');
try {
this.webhookService.create(instance, {
enabled: true,
url: webhook,
events,
webhook_by_events,
});
getEvents = (await this.webhookService.find(instance)).events;
} catch (error) {
this.logger.log(error);
}
}
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) {
let getQrcode: wa.QrCode;
if (qrcode) {
this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp(number);
await delay(5000);
getQrcode = instance.qrCode;
}
const result = {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events, webhook_by_events,
events: getEvents,
qrcode: getQrcode,
};
this.logger.verbose('instance created');
this.logger.verbose(result);
return result;
}
if (!chatwoot_account_id) {
throw new BadRequestException('account_id is required');
}
if (!chatwoot_token) {
throw new BadRequestException('token is required');
}
if (!chatwoot_url) {
throw new BadRequestException('url is required');
}
if (!isURL(chatwoot_url, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
number,
}); });
getEvents = (await this.webhookService.find(instance)).events; this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
number,
);
} catch (error) { } catch (error) {
this.logger.log(error); this.logger.log(error);
} }
}
if (!chatwoot_account_id || !chatwoot_token || !chatwoot_url) { return {
let getQrcode: wa.QrCode;
if (qrcode) {
this.logger.verbose('creating qrcode');
await instance.connectToWhatsapp(number);
await delay(5000);
getQrcode = instance.qrCode;
}
const result = {
instance: { instance: {
instanceName: instance.instanceName, instanceName: instance.instanceName,
status: 'created', status: 'created',
@ -120,75 +179,21 @@ export class InstanceController {
webhook, webhook,
webhook_by_events, webhook_by_events,
events: getEvents, events: getEvents,
qrcode: getQrcode, chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
number,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
},
}; };
this.logger.verbose('instance created');
this.logger.verbose(result);
return result;
}
if (!chatwoot_account_id) {
throw new BadRequestException('account_id is required');
}
if (!chatwoot_token) {
throw new BadRequestException('token is required');
}
if (!chatwoot_url) {
throw new BadRequestException('url is required');
}
if (!isURL(chatwoot_url, { require_tld: false })) {
throw new BadRequestException('Invalid "url" property in chatwoot');
}
const urlServer = this.configService.get<HttpServer>('SERVER').URL;
try {
this.chatwootService.create(instance, {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName,
number,
});
this.chatwootService.initInstanceChatwoot(
instance,
instance.instanceName,
`${urlServer}/chatwoot/webhook/${instance.instanceName}`,
qrcode,
number,
);
} catch (error) { } catch (error) {
this.logger.log(error); console.log(error);
return { error: true, message: error.toString() };
} }
return {
instance: {
instanceName: instance.instanceName,
status: 'created',
},
hash,
webhook,
webhook_by_events,
events: getEvents,
chatwoot: {
enabled: true,
account_id: chatwoot_account_id,
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
number,
name_inbox: instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${instance.instanceName}`,
},
};
} }
public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) { public async connectToWhatsapp({ instanceName, number = null }: InstanceDto) {

View File

@ -102,7 +102,12 @@ export class RepositoryBroker {
this.logger.verbose('creating store path: ' + storePath); this.logger.verbose('creating store path: ' + storePath);
const tempDir = join(storePath, 'temp'); const tempDir = join(storePath, 'temp');
const chatwootDir = join(storePath, 'chatwoot');
if (!fs.existsSync(chatwootDir)) {
this.logger.verbose('creating chatwoot dir: ' + chatwootDir);
fs.mkdirSync(chatwootDir, { recursive: true });
}
if (!fs.existsSync(tempDir)) { if (!fs.existsSync(tempDir)) {
this.logger.verbose('creating temp dir: ' + tempDir); this.logger.verbose('creating temp dir: ' + tempDir);
fs.mkdirSync(tempDir, { recursive: true }); fs.mkdirSync(tempDir, { recursive: true });

View File

@ -1179,7 +1179,7 @@ export class ChatwootService {
videoMessage: msg.videoMessage?.caption, videoMessage: msg.videoMessage?.caption,
extendedTextMessage: msg.extendedTextMessage?.text, extendedTextMessage: msg.extendedTextMessage?.text,
messageContextInfo: msg.messageContextInfo?.stanzaId, messageContextInfo: msg.messageContextInfo?.stanzaId,
stickerMessage: msg.stickerMessage?.fileSha256.toString('base64'), stickerMessage: undefined,
documentMessage: msg.documentMessage?.caption, documentMessage: msg.documentMessage?.caption,
documentWithCaptionMessage: documentWithCaptionMessage:
msg.documentWithCaptionMessage?.message?.documentMessage?.caption, msg.documentWithCaptionMessage?.message?.documentMessage?.caption,
@ -1199,10 +1199,6 @@ export class ChatwootService {
const result = typeKey ? types[typeKey] : undefined; const result = typeKey ? types[typeKey] : undefined;
if (typeKey === 'stickerMessage') {
return null;
}
if (typeKey === 'contactMessage') { if (typeKey === 'contactMessage') {
const vCardData = result.split('\n'); const vCardData = result.split('\n');
const contactInfo = {}; const contactInfo = {};

View File

@ -116,16 +116,15 @@ import { useMultiFileAuthStateDb } from '../../utils/use-multi-file-auth-state-d
import Long from 'long'; import Long from 'long';
import { WebhookRaw } from '../models/webhook.model'; import { WebhookRaw } from '../models/webhook.model';
import { ChatwootRaw } from '../models/chatwoot.model'; import { ChatwootRaw } from '../models/chatwoot.model';
import { SettingsRaw } from '../models';
import { dbserver } from '../../db/db.connect'; import { dbserver } from '../../db/db.connect';
import NodeCache from 'node-cache'; import NodeCache from 'node-cache';
import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db'; import { useMultiFileAuthStateRedisDb } from '../../utils/use-multi-file-auth-state-redis-db';
import sharp from 'sharp'; import sharp from 'sharp';
import { RedisCache } from '../../db/redis.client'; import { RedisCache } from '../../db/redis.client';
import { Log } from '../../config/env.config'; import { Log } from '../../config/env.config';
import ProxyAgent from 'proxy-agent';
import { ChatwootService } from './chatwoot.service'; import { ChatwootService } from './chatwoot.service';
import { waMonitor } from '../whatsapp.module'; import { waMonitor } from '../whatsapp.module';
import { SettingsRaw } from '../models';
export class WAStartupService { export class WAStartupService {
constructor( constructor(
@ -382,7 +381,7 @@ export class WAStartupService {
if (!data) { if (!data) {
this.logger.verbose('Settings not found'); this.logger.verbose('Settings not found');
throw new NotFoundException('Settings not found'); return null;
} }
this.logger.verbose(`Settings url: ${data.reject_call}`); this.logger.verbose(`Settings url: ${data.reject_call}`);
@ -1129,7 +1128,7 @@ export class WAStartupService {
received.messageTimestamp = received.messageTimestamp?.toNumber(); received.messageTimestamp = received.messageTimestamp?.toNumber();
} }
if (settings.groups_ignore && received.key.remoteJid.includes('@g.us')) { if (settings?.groups_ignore && received.key.remoteJid.includes('@g.us')) {
this.logger.verbose('group ignored'); this.logger.verbose('group ignored');
return; return;
} }