community-relations/src/pbx/actions/Misc.ts

45 lines
1.6 KiB
TypeScript

import ARI from 'ari-client';
import { randomBytes } from 'crypto';
import { promises as fs } from 'fs';
import { PBX } from '../../class';
export default class Misc {
public static accessDenied(channel: ARI.Channel) {
return new Promise<void>((resolve, reject) => {
channel.play({
media: 'sound:access-denied',
}, undefined).then((playback) => {
playback.on('PlaybackFinished', () => {
channel.hangup().catch((err) => reject(err));
resolve();
});
}).catch((err) => reject(err));
});
}
public static async TTS(pbx: PBX, text: string, gender: string | any): Promise<string> {
const fileExtension = `${randomBytes(10).toString('hex')}`;
const [response] = await pbx.tts.synthesizeSpeech({
input: { text },
voice: { languageCode: 'en-US', ssmlGender: gender },
audioConfig: { audioEncoding: 'OGG_OPUS' },
});
await fs.writeFile(`/tmp/${fileExtension}.ogg`, response.audioContent, 'binary');
await pbx.client.util.exec(`ffmpeg -i /tmp/${fileExtension}.ogg -af "highpass=f=300, lowpass=f=3400" -ar 8000 -ac 1 -ab 64k -f mulaw /tmp/${fileExtension}.ulaw`);
return `sound:/tmp/${fileExtension}`;
}
public static play(pbx: PBX, channel: ARI.Channel, sound: string | string[]): Promise<ARI.Playback> {
const playback = pbx.ari.Playback();
return new Promise((resolve, reject) => {
playback.once('PlaybackFinished', (_, pl: ARI.Playback) => {
resolve(pl);
});
channel.play({ media: sound }, playback).catch((err) => reject(err));
});
}
}