WeChat mini program

更新时间:
复制 MD 格式

This topic describes how to use the WeChat mini program software development kit (SDK) for short sentence recognition in Intelligent Speech Interaction and provides installation instructions and code examples.

Prerequisites

  • Before you use the SDK, read the API reference. For more information, see API reference.

  • The WeChat base library must be version 2.4.4 or later.

  • Make sure that you have installed the WeChat mini program development environment and completed the basic configuration. For more information, see WeChat DevTools.

  • Add the following URLs to the server domain name configuration in your WeChat mini program backend:

    • Valid request domain names: https://nls-meta.cn-shanghai.aliyuncs.com

    • Valid socket domain names: wss://nls-gateway-cn-shanghai.aliyuncs.com

Download and install

  1. Download and install the SDK.

    You can download the SDK code from Github or download the alibabacloud-nls-wx-sdk-master.zip file directly.

  2. Import the SDK.

    Copy the downloaded code to a folder in your project. Then, import the SDK using `require` and specifying the folder path.

Get a token

getToken

This method obtains a token and caches it using the AKID (AccessKey ID) and AKKEY (AccessKey Secret) as keys. If the cached token expires, the method automatically refreshes the token. For more information about the caching mechanism, see the Data Caching topic in the WeChat Mini Program documentation.

  • Parameters: None.

  • Return value: A string that represents the token.

getTokenInner

This method directly retrieves a token without using the caching mechanism. This method is suitable for custom caching.

  • Parameters: None.

  • Return value: A string that represents the token.

Important

The server denies frequent calls to this API.

Short sentence recognition

Class: SpeechRecognition

The SpeechRecognition class is used for short sentence recognition.

  • Constructor parameters:

    Parameter

    Type

    Description

    config

    Object

    The connection configuration object.

  • Description of the Config object:

    Parameter

    Type

    Description

    url

    String

    The service URL.

    token

    String

    The access token. For more information, see Get a token.

    appkey

    String

    The AppKey of the project. To get an AppKey, go to the console.

defaultStartParams()

This method returns a default parameter object. In this object, Format is set to PCM, the sample rate is 16000 Hz, and intermediate results, punctuation prediction, and Inverse Text Normalization (ITN) are enabled. After you obtain the default object, you can add or modify parameters as needed. For more information about the parameters, see the API reference.

  • Parameters: None.

  • Return value:

    An object with the following fields:

    {
        "format": "pcm",
        "sample_rate": 16000,
        "enable_intermediate_result": true,
        "enable_punctuation_prediction": true,
        "enable_inverse_text_normalization": true
    }

on(which, handler)

This method sets the event callback.

  • Parameters:

    Parameter

    Type

    Description

    which

    String

    The event name.

    handler

    Function

    The callback function.

    The supported callback events are as follows:

    Event name

    Description

    Number of callback function parameters

    Callback function parameter description

    started

    Short sentence recognition starts.

    1

    String. The start message.

    changed

    The intermediate result of short sentence recognition.

    1

    String. The intermediate result message.

    completed

    Short sentence recognition is complete.

    1

    String. The completion message.

    closed

    The connection is closed.

    0

    None.

    failed

    An error occurred.

    1

    String. The error message.

  • Return value: None.

async start(param)

This method starts a short sentence recognition task based on the specified `param`. You can use the return value of the `defaultStartParams` method as the `param`. For more information about the parameters, see the API reference.

  • Parameters:

    Parameter

    Type

    Description

    param

    Object

    The parameters for short sentence recognition.

  • Return value: A Promise object is returned. The `resolve` function is called with the `started` message when the `started` event is triggered. The `reject` function is called with the exception information if an error occurs.

async close(param)

This method stops the short sentence recognition task.

  • Parameters:

    Parameter

    Type

    Description

    param

    Object

    The parameters to end short sentence recognition.

  • Return value:

    A Promise object is returned. The `resolve` function is called with the `completed` message when the `completed` event is triggered. The `reject` function is called with the exception information if an error occurs.

shutdown()

Forced disconnection.

  • Parameters: None.

  • Return value: None.

sendAudio(data)

This method sends audio data. The audio format must be the same as the format specified in the start parameters.

  • Parameters:

    Parameter

    Type

    Description

    data

    ArrayBuffer

    The binary audio data.

  • Return value: None.

Code example

The following code is provided for reference. This code uses the built-in recording feature of WeChat mini programs. When you use this code, you must consider the limitations of WeChat mini programs, your frontend page design, and your specific business requirements.

// pages/sr/sr.js

const app = getApp()

const AKID = "Your AKID"
const AKKEY = "Your AKKEY"
const getToken = require("../../utils/token").getToken
const SpeechRecognition = require("../../utils/sr")
const sleep = require("../../utils/util").sleep

Page({
    /**
     * Initial data of the page
     */
    data: {
        srStart : false,
        srResult : "Recognition not started"
    },

    /**
     * Lifecycle function--Called when the page loads
     */
    onLoad: async function (options) {
        wx.getRecorderManager().onFrameRecorded((res)=>{
            if (res.isLastFrame) {
                console.log("record done")
            }
            if (this.data.sr && this.data.srStart) {
                console.log("send " + res.frameBuffer.byteLength)
                this.data.sr.sendAudio(res.frameBuffer)
            }
        })
        wx.getRecorderManager().onStart(()=>{
            console.log("start recording...")
        })
        wx.getRecorderManager().onStop((res) => {
            console.log("stop recording...")
            if (res.tempFilePath) {
                wx.removeSavedFile({
                    filePath:res.tempFilePath
                })
            }
        })
        wx.getRecorderManager().onError((res) => {
            console.log("recording failed:" + res)
        })

        try {
            this.data.token = await getToken(AKID, AKKEY)
        } catch (e) {
            console.log("error on get token:", JSON.stringify(e))
            return
        }
        let sr = new SpeechRecognition({
            url : app.globalData.URL,
            appkey: app.globalData.APPKEY,
            token: this.data.token
        })

        sr.on("started", (msg)=> {
            console.log("Client recv started")
            this.setData({
                srResult : msg
            })
        })

        sr.on("changed", (msg)=>{
            console.log("Client recv changed:", msg)
            this.setData({
                srResult : msg
            })
        })
      
        sr.on("completed", (msg)=>{
            console.log("Client recv completed:", msg)
            this.setData({
                srResult : msg
            })
        })
    
        sr.on("closed", () => {
            console.log("Client recv closed")
        })
    
        sr.on("failed", (msg)=>{
            console.log("Client recv failed:", msg)
            this.setData({
                srResult : msg
            })
        })

        this.data.sr = sr
    },

    /**
     * Lifecycle function--Called when the page is first rendered
     */
    onReady: function () {

    },

    /**
     * Lifecycle function--Called when the page is displayed
     */
    onShow: function () {

    },

    /**
     * Lifecycle function--Called when the page is hidden
     */
    onHide: function () {

    },

    /**
     * Lifecycle function--Called when the page is unloaded
     */
    onUnload: function () {
        console.log("sr onUnload")
        this.data.srStart = false
        wx.getRecorderManager().stop()
        if (this.data.sr) {
            this.data.sr.shutdown()
        } else {
            console.log("sr is null")
        }
    },

    /**
     * Page-related event handler function--Listens for user pull-down actions
     */
    onPullDownRefresh: function () {

    },

    /**
     * Handler function for the page pull-up and bottom-reaching event
     */
    onReachBottom: function () {

    },

    /**
     * Called when the user clicks the share button in the upper-right corner
     */
    onShareAppMessage: function () {

    },

    onSrStart: async function() {
        if (!this.data.sr) {
            console.log("sr is null")
            return
        }

        if (this.data.srStart) {
            console.log("sr is started!")
            return
        }
        let sr = this.data.sr
        try {
            await sr.start(sr.defaultStartParams())
            this.data.srStart = true
        } catch (e) {
            console.log("start failed:" + e)
            return
        }

        wx.getRecorderManager().start({
            duration: 600000,
            numberOfChannels: 1,
            sampleRate : 16000,
            format: "PCM",
            frameSize: 4
        })
    },

    onSrStop: async function() {
        wx.getRecorderManager().stop()
        await sleep(500)
        if (this.data.srStart && this.data.sr) {
            try {
                console.log("prepare close sr")
                await this.data.sr.close()
                this.data.srStart = false
            } catch(e) {
                console.log("close sr failed:" + e)
            }
        }
    }
})