正在阅读:如何在微信小程序上制作智能机器人?制作方法如何在微信小程序上制作智能机器人?制作方法

2017-12-22 11:43 出处:其他 作者:佚名 责任编辑:lishengtao

  如何在微信小程序上制作智能机器人?还不会操作的小伙伴,就跟着小编一起去了解具体的方法吧!

  项目为智能应答机器人,使用了图灵机器人接口,慢慢调戏吧

  首页,主要处理页:

  //index.js

  varapp=getApp();

  varthat;

  varchatListData=[];

  Page({

  data:{

  askWord:'',

  userInfo:{},

  chatList:[],

  },

  onLoad:function(){

  that=this;

  //获取用户信息

  app.getUserInfo(function(userInfo){

  that.setData({

  userInfo:userInfo

  });

  });

  },

  onReady:function(){

  //问候语

  setTimeout(function(){

  that.addChat('你好啊!','l');

  },1000);

  },

  sendChat:function(e){

  letword=e.detail.value.ask_word?e.detail.value.ask_word:e.detail.value;//支持两种提交方式that.addChat(word,'r');//请求api获取回答

  app.req('post','openapi/api',{

  'data':{'info':word,'loc':'广州','userid':'123'},'success':function(resp){that.addChat(resp.text,'l');

  if(resp.url){

  that.addChat(resp.url,'l');

  }

  },

  });

  //清空输入框

  that.setData({

  askWord:''

  });

  },

  //新增聊天列表

  addChat:function(word,orientation){

  letch={'text':word,'time':newDate().getTime(),'orientation':orientation};chatListData.push(ch);that.setData({

  chatList:chatListData

  });

  }

  })

  页面:

  //index.wxml

  <viewclass="container">

  <scroll-viewclass="scrool-view"scroll-y="true">

  <viewclass="chat-list">

  <blockwx:for="{{chatList}}"wx:key="time">

  <viewclass="chat-left"wx:if="{{item.orientation=='l'}}">

  <imageclass="avatar-img"src="../../res/image/wechat-logo.png"></image>

  <text>{{item.text}}</text>

  </view>

  <viewclass="chat-right"wx:if="{{item.orientation=='r'}}">

  <text>{{item.text}}{{item.url}}</text>

  <imageclass="avatar-img"src="{{userInfo.avatarUrl}}"></image>

  </view>

  </block>

  </view>

  </scroll-view>

  <formbindsubmit="sendChat">

  <viewclass="ask-input-word">

  <inputplaceholder=""name="ask_word"type="text"bindconfirm="sendChat"value="{{askWord}}"/>

  <buttonformType="submit"size="mini">发送</button>

  </view>

  </form>

  </view>

  网络请求方法:

  //app.js

  req:function(method,url,arg){

  letdomian='http://www.tuling123.com/',data={'key':'9d2ff29d44b54e55acadbf5643569584'},dataType='json';//为方便广大群众,提供keyletheader={'content-type':'application/x-www-form-urlencoded'};if(arg.data){data=Object.assign(data,arg.data);

  }

  if(arg.header){

  header=Object.assign(header,arg.header);}

  if(arg.dataType){

  dataType=arg.dataType;

  }

  letrequest={

  method:method.toUpperCase(),

  url:domian+url,

  data:data,

  dataType:dataType,

  header:header,

  success:function(resp){

  console.log('responsecontent:',resp.data);letdata=resp.data;typeofarg.success=="function"&&arg.success(data);},fail:function(){

  wx.showToast({

  title:'请求失败,请稍后再试',

  icon:'success',

  duration:2000

  });

  typeofarg.fail=="function"&&arg.fail();},complete:function(){

  typeofarg.complete=="function"&&arg.complete();}

  };

  wx.request(request);

  }

关注我们

最新资讯离线随时看 聊天吐槽赢奖品