Skip to content

Commit

Permalink
Merge branch 'release/1.7.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidsonGomes committed Apr 28, 2024
2 parents e071f56 + 95907b3 commit 633dbb8
Show file tree
Hide file tree
Showing 25 changed files with 127 additions and 212 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# 1.7.4 (develop)
# 1.7.4 (2024-04-28 09:46)

### Fixed
* Adjusts in proxy on fetchAgent
* Recovering messages lost with redis cache
* Log when init redis cache service
* Recovering messages lost with redis cache
* Chatwoot inbox name
* Update Baileys version

# 1.7.3 (2024-04-18 12:07)

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:20.7.0-alpine AS builder

LABEL version="1.7.3" description="Api to control whatsapp features through http requests."
LABEL version="1.7.4" description="Api to control whatsapp features through http requests."
LABEL maintainer="Davidson Gomes" git="https://github.com/DavidsonGomes"
LABEL contact="[email protected]"

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "evolution-api",
"version": "1.7.3",
"version": "1.7.4",
"description": "Rest api for communication with WhatsApp",
"main": "./dist/src/main.js",
"scripts": {
Expand Down Expand Up @@ -46,10 +46,10 @@
"@figuro/chatwoot-sdk": "^1.1.16",
"@hapi/boom": "^10.0.1",
"@sentry/node": "^7.59.2",
"@whiskeysockets/baileys": "github:AtendAI/Baileys",
"amqplib": "^0.10.3",
"aws-sdk": "^2.1499.0",
"axios": "^1.6.5",
"baileys": "^6.7.0",
"class-validator": "^0.14.1",
"compression": "^1.7.4",
"cors": "^2.8.5",
Expand Down
11 changes: 6 additions & 5 deletions src/api/controllers/instance.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { delay } from '@whiskeysockets/baileys';
import { delay } from 'baileys';
import { isURL } from 'class-validator';
import EventEmitter2 from 'eventemitter2';
import { v4 } from 'uuid';
Expand All @@ -15,12 +15,12 @@ import { WebsocketService } from '../integrations/websocket/services/websocket.s
import { RepositoryBroker } from '../repository/repository.manager';
import { AuthService, OldToken } from '../services/auth.service';
import { CacheService } from '../services/cache.service';
import { BaileysStartupService } from '../services/channels/whatsapp.baileys.service';
import { BusinessStartupService } from '../services/channels/whatsapp.business.service';
import { IntegrationService } from '../services/integration.service';
import { WAMonitoringService } from '../services/monitor.service';
import { SettingsService } from '../services/settings.service';
import { WebhookService } from '../services/webhook.service';
import { BaileysStartupService } from '../services/whatsapp/whatsapp.baileys.service';
import { BusinessStartupService } from '../services/whatsapp/whatsapp.business.service';
import { Events, Integration, wa } from '../types/wa.types';
import { ProxyController } from './proxy.controller';

Expand Down Expand Up @@ -65,6 +65,7 @@ export class InstanceController {
chatwoot_reopen_conversation,
chatwoot_conversation_pending,
chatwoot_import_contacts,
chatwoot_name_inbox,
chatwoot_import_messages,
chatwoot_days_limit_import_messages,
reject_call,
Expand Down Expand Up @@ -513,7 +514,7 @@ export class InstanceController {
token: chatwoot_token,
url: chatwoot_url,
sign_msg: chatwoot_sign_msg || false,
name_inbox: instance.instanceName.split('-cwId-')[0],
name_inbox: chatwoot_name_inbox ?? instance.instanceName.split('-cwId-')[0],
number,
reopen_conversation: chatwoot_reopen_conversation || false,
conversation_pending: chatwoot_conversation_pending || false,
Expand Down Expand Up @@ -577,7 +578,7 @@ export class InstanceController {
import_messages: chatwoot_import_messages ?? true,
days_limit_import_messages: chatwoot_days_limit_import_messages || 60,
number,
name_inbox: instance.instanceName,
name_inbox: chatwoot_name_inbox ?? instance.instanceName,
webhook_url: `${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`,
},
};
Expand Down
2 changes: 1 addition & 1 deletion src/api/dto/chat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from '@whiskeysockets/baileys';
import { proto, WAPresence, WAPrivacyOnlineValue, WAPrivacyValue, WAReadReceiptsValue } from 'baileys';

export class OnWhatsAppDto {
constructor(
Expand Down
3 changes: 2 additions & 1 deletion src/api/dto/instance.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WAPresence } from '@whiskeysockets/baileys';
import { WAPresence } from 'baileys';

import { ProxyDto } from './proxy.dto';

Expand Down Expand Up @@ -30,6 +30,7 @@ export class InstanceDto {
chatwoot_import_contacts?: boolean;
chatwoot_import_messages?: boolean;
chatwoot_days_limit_import_messages?: number;
chatwoot_name_inbox?: string;
websocket_enabled?: boolean;
websocket_events?: string[];
rabbitmq_enabled?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/api/dto/sendMessage.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { proto, WAPresence } from '@whiskeysockets/baileys';
import { proto, WAPresence } from 'baileys';

export class Quoted {
key: proto.IMessageKey;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,12 @@ export class ChatwootController {
data.import_messages = false;
data.days_limit_import_messages = 0;
data.auto_create = false;
data.name_inbox = '';
}

data.name_inbox = instance.instanceName;
if (!data.name_inbox || data.name_inbox === '') {
data.name_inbox = instance.instanceName;
}

const result = await this.chatwootService.create(instance, data);

Expand Down
79 changes: 60 additions & 19 deletions src/api/integrations/chatwoot/services/chatwoot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import ChatwootClient, {
} from '@figuro/chatwoot-sdk';
import { request as chatwootRequest } from '@figuro/chatwoot-sdk/dist/core/request';
import axios from 'axios';
import { proto } from 'baileys';
import FormData from 'form-data';
import { createReadStream, unlinkSync, writeFileSync } from 'fs';
import Jimp from 'jimp';
Expand Down Expand Up @@ -85,12 +86,13 @@ export class ChatwootService {
return client;
}

public getClientCwConfig(): ChatwootAPIConfig {
public getClientCwConfig(): ChatwootAPIConfig & { name_inbox: string } {
return {
basePath: this.provider.url,
with_credentials: true,
credentials: 'include',
token: this.provider.token,
name_inbox: this.provider.name_inbox,
};
}

Expand All @@ -110,7 +112,7 @@ export class ChatwootService {

await this.initInstanceChatwoot(
instance,
instance.instanceName.split('-cwId-')[0],
data.name_inbox ?? instance.instanceName.split('-cwId-')[0],
`${urlServer}/chatwoot/webhook/${encodeURIComponent(instance.instanceName)}`,
true,
data.number,
Expand Down Expand Up @@ -628,7 +630,7 @@ export class ChatwootService {
id: contactId,
})) as any;

if (contactConversations) {
if (contactConversations?.payload?.length) {
let conversation: any;
if (this.provider.reopen_conversation) {
conversation = contactConversations.payload.find((conversation) => conversation.inbox_id == filterInbox.id);
Expand Down Expand Up @@ -710,7 +712,7 @@ export class ChatwootService {
}

this.logger.verbose('find inbox by name');
const findByName = inbox.payload.find((inbox) => inbox.name === instance.instanceName.split('-cwId-')[0]);
const findByName = inbox.payload.find((inbox) => inbox.name === this.getClientCwConfig().name_inbox);

if (!findByName) {
this.logger.warn('inbox not found');
Expand Down Expand Up @@ -1106,6 +1108,26 @@ export class ChatwootService {
}
}

public async onSendMessageError(instance: InstanceDto, conversation: number, error?: string) {
const client = await this.clientCw(instance);

if (!client) {
return;
}

client.messages.create({
accountId: this.provider.account_id,
conversationId: conversation,
data: {
content: i18next.t('cw.message.notsent', {
error: error?.length > 0 ? `_${error}_` : '',
}),
message_type: 'outgoing',
private: true,
},
});
}

public async receiveWebhook(instance: InstanceDto, body: any) {
try {
await new Promise((resolve) => setTimeout(resolve, 500));
Expand Down Expand Up @@ -1274,6 +1296,11 @@ export class ChatwootService {
return { message: 'bot' };
}

if (!waInstance && body.conversation?.id) {
this.onSendMessageError(instance, body.conversation?.id, 'Instance not found');
return { message: 'bot' };
}

this.logger.verbose('Format message to send');
let formatText: string;
if (senderName === null || senderName === undefined) {
Expand Down Expand Up @@ -1310,6 +1337,9 @@ export class ChatwootService {
formatText,
options,
);
if (!messageSent && body.conversation?.id) {
this.onSendMessageError(instance, body.conversation?.id);
}

this.updateChatwootMessageId(
{
Expand Down Expand Up @@ -1343,23 +1373,34 @@ export class ChatwootService {
},
};

const messageSent = await waInstance?.textMessage(data, true);
let messageSent: MessageRaw | proto.WebMessageInfo;
try {
messageSent = await waInstance?.textMessage(data, true);
if (!messageSent) {
throw new Error('Message not sent');
}

this.updateChatwootMessageId(
{
...messageSent,
owner: instance.instanceName,
},
{
messageId: body.id,
inboxId: body.inbox?.id,
conversationId: body.conversation?.id,
contactInbox: {
sourceId: body.conversation?.contact_inbox?.source_id,
this.updateChatwootMessageId(
{
...messageSent,
owner: instance.instanceName,
},
},
instance,
);
{
messageId: body.id,
inboxId: body.inbox?.id,
conversationId: body.conversation?.id,
contactInbox: {
sourceId: body.conversation?.contact_inbox?.source_id,
},
},
instance,
);
} catch (error) {
if (!messageSent && body.conversation?.id) {
this.onSendMessageError(instance, body.conversation?.id, error.toString());
}
throw error;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { inbox } from '@figuro/chatwoot-sdk';
import { proto } from '@whiskeysockets/baileys';
import { proto } from 'baileys';

import { InstanceDto } from '../../../../api/dto/instance.dto';
import { ChatwootRaw, ContactRaw, MessageRaw } from '../../../../api/models';
Expand Down
1 change: 1 addition & 0 deletions src/api/integrations/chatwoot/validate/chatwoot.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const chatwootSchema: JSONSchema7 = {
url: { type: 'string' },
sign_msg: { type: 'boolean', enum: [true, false] },
sign_delimiter: { type: ['string', 'null'] },
name_inbox: { type: ['string', 'null'] },
reopen_conversation: { type: 'boolean', enum: [true, false] },
conversation_pending: { type: 'boolean', enum: [true, false] },
auto_create: { type: 'boolean', enum: [true, false] },
Expand Down
2 changes: 1 addition & 1 deletion src/api/services/cache.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BufferJSON } from '@whiskeysockets/baileys';
import { BufferJSON } from 'baileys';

import { Logger } from '../../config/logger.config';
import { ICache } from '../abstract/abstract.cache';
Expand Down
Loading

0 comments on commit 633dbb8

Please sign in to comment.