File size: 4,950 Bytes
b62a170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc7a493
b62a170
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fc7a493
b62a170
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';

import { parseBool } from './helpers';
import { WAHAEngine } from './structures/enums.dto';
import { WebhookConfig } from './structures/webhooks.config.dto';
import { getEngineName } from './version';

@Injectable()
export class WhatsappConfigService {
  public files_uri = '/api/files';
  public schema = 'http';

  constructor(private configService: ConfigService) {}

  get filesURL(): string {
    return `${this.schema}://${this.hostname}:${this.port}${this.files_uri}/`;
  }

  get hostname(): string {
    return this.configService.get('WHATSAPP_API_HOSTNAME', 'localhost');
  }

  get port(): string {
    return this.configService.get('WHATSAPP_API_PORT', '7860');
  }

  get filesFolder(): string {
    return this.configService.get(
      'WHATSAPP_FILES_FOLDER',
      '/tmp/whatsapp-files',
    );
  }

  get filesLifetime(): number {
    return this.configService.get<number>('WHATSAPP_FILES_LIFETIME', 180);
  }

  get mimetypes(): string[] {
    if (!this.shouldDownloadMedia) {
      return ['mimetype/ignore-all-media'];
    }
    const types = this.configService.get('WHATSAPP_FILES_MIMETYPES', '');
    return types ? types.split(',') : [];
  }

  get shouldDownloadMedia(): boolean {
    const value = this.configService.get('WHATSAPP_DOWNLOAD_MEDIA', 'true');
    return parseBool(value);
  }

  get startSessions(): string[] {
    const value: string = this.configService.get('WHATSAPP_START_SESSION', '');
    if (!value) {
      return [];
    }
    return value.split(',');
  }

  get shouldRestartAllSessions(): boolean {
    const value: string = this.configService.get(
      'WHATSAPP_RESTART_ALL_SESSIONS',
      'false',
    );
    return parseBool(value);
  }

  get proxyServer(): string[] | string | undefined {
    const single = this.configService.get('WHATSAPP_PROXY_SERVER', undefined);
    const multipleValues = this.configService.get(
      'WHATSAPP_PROXY_SERVER_LIST',
      undefined,
    );
    const multiple = multipleValues ? multipleValues.split(',') : undefined;
    return single ? single : multiple;
  }

  get proxyServerIndexPrefix(): string | undefined {
    return this.configService.get(
      'WHATSAPP_PROXY_SERVER_INDEX_PREFIX',
      undefined,
    );
  }

  get proxyServerUsername(): string | undefined {
    return this.configService.get('WHATSAPP_PROXY_SERVER_USERNAME', undefined);
  }

  get proxyServerPassword(): string | undefined {
    return this.configService.get('WHATSAPP_PROXY_SERVER_PASSWORD', undefined);
  }

  getWebhookConfig(): WebhookConfig | undefined {
    const url = this.getWebhookUrl();
    const events = this.getWebhookEvents();
    if (!url || events.length === 0) {
      return undefined;
    }
    return { url: url, events: events };
  }

  private getWebhookUrl(): string | undefined {
    return this.get('WHATSAPP_HOOK_URL');
  }

  private getWebhookEvents(): string[] {
    const value = this.get('WHATSAPP_HOOK_EVENTS', '');
    return value ? value.split(',') : [];
  }

  getSessionMongoUrl(): string | undefined {
    return this.configService.get('WHATSAPP_SESSIONS_MONGO_URL', undefined);
  }

  getDefaultEngineName(): WAHAEngine {
    const value = getEngineName();
    if (value in WAHAEngine) {
      return WAHAEngine[value];
    }
    console.log(
      `Unknown WhatsApp default engine WHATSAPP_DEFAULT_ENGINE=${value}. Using WEBJS`,
    );
    return WAHAEngine.WEBJS;
  }

  get(name: string, defaultValue: any = undefined): any {
    return this.configService.get(name, defaultValue);
  }

  getApiKey(): string | undefined {
    return this.configService.get('WHATSAPP_API_KEY', '');
  }

  getSwaggerEnabled(): boolean {
    const value = this.configService.get('WHATSAPP_SWAGGER_ENABLED', 'true');
    return parseBool(value);
  }

  getSwaggerUsernamePassword(): [string, string] | undefined {
    const user = this.configService.get('WHATSAPP_SWAGGER_USERNAME', undefined);
    const password = this.configService.get(
      'WHATSAPP_SWAGGER_PASSWORD',
      undefined,
    );
    if (!user && !password) {
      console.log(
        'Please set up both WHATSAPP_SWAGGER_USERNAME and WHATSAPP_SWAGGER_PASSWORD ' +
          'to enable swagger authentication.',
      );
      return undefined;
    }
    return [user, password];
  }

  getSwaggerAdvancedConfigEnabled(): boolean {
    return this.configService.get('WHATSAPP_SWAGGER_CONFIG_ADVANCED', false);
  }

  getHealthMediaFilesThreshold(): number {
    return this.configService.get<number>(
      'WHATSAPP_HEALTH_MEDIA_FILES_THRESHOLD_MB',
      100,
    );
  }

  getHealthSessionFilesThreshold(): number {
    return this.configService.get<number>(
      'WHATSAPP_HEALTH_SESSION_FILES_THRESHOLD_MB',
      100,
    );
  }

  getHealthMongoTimeout(): number {
    return this.configService.get<number>(
      'WHATSAPP_HEALTH_MONGO_TIMEOUT_MS',
      7860,
    );
  }
}