All Products
Search
Document Center

Node.js Demo

Last Updated: May 18, 2020

Note:

  • Read API reference before using the SDK.
  • The SDK in this topic applies only to Intelligent Speech Interaction V2.0 that Alibaba Cloud published on July 5, 2018. If you have activated an earlier version of Intelligent Speech Interaction, activate Intelligent Speech Interaction V2.0 before using the SDK.

SDK instructions

The Node.js demo of the recording file recognition service uses the Alibaba Cloud SDK for Node.js to send pctowap open platform (POP) API requests in a remote procedure call (RPC) style. You can send a recording file recognition request and query the recording file recognition result. For more information about the Alibaba Cloud SDK for Node.js, see Use Node.js SDK in Node.js SDK Developer Guide.

SDK installation

Note: The Alibaba Cloud SDK for Node.js supports the latest long term support (LTS) versions: Node.js 4.x and Node.js 6.x. You can run the node -v command to view the current Node.js version.

Use npm to install dependent modules for Node.js

All official Node.js SDKs of Alibaba Cloud are stored in the @alicloud directory. The Node.js demo of the recording file recognition service is dependent on @alicloud/nls-filetrans-2018-08-17. In the directory of demo files, run the following command to install the dependent module:

  1. npm install @alicloud/nls-filetrans-2018-08-17 --save

Call procedure

  1. Create and initialize a client. Use the AccessKey ID and AccessKey secret of your Alibaba Cloud account to authenticate the client. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.
  2. Create a recording file recognition request and set request parameters. Send the recording file recognition request and process the response returned by the server to obtain the task ID.
  3. Create a query request for the recording file recognition result and specify the task ID. Poll the recognition result.

Demo instructions

Note: The recording file in the demo is a pulse-code modulation (PCM) file sampled at 16 kHz. The demo uses a universal model in the console. If you use your own recording file, specify its audio coding format and audio sampling rate and select an appropriate model in the console. For more information about how to select a model, see Manage projects in Console User Guide.

nls-sample-16k.wav


Alibaba Cloud authentication:When using the SDK, you need to use your Alibaba Cloud account to authenticate the client for all API operations. You need to specify the AccessKey ID and AccessKey secret of your Alibaba Cloud account. For more information about how to obtain your AccessKey information, see Activate Intelligent Speech Interaction in Quick Start.

Sample code

  1. 'use strict';
  2. const Client = require('@alicloud/nls-filetrans-2018-08-17');
  3. function fileTrans(akID, akSecret, appKey, fileLink) {
  4. /**
  5. * Specify the region ID
  6. * and other constant parameters. Do not modify their values.
  7. */
  8. var ENDPOINT = 'http://filetrans.ap-southeast-1.aliyuncs.com';
  9. var API_VERSION = '2019-08-23';
  10. /**
  11. * Create and authenticate a client.
  12. */
  13. var client = new Client({
  14. accessKeyId: akID,
  15. secretAccessKey: akSecret,
  16. endpoint: ENDPOINT,
  17. apiVersion: API_VERSION
  18. });
  19. client.apiVersion = API_VERSION;
  20. /**
  21. * Create a recording file recognition request and set request parameters.
  22. * appkey: the appkey of your project.
  23. * file_link: the URL of the recording file.
  24. * task: the request body, which includes request parameters in a JSON-formatted string.
  25. */
  26. var task = {
  27. appkey : appKey,
  28. file_link : fileLink,
  29. version : "4.0", // The version of the recording file recognition service. If you are a new user, set this parameter to 4.0. If you use the default version 2.0, comment out this parameter.
  30. enable_words : false // Specifies whether to return the recognition results of words. Default value: false. This parameter takes effect only when the version of the recording file recognition service is 4.0.
  31. };
  32. task = JSON.stringify(task);
  33. var taskParams = {
  34. Task : task
  35. };
  36. var options = {
  37. method: 'POST'
  38. };
  39. // Send the recording file recognition request and process the response returned by the server.
  40. client.submitTask(taskParams, options).then((response) => {
  41. console.log(response);
  42. // The status message in the response returned by the server.
  43. var statusText = response.StatusText;
  44. if (statusText != 'SUCCESS') {
  45. console.log('The recording file recognition request fails.')
  46. return;
  47. }
  48. console.log('The recording file recognition request is successful.') ;
  49. // Obtain the ID of the recording file recognition task. You can use the task ID to query the recognition result.
  50. var taskId = response.TaskId;
  51. /**
  52. * Create a query request for the recording file recognition result and specify the task ID.
  53. * Use the polling method to query the recognition result. The polling runs until the status message that the server returns is SUCCESS, SUCCESS_WITH_NO_VALID_FRAGMENT,
  54. * or an error message.
  55. */
  56. var taskIdParams = {
  57. TaskId : taskId
  58. };
  59. var timer = setInterval(() => {
  60. client.getTaskResult(taskIdParams).then((response) => {
  61. console.log('Response to the query request for the recording file recognition result: ');
  62. console.log(response);
  63. var statusText = response.StatusText;
  64. if (statusText == 'RUNNING' || statusText == 'QUEUEING') {
  65. // Continue the polling. Note that the polling interval.
  66. }
  67. else {
  68. if (statusText == 'SUCCESS' || statusText == 'SUCCESS_WITH_NO_VALID_FRAGMENT') {
  69. console.log('The recording file is recognized.');
  70. var sentences = response.Result;
  71. console.log(sentences);
  72. }
  73. else {
  74. console.log('Failed to recognize the recording file.') ;
  75. }
  76. // End the polling.
  77. clearInterval(timer);
  78. }
  79. }).catch((error) => {
  80. console.error(error);
  81. // End the polling due to an error.
  82. clearInterval(timer);
  83. });
  84. }, 10000);
  85. }).catch((error) => {
  86. console.error(error);
  87. });
  88. }
  89. var akId = 'Your AccessKey ID';
  90. var akSecret = 'Your AccessKey secret';
  91. var appKey = 'Your appkey';
  92. var fileLink = 'https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav';
  93. fileTrans(akId, akSecret, appKey, fileLink);