All Products
Search
Document Center

GO 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 Go demo of the recording file recognition service uses the CommonRequest method of the Alibaba Cloud SDK for Go 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 Go, see Use Go SDK in Go SDK. For more information about how to use the CommonRequest method of the Go SDK, see Use CommonRequest in Go SDK.

SDK installation

The Alibaba Cloud SDK for Go supports Go 1.7 or later. You can use either of the following methods to install the Go SDK.

  1. glide get github.com/aliyun/alibaba-cloud-sdk-go

Use govendor

Run the following command to install the Go SDK:

  1. go get -u github.com/aliyun/alibaba-cloud-sdk-go/sdk

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.
  3. Send the recording file recognition request and process the response returned by the server to obtain the task ID.
  4. Create a query request for the recording file recognition result and specify the task ID.
  5. 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. package main
  2. import (
  3. "github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
  4. "github.com/aliyun/alibaba-cloud-sdk-go/sdk"
  5. "fmt"
  6. "encoding/json"
  7. "time"
  8. )
  9. func main() {
  10. // The constant parameters, such as the region ID. Do not modify their values.
  11. const REGION_ID string = "ap-southeast-1"
  12. const ENDPOINT_NAME string = "ap-southeast-1"
  13. const PRODUCT string = "nls-filetrans"
  14. const DOMAIN string = "filetrans.ap-southeast-1.aliyuncs.com"
  15. const API_VERSION string = "2019-08-23"
  16. const POST_REQUEST_ACTION string = "SubmitTask"
  17. const GET_REQUEST_ACTION string = "GetTaskResult"
  18. // The keys of request parameters.
  19. const KEY_APP_KEY string = "appkey"
  20. const KEY_FILE_LINK string = "file_link"
  21. const KEY_VERSION string = "version"
  22. const KEY_ENABLE_WORDS string = "enable_words"
  23. // The keys of response parameters.
  24. const KEY_TASK string = "Task"
  25. const KEY_TASK_ID string = "TaskId"
  26. const KEY_STATUS_TEXT string = "StatusText"
  27. const KEY_RESULT string = "Result"
  28. // The status values.
  29. const STATUS_SUCCESS string = "SUCCESS"
  30. const STATUS_RUNNING string = "RUNNING"
  31. const STATUS_QUEUEING string = "QUEUEING"
  32. var accessKeyId string = "Your AccessKey ID"
  33. var accessKeySecret string = "Your AccessKey secret"
  34. var appKey string = "Your appkey"
  35. var fileLink string = "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
  36. client, err := sdk.NewClientWithAccessKey(REGION_ID, accessKeyId, accessKeySecret)
  37. if err != nil {
  38. panic(err)
  39. }
  40. postRequest := requests.NewCommonRequest()
  41. postRequest.Domain = DOMAIN
  42. postRequest.Version = API_VERSION
  43. postRequest.Product = PRODUCT
  44. postRequest.ApiName = POST_REQUEST_ACTION
  45. postRequest.Method = "POST"
  46. mapTask := make(map[string]string)
  47. mapTask[KEY_APP_KEY] = appKey
  48. mapTask[KEY_FILE_LINK] = fileLink
  49. // Specify 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.
  50. mapTask[KEY_VERSION] = "4.0"
  51. // Specify 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.
  52. mapTask[KEY_ENABLE_WORDS] = "false"
  53. task, err := json.Marshal(mapTask)
  54. if err != nil {
  55. panic(err)
  56. }
  57. postRequest.FormParams[KEY_TASK] = string(task)
  58. postResponse, err := client.ProcessCommonRequest(postRequest)
  59. if err != nil {
  60. panic(err)
  61. }
  62. postResponseContent := postResponse.GetHttpContentString()
  63. fmt.Println(postResponseContent)
  64. if (postResponse.GetHttpStatus() != 200) {
  65. fmt.Println("The recording file recognition request fails. HTTP status code: ", postResponse.GetHttpStatus())
  66. return
  67. }
  68. var postMapResult map[string]interface{}
  69. err = json.Unmarshal([]byte(postResponseContent), &postMapResult)
  70. if err != nil {
  71. panic(err)
  72. }
  73. var taskId string = ""
  74. var statusText string = ""
  75. statusText = postMapResult[KEY_STATUS_TEXT].(string)
  76. if statusText == STATUS_SUCCESS {
  77. fmt.Println("The recording file recognition request is successful.")
  78. taskId = postMapResult[KEY_TASK_ID].(string)
  79. } else {
  80. fmt.Println("The recording file recognition request fails.")
  81. return
  82. }
  83. getRequest := requests.NewCommonRequest()
  84. getRequest.Domain = DOMAIN
  85. getRequest.Version = API_VERSION
  86. getRequest.Product = PRODUCT
  87. getRequest.ApiName = GET_REQUEST_ACTION
  88. getRequest.Method = "GET"
  89. getRequest.QueryParams[KEY_TASK_ID] = taskId
  90. statusText = ""
  91. for true {
  92. getResponse, err := client.ProcessCommonRequest(getRequest)
  93. if err != nil {
  94. panic(err)
  95. }
  96. getResponseContent := getResponse.GetHttpContentString()
  97. fmt.Println("Queried recording file recognition result: ", getResponseContent)
  98. if (getResponse.GetHttpStatus() != 200) {
  99. fmt.Println("Failed to query the recording file recognition result. HTTP status code: ", getResponse.GetHttpStatus())
  100. break
  101. }
  102. var getMapResult map[string]interface{}
  103. err = json.Unmarshal([]byte(getResponseContent), &getMapResult)
  104. if err != nil {
  105. panic(err)
  106. }
  107. statusText = getMapResult[KEY_STATUS_TEXT].(string)
  108. if statusText == STATUS_RUNNING || statusText == STATUS_QUEUEING {
  109. time.Sleep(10 * time.Second)
  110. } else {
  111. break
  112. }
  113. }
  114. if statusText == STATUS_SUCCESS {
  115. fmt.Println("The recording file is recognized.")
  116. } else {
  117. fmt.Println("Failed to recognize the recording file.")
  118. }
  119. }