Node.js demo
This topic describes how to install and use the Node.js SDK for Alibaba Cloud Voice Service and provides code examples.
Prerequisites
Before you use the SDK, read the API reference. For more information, see API reference.
Activate Intelligent Speech Interaction and obtain an AccessKey ID and AccessKey secret. For more information, see Get started.
Example description
The audio file transcription example uses the Node.js SDK to submit transcription requests and query the results. This example uses the POP RPC API call method.
For more information about the Alibaba Cloud Node.js SDK, see Quick Start.
SDK installation
The Alibaba Cloud Node.js SDK supports Node.js Long-Term Support (LTS) versions 4.x and 6.x. You can run the node -v command to view your Node.js version.
The Node.js example for audio file transcription depends on the aliyun-nls-filetrans package. In the directory of the example file, run the following command to install the dependency:
npm install aliyun-nls-filetrans --saveProcedure
Create and initialize an Alibaba Cloud authentication object.
Set the request parameters, submit an audio file transcription request, process the server-side response, and obtain the task ID.
Set the query parameter to the task ID and use polling to query the transcription result for the task.
Code example
Download nls-sample-16k.wav. This audio file uses the PCM encoding format and has a sample rate of 16000 Hz. For this example, the model in the console is set to the general-purpose model. If you use a different audio file, you must specify its encoding format and sample rate, and set the corresponding model in the console. For more information about model settings, see Manage projects.
Before you call the API, configure environment variables to store your access credentials. The environment variables are ALIYUN_AK_ID for your AccessKey ID, ALIYUN_AK_SECRET for your AccessKey secret, and NLS_APP_KEY for the AppKey of Intelligent Speech Interaction.
'use strict';
const Client = require('aliyun-nls-filetrans');
function fileTrans(akID, akSecret, appKey, fileLink) {
// The region ID. This is a static field.
var ENDPOINT = 'http://speechfiletranscriberlite.cn-shanghai.aliyuncs.com';
var API_VERSION = '2021-12-21';
/**
* Create an Alibaba Cloud authentication client.
*/
var client = new Client({
accessKeyId: akID,
secretAccessKey: akSecret,
endpoint: ENDPOINT,
apiVersion: API_VERSION
});
/**
* Submit an audio file transcription request. The request parameters are combined into a JSON-formatted string as the value of the task.
* Request parameter appkey: The AppKey of your project. To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist
* Request parameter file_link: The audio file to be transcribed.
*/
var task = {
appkey : appKey,
file_link : fileLink,
enable_words : false // Specifies whether to return word-level timestamps. The default value is false.
};
task = JSON.stringify(task);
var taskParams = {
Task : task
};
var options = {
method: 'POST'
};
// Submit the audio file transcription request and process the server-side response.
client.submitTask(taskParams, options).then((response) => {
console.log(response);
// The status description StatusText of the server-side response.
var statusText = response.StatusText;
if (statusText != 'SUCCESS') {
console.log('The audio file transcription request failed!')
return;
}
console.log('The audio file transcription request was successful!');
// Obtain the TaskId of the audio file transcription task to query the transcription result.
var taskId = response.TaskId;
/**
* Submit a request to query the transcription result with TaskId as a query parameter.
* Use polling to query the transcription result until the server-side returns a status of "SUCCESS", "SUCCESS_WITH_NO_VALID_FRAGMENT",
* or an error. Then, stop polling.
*/
var taskIdParams = {
TaskId : taskId
};
var timer = setInterval(() => {
client.getTaskResult(taskIdParams).then((response) => {
console.log('Response to the transcription result query:');
console.log(response);
var statusText = response.StatusText;
if (statusText == 'RUNNING' || statusText == 'QUEUEING') {
// Continue polling. Note the interval.
}
else {
if (statusText == 'SUCCESS' || statusText == 'SUCCESS_WITH_NO_VALID_FRAGMENT') {
console.log('Audio file transcription successful:');
var sentences = response.Result;
console.log(sentences);
}
else {
console.log('Audio file transcription failed!');
}
// Stop polling.
clearInterval(timer);
}
}).catch((error) => {
console.error(error);
// An exception occurred. Stop polling.
clearInterval(timer);
});
}, 10000);
}).catch((error) => {
console.error(error);
});
}
var akId = process.env.ALIYUN_AK_ID;
var akSecret = process.env.ALIYUN_AK_SECRET;
var appKey = process.env.NLS_APP_KEY;
var fileLink = 'https://gw.alipayobjects.com/os/bmw-prod/0574ee2e-f494-45a5-820f-63aee583045a.wav';
fileTrans(akId, akSecret, appKey, fileLink);