微信小程序—智能对话开发

     这段时间开发了一个智能对话的微信小程序,下面就把这个介绍一下。0.介绍界面:对话:功能:目前支持闲聊,问时间,问天气,算24点,单位换算,汇率查询,邮政编码,笑话,故事,算数功能。1.智能对话接口首先是对话 ...

0.介绍

界面: 
对话首页 

对话: 
聊天 
功能:目前支持闲聊,问时间,问天气,算24点,单位换算,汇率查询,邮政编码,笑话,故事,算数功能。

1.智能对话接口

首先是对话的接口,用的是OLAMI的接口,可以自己定义需要的对话,也有系统提供的对话模块。 
对话模块定义好之后,查看API文档,将对话通过API发送之后就可以得到回答。

API调用代码

  NLIRequest:function(corpus,arg) { // corpus是要发送的对话;arg是回调方法
    var that = this;
    // appkey
    var appkey = that.globalData.NLPAppkey;
    // appsecret
    var appSecret = that.globalData.NLPAppSecret;
    var api = "nli";
    var timestamp = new Date().getTime();
    // MD5签名
    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是用来实现上下文的,可以自己随意定义内容,要够长够随机
    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);
        // 回调函数,解析数据
        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.对话内容显示

前端显示代码

<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">语义解析技术由OLAMI提供</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}}">发送</button>
    </view>
  </form>
</view>
  •  

【1】scroll-into-view=”{{scrolltop}}”是将对话滚动到最新位置,在js中把最新的id赋给scrolltop,页面会自动滚动到指定位置。 
【2】chatList存储对话内容,循环渲染对话框。orientation是左右位置,左边是答案,右边是用户输入。 
【3】userLogoUrl是用户头像的url,如果用户不授权使用用户公开信息,则使用默认的用户头像。

最后

其他的内容就是一些判断以及解析数据。

源代码地址:https://pan.baidu.com/s/1eSvjbQa

转载于:https://my.oschina.net/u/3396785/blog/1511635

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序开发AI对话机器人是一种利用人工智能技术实现自动化对话功能的应用程序。通过开发微信小程序中的AI对话机器人,可以帮助用户解决问题、提供服务和娱乐。 首先,AI对话机器人可以通过自然语言处理技术,理解用户输入的文本信息。通过文本分析,机器人能够识别并理解用户提出的问题、请求或指令,从而进行相应的回应和处理。 其次,AI对话机器人可以通过机器学习和深度学习技术,不断积累和学习用户的运用场景、意图和语义。通过模型训练和优化,机器人可以更准确地理解和回答用户的问题,提供更加智能和个性化的服务。 同时,AI对话机器人还可以根据用户的需求和行为,提供相应的推荐和建议。例如,在购物小程序中,机器人可以根据用户的喜好和购买历史,为其推荐相关的商品和促销活动。 除了智能问答功能外,AI对话机器人还可以通过图像和语音识别技术,实现更加丰富和多样化的交互方式。例如,用户可以通过拍照、语音输入等方式与机器人进行沟通和互动,提高用户体验和便利性。 总而言之,微信小程序开发AI对话机器人可以为用户提供智能化的问题解答、个性化的服务推荐和多样化的交互体验。随着人工智能技术的不断发展,相信AI对话机器人的功能和性能将越来越强大,为用户带来更多的便利和价值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值