Created
February 13, 2021 20:00
-
-
Save gufranco/815d7bb8215772b2a82c19647bad8633 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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