Skip to content

Instantly share code, notes, and snippets.

@gufranco
Created February 13, 2021 20:00
Show Gist options
  • Save gufranco/815d7bb8215772b2a82c19647bad8633 to your computer and use it in GitHub Desktop.
Save gufranco/815d7bb8215772b2a82c19647bad8633 to your computer and use it in GitHub Desktop.
import HttpService from './HttpService';
export default class WitService {
constructor(private readonly httpService: HttpService) {
['WIT_API_URL', 'WIP_API_TOKEN'].forEach((environmentVar) => {
if (!process.env[environmentVar]) {
throw new Error(`Missing ${environmentVar} environment var`);
}
});
}
public async getTextFromSpeech(
content: string,
type: 'audio/wav' | 'audio/mpeg3' | 'audio/ogg' | 'audio/ulaw' | 'audio/raw'
): Promise<string> {
const response = await this.httpService.post(
`${process.env.WIT_API_URL}/speech`,
Buffer.from(content, 'base64'),
{
params: {
v: '20200513',
},
headers: {
Authorization: `Bearer ${process.env.WIP_API_TOKEN}`,
'Content-Type': type,
},
}
);
return response.data.text;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment