Skip to content

Commit b8fa432

Browse files
committed
conversion of audios for sending recorded audio, now it is possible to send mp3 audios and not just ogg
1 parent 631dd01 commit b8fa432

File tree

9 files changed

+36
-17
lines changed

9 files changed

+36
-17
lines changed

.DS_Store

6 KB
Binary file not shown.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ lerna-debug.log*
3333
!/instances/.gitkeep
3434
/test/
3535
/src/env.yml
36+
37+
/temp

src/config/env.config.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readFileSync } from 'fs';
22
import { load } from 'js-yaml';
33
import { join } from 'path';
44
import { SRC_DIR } from './path.config';
5+
import { isBooleanString } from 'class-validator';
56

67
export type HttpServer = { TYPE: 'http' | 'https'; PORT: number };
78

@@ -135,7 +136,7 @@ export class ConfigService {
135136
this.env.PRODUCTION = process.env?.NODE_ENV === 'PROD';
136137
if (process.env?.DOCKER_ENV === 'true') {
137138
this.env.SERVER.TYPE = 'http';
138-
this.env.SERVER.PORT = Number.parseInt(process.env?.SERVER_PORT ?? '8080');
139+
this.env.SERVER.PORT = 8080;
139140
}
140141
}
141142

@@ -190,10 +191,9 @@ export class ConfigService {
190191
LEVEL: process.env?.LOG_LEVEL.split(',') as LogLevel[],
191192
COLOR: process.env?.LOG_COLOR === 'true',
192193
},
193-
DEL_INSTANCE:
194-
typeof process.env?.DEL_INSTANCE === 'boolean'
195-
? process.env.DEL_INSTANCE === 'true'
196-
: Number.parseInt(process.env.DEL_INSTANCE),
194+
DEL_INSTANCE: isBooleanString(process.env?.DEL_INSTANCE)
195+
? process.env.DEL_INSTANCE === 'true'
196+
: Number.parseInt(process.env.DEL_INSTANCE),
197197
WEBHOOK: {
198198
GLOBAL: {
199199
URL: process.env?.WEBHOOK_GLOBAL_URL,

src/db/db.connect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Logger } from '../config/logger.config';
44

55
const logger = new Logger('Db Connection');
66

7-
export const db = configService.get<Database>('DATABASE');
7+
const db = configService.get<Database>('DATABASE');
88
export const dbserver = db.ENABLED
99
? mongoose.createConnection(db.CONNECTION.URI, {
1010
dbName: db.CONNECTION.DB_PREFIX_NAME + '-whatsapp-api',

src/db/redis.client.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export class RedisCache {
2525
}
2626
}
2727

28+
public async keyExists(key?: string) {
29+
if (key) {
30+
return !!(await this.instanceKeys()).find((i) => i === key);
31+
}
32+
return !!(await this.instanceKeys()).find((i) => i === this.instanceName);
33+
}
34+
2835
public async writeData(field: string, data: any) {
2936
try {
3037
const json = JSON.stringify(data, BufferJSON.replacer);

src/whatsapp/controllers/instance.controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export class InstanceController {
118118
public async connectToWhatsapp({ instanceName }: InstanceDto) {
119119
try {
120120
const instance = this.waMonitor.waInstances[instanceName];
121-
const state = instance?.connectionStatus?.state ?? null;
121+
const state = instance?.connectionStatus?.state;
122122

123123
switch (state) {
124124
case 'close':
@@ -131,7 +131,7 @@ export class InstanceController {
131131
return await this.connectionState({ instanceName });
132132
}
133133
} catch (error) {
134-
this.logger.log(error);
134+
this.logger.error(error);
135135
}
136136
}
137137

src/whatsapp/guards/instance.guard.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,28 @@ import { NextFunction, Request, Response } from 'express';
22
import { existsSync } from 'fs';
33
import { join } from 'path';
44
import { INSTANCE_DIR } from '../../config/path.config';
5-
import { db, dbserver } from '../../db/db.connect';
5+
import { dbserver } from '../../db/db.connect';
66
import {
77
BadRequestException,
88
ForbiddenException,
99
NotFoundException,
1010
} from '../../exceptions';
1111
import { InstanceDto } from '../dto/instance.dto';
1212
import { waMonitor } from '../whatsapp.module';
13+
import { Database, Redis, configService } from '../../config/env.config';
14+
import { RedisCache } from '../../db/redis.client';
1315

1416
async function getInstance(instanceName: string) {
15-
const exists = waMonitor.waInstances[instanceName];
17+
const db = configService.get<Database>('DATABASE');
18+
const redisConf = configService.get<Redis>('REDIS');
19+
20+
const exists = !!waMonitor.waInstances[instanceName];
21+
22+
if (redisConf.ENABLED) {
23+
const cache = new RedisCache(redisConf, instanceName);
24+
const keyExists = await cache.keyExists();
25+
return exists || keyExists;
26+
}
1627

1728
if (db.ENABLED) {
1829
const collection = dbserver

src/whatsapp/services/whatsapp.service.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,8 @@ export class WAStartupService {
11461146
let outputAudio: string;
11471147

11481148
if (isURL(audio)) {
1149-
outputAudio = `${join(process.cwd(), 'temp', 'audio.opus')}`;
1150-
tempAudioPath = `${join(process.cwd(), 'temp', 'audio.mp3')}`;
1149+
outputAudio = `${join(process.cwd(), 'temp', 'audio.mp4')}`;
1150+
tempAudioPath = `${join(process.cwd(), 'temp', 'audioTemp.mp3')}`;
11511151

11521152
const response = await axios.get(audio, { responseType: 'arraybuffer' });
11531153
fs.writeFileSync(tempAudioPath, response.data);
@@ -1161,7 +1161,8 @@ export class WAStartupService {
11611161

11621162
return new Promise((resolve, reject) => {
11631163
exec(
1164-
`${ffmpegPath.path} -i ${tempAudioPath} -c:a libopus ${outputAudio} -y`,
1164+
// `${ffmpegPath.path} -i ${tempAudioPath} -c:a libopus ${outputAudio} -y`,
1165+
`${ffmpegPath.path} -i ${tempAudioPath} -vn -ab 128k -ar 44100 -f ipod ${outputAudio} -y`,
11651166
(error, _stdout, _stderr) => {
11661167
fs.unlinkSync(tempAudioPath);
11671168
if (error) reject(error);
@@ -1179,11 +1180,9 @@ export class WAStartupService {
11791180
data.number,
11801181
{
11811182
audio: Buffer.from(audio, 'base64'),
1182-
// audio: isURL(data.audioMessage.audio)
1183-
// ? { url: data.audioMessage.audio }
1184-
// : Buffer.from(data.audioMessage.audio, 'base64'),
11851183
ptt: true,
1186-
mimetype: 'audio/ogg; codecs=opus',
1184+
// mimetype: 'audio/ogg; codecs=opus',
1185+
mimetype: 'audio/mp4',
11871186
},
11881187
{ presence: 'recording', delay: data?.options?.delay },
11891188
);

temp/audio.opus

-46.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)