This topic describes how to install and use the Node.js software development kit (SDK) for Alibaba Cloud Intelligent Speech Interaction and provides code examples.
Prerequisites
Before you use the software development kit (SDK), review 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 recording file recognition example uses the Node.js SDK to submit a recognition request and query the result. This example uses Platform Open API (POP) calls in the remote procedure call (RPC) style.
For more information about the Alibaba Cloud SDK for Node.js, see Quick start.
SDK installation
The Alibaba Cloud SDK for Node.js supports the Long-Term Support (LTS) versions of Node.js 4.x and 6.x. You can run the node -v command to check your Node.js version.
All official Alibaba Cloud SDKs for Node.js are in the @alicloud directory. The Node.js example for recording file recognition depends on @alicloud/nls-filetrans-2018-08-17. In the directory that contains the example file, run the following command to install the Node.js dependency:
npm install @alicloud/nls-filetrans-2018-08-17 --saveCall procedure
Create and initialize an Alibaba Cloud authentication object.
Set request parameters, submit a recording file recognition request, process the server-side response, and obtain the task ID.
Set the task ID as a query parameter and poll for the recognition result of the task.
Code example
Download nls-sample-16k.wav. This recording is in Pulse-Code Modulation (PCM) format with a 16000 Hz sample rate. The model set in the console is the general-purpose model. If you use a different recording, you must specify the corresponding encoding format and sample rate, and set the corresponding model in the console. For more information about setting models, see Manage projects.
Before you call the API, configure environment variables to load your access credentials. The environment variable names for the AccessKey ID, AccessKey secret, and AppKey are ALIYUN_AK_ID, ALIYUN_AK_SECRET, and NLS_APP_KEY.
'use strict';
const Client = require('@alicloud/nls-filetrans-2018-08-17');
function fileTrans(akID, akSecret, appKey, fileLink) {
// The region ID. This is a static field.
var ENDPOINT = 'http://filetrans.cn-shanghai.aliyuncs.com';
var API_VERSION = '2018-08-17';
/**
* Create an Alibaba Cloud authentication client.
*/
var client = new Client({
accessKeyId: akID, // To obtain an AccessKey ID and AccessKey secret, go to the console: https://ram.console.aliyun.com/manage/ak
secretAccessKey: akSecret,
endpoint: ENDPOINT,
apiVersion: API_VERSION
});
/**
* Submit a recording recognition request. Combine the request parameters into a JSON string as the value of task.
* appkey: The AppKey of the project. To obtain an AppKey, go to the console: https://nls-portal.console.aliyun.com/applist
* file_link: The recording to recognize.
*/
var task = {
appkey : appKey,
file_link : fileLink,
version : "4.0", // If you are a new user, use version 4.0. If you are an existing user and want to keep the default version 2.0, comment out this parameter setting.
enable_words : false // Specifies whether to output word-level information. The default value is false. To enable this feature, you must set the version parameter to 4.0.
};
task = JSON.stringify(task);
var taskParams = {
Task : task
};
var options = {
method: 'POST'
};
// Submit the recording recognition request and process the server-side response.
client.submitTask(taskParams, options).then((response) => {
console.log(response);
// The status description (StatusText) in the server-side response.
var statusText = response.StatusText;
if (statusText != 'SUCCESS') {
console.log('The recording recognition request failed!')
return;
}
console.log('The recording recognition request succeeded!');
// Get the TaskId of the recording recognition task to use for querying the result.
var taskId = response.TaskId;
/**
* Use the TaskId as a query parameter to submit a request to query the recognition result.
* Poll for the recognition result until the server-side returns a status of "SUCCESS", "SUCCESS_WITH_NO_VALID_FRAGMENT",
* or an error description. Then, stop polling.
*/
var taskIdParams = {
TaskId : taskId
};
var timer = setInterval(() => {
client.getTaskResult(taskIdParams).then((response) => {
console.log('Query response for the recognition result:');
console.log(response);
var statusText = response.StatusText;
if (statusText == 'RUNNING' || statusText == 'QUEUEING') {
// Continue polling. Note the polling interval.
}
else {
if (statusText == 'SUCCESS' || statusText == 'SUCCESS_WITH_NO_VALID_FRAGMENT') {
console.log('Recording recognized successfully:');
var sentences = response.Result;
console.log(sentences);
}
else {
console.log('Recording recognition 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);