Wechat Program-Intelligent Dialogue Development

Keywords: JSON

During this period, an intelligent dialog was developed. Wechat applet Now let's introduce this. 0. Introduce the interface: Dialogue: Functions: currently support chat, ask time, ask weather, calculate 24 points, unit conversion, exchange rate query, postal code, jokes, stories, arithmetic functions. 1. Intelligent dialog interface is dialog first.

0. introduction

Interface:
 

Dialogue:
 
Functions: At present, it supports chatting, asking time, asking weather, calculating 24 points, unit conversion, exchange rate query, postal code, jokes, stories, arithmetic functions.

1. Intelligent Dialogue Interface

The first is the interface of dialogue. OLAMI The interface can define the dialog needed by itself, as well as the dialog module provided by the system.  
After the dialog module is defined, the API document is viewed and the dialog can be answered after it is sent through the API.

API call code

  NLIRequest:function(corpus,arg) { // corpus is a conversation to send; arg is a callback method
    var that = this;
    // appkey
    var appkey = that.globalData.NLPAppkey;
    // appsecret
    var appSecret = that.globalData.NLPAppSecret;
    var api = "nli";
    var timestamp = new Date().getTime();
    // MD5 signature
    var sign = MD5.md5(appSecret + "api=" + api + "appkey=" + appkey + "timestamp=" + timestamp + appSecret);
    var rqJson = { "data": { "input_type": 1, "text": corpus }, "data_type": "stt" };
    var rq = JSON.stringify(rqJson);
    var nliUrl = that.globalData.NLPUrl;
    // cusid is used to achieve context, you can define content at will, long enough random
    var cusid = that.globalData.NLPCusid;
    console.log("[Console log]:NLIRequest(),URL:" + nliUrl);
    wx.request({
      url: nliUrl,
      data: {
        appkey: appkey,
        api: api,
        timestamp: timestamp,
        sign: sign,
        rq: rq,
        cusid: cusid,
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      success: function (res) {
        var resData = res.data;
        console.log("[Console log]:NLIRequest() success...");
        console.log("[Console log]:Result:");
        console.log(resData);
        var nli = JSON.stringify(resData);
        // Callback function, parsing data
        typeof arg.success == "function" && arg.success(nli);
      },
      fail: function (res) {
        console.log("[Console log]:NLIRequest() failed...");
        console.error("[Console log]:Error Message:" + res.errMsg);
        typeof arg.fail == "function" && arg.fail();
      },
      complete: function () {
        console.log("[Console log]:NLIRequest() complete...");
        typeof arg.complete == "function" && arg.complete();
      }
    })
  }
  •  

2. Display of Dialogue Content

Front-end display code

<view class="container">
  <scroll-view class="scrool-view" scroll-y="true" scroll-with-animation="true" scroll-into-view="{{scrolltop}}" enable-back-to-top="true">
    <view class="chat-list">
      <block wx:for="{{chatList}}" wx:key="time">
        <view id="roll{{index + 1}}" class="chat-left" wx:if="{{item.orientation == 'l'}}">
          <image class="avatar-img" src="/res/image/chat_logo.png"></image>
          <text>{{item.text}}</text>
          <image class="avatar-img"></image>
        </view>
        <view id="roll{{index + 1}}" class="chat-right" wx:if="{{item.orientation == 'r'}}">
          <image class="avatar-img"></image>
          <text>{{item.text}}{{item.url}}</text>
          <image class="avatar-img" src="{{userLogoUrl}}"></image>
        </view>
      </block>
    </view>
  </scroll-view>
  <view id="rollend" class="weui-footer weui-footer__text">Semantic parsing technology OLAMI provide</view>
  <form bindsubmit="sendChat">
    <view class="ask-input-word">
      <input class="input-big" placeholder="" name="ask_word" type="text" bindconfirm="sendChat" bindinput="Typing" value="{{askWord}}" />
      <button formType="submit" size="mini" disabled="{{sendButtDisable}}">Send out</button>
    </view>
  </form>
</view>
  •  

[1] scroll-in-view="{{scrolltop}" is to scroll the dialogue to the latest location, in the js The latest id is assigned to scrolltop, and the page automatically scrolls to the specified location.  
[2] chatList stores the dialog content and renders the dialog box circularly. orientation is left and right, left is the answer, and right is the user input.  
[3] userLogoUrl is the url of the user's avatar. If the user is not authorized to use the user's public information, the default user's avatar is used.

Last

Other things are judgments and parsing data.

Source code address: https://pan.baidu.com/s/1eSvjbQa

Posted by mrwhale on Mon, 03 Jun 2019 20:53:34 -0700