Skip to content

Commit d8ca480

Browse files
Merge pull request #395 from jaison-x/import-messages-chatwoot
feat(chatwoot): import history messages to chatwoot on whatsapp connection
2 parents 803b123 + 1f6535d commit d8ca480

23 files changed

+987
-105
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"node-mime-types": "^1.1.0",
7777
"node-windows": "^1.0.0-beta.8",
7878
"parse-bmfont-xml": "^1.1.4",
79+
"pg": "^8.11.3",
7980
"pino": "^8.11.0",
8081
"qrcode": "^1.5.1",
8182
"qrcode-terminal": "^0.12.0",
@@ -112,4 +113,4 @@
112113
"ts-node-dev": "^2.0.0",
113114
"typescript": "^4.9.5"
114115
}
115-
}
116+
}

src/config/env.config.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,18 @@ export type Webhook = { GLOBAL?: GlobalWebhook; EVENTS: EventsWebhook };
149149
export type ConfigSessionPhone = { CLIENT: string; NAME: string };
150150
export type QrCode = { LIMIT: number; COLOR: string };
151151
export type Typebot = { API_VERSION: string; KEEP_OPEN: boolean };
152-
export type ChatWoot = { MESSAGE_DELETE: boolean };
152+
export type Chatwoot = {
153+
MESSAGE_DELETE: boolean;
154+
IMPORT: {
155+
DATABASE: {
156+
CONNECTION: {
157+
URI: string;
158+
};
159+
};
160+
PLACEHOLDER_MEDIA_MESSAGE: boolean;
161+
};
162+
};
163+
153164
export type CacheConf = { REDIS: CacheConfRedis; LOCAL: CacheConfLocal };
154165
export type Production = boolean;
155166

@@ -171,7 +182,7 @@ export interface Env {
171182
CONFIG_SESSION_PHONE: ConfigSessionPhone;
172183
QRCODE: QrCode;
173184
TYPEBOT: Typebot;
174-
CHATWOOT: ChatWoot;
185+
CHATWOOT: Chatwoot;
175186
CACHE: CacheConf;
176187
AUTHENTICATION: Auth;
177188
PRODUCTION?: Production;
@@ -338,6 +349,14 @@ export class ConfigService {
338349
},
339350
CHATWOOT: {
340351
MESSAGE_DELETE: process.env.CHATWOOT_MESSAGE_DELETE === 'false',
352+
IMPORT: {
353+
DATABASE: {
354+
CONNECTION: {
355+
URI: process.env.CHATWOOT_DATABASE_CONNECTION_URI || '',
356+
},
357+
},
358+
PLACEHOLDER_MEDIA_MESSAGE: process.env?.CHATWOOT_IMPORT_PLACEHOLDER_MEDIA_MESSAGE === 'true',
359+
},
341360
},
342361
CACHE: {
343362
REDIS: {

src/dev-env.yml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,16 @@ TYPEBOT:
153153
API_VERSION: 'old' # old | latest
154154
KEEP_OPEN: false
155155

156-
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
157156
CHATWOOT:
157+
# If you leave this option as false, when deleting the message for everyone on WhatsApp, it will not be deleted on Chatwoot.
158158
MESSAGE_DELETE: true # false | true
159-
159+
IMPORT:
160+
# This db connection is used to import messages from whatsapp to chatwoot database
161+
DATABASE:
162+
CONNECTION:
163+
URI: "postgres://user:password@hostname:port/dbname"
164+
PLACEHOLDER_MEDIA_MESSAGE: true
165+
160166
# Cache to optimize application performance
161167
CACHE:
162168
REDIS:

src/docs/swagger.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,6 +2076,9 @@ paths:
20762076
read_status:
20772077
type: boolean
20782078
description: "Indicates whether to mark status messages as read."
2079+
sync_full_history:
2080+
type: boolean
2081+
description: "Indicates whether to request a full history messages sync on connect."
20792082
parameters:
20802083
- name: instanceName
20812084
in: path
@@ -2141,6 +2144,15 @@ paths:
21412144
conversation_pending:
21422145
type: boolean
21432146
description: "Indicates whether to mark conversations as pending."
2147+
import_contacts:
2148+
type: boolean
2149+
description: "Indicates whether to import contacts from phone to Chatwoot when connecting."
2150+
import_messages:
2151+
type: boolean
2152+
description: "Indicates whether to import messages from phone to Chatwoot when connecting."
2153+
days_limit_import_messages:
2154+
type: number
2155+
description: "Indicates number of days to limit messages imported to Chatwoot."
21442156
parameters:
21452157
- name: instanceName
21462158
in: path

src/libs/postgres.client.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import postgresql from 'pg';
2+
3+
import { Chatwoot, configService } from '../config/env.config';
4+
import { Logger } from '../config/logger.config';
5+
6+
const { Pool } = postgresql;
7+
8+
class Postgres {
9+
private logger = new Logger(Postgres.name);
10+
private pool;
11+
private connected = false;
12+
13+
getConnection(connectionString: string) {
14+
if (this.connected) {
15+
return this.pool;
16+
} else {
17+
this.pool = new Pool({
18+
connectionString,
19+
ssl: {
20+
rejectUnauthorized: false,
21+
},
22+
});
23+
24+
this.pool.on('error', () => {
25+
this.logger.error('postgres disconnected');
26+
this.connected = false;
27+
});
28+
29+
try {
30+
this.logger.verbose('connecting new postgres');
31+
this.connected = true;
32+
} catch (e) {
33+
this.connected = false;
34+
this.logger.error('postgres connect exception caught: ' + e);
35+
return null;
36+
}
37+
38+
return this.pool;
39+
}
40+
}
41+
42+
getChatwootConnection() {
43+
const uri = configService.get<Chatwoot>('CHATWOOT').IMPORT.DATABASE.CONNECTION.URI;
44+
45+
return this.getConnection(uri);
46+
}
47+
}
48+
49+
export const postgresClient = new Postgres();

0 commit comments

Comments
 (0)