图灵聊天机器人小程序

历时半年整理出了十多万字的学习笔记,目前依旧在更新
欢迎点赞和支持~
博客

项目描述:

  • 根据图灵API向聊天机器人发送聊天信息,并渲染返回的数据。
  • 具有清空聊天记录的按钮。
  • 本来是想上线体验版,但体验版就是发送不了信息,真机调试都好好的,
  • 图灵的聊天API免费接口一台终端一天大概只有十几次的请求…

来个图

图灵聊天机器人小程序_第1张图片

用到的组件和API:

  • API主要用到是用于发送post请求的wx.request(),点击事件的绑定和提交事件的绑定。
  • 组件主要用的是可滚动视图组件和用于循环生成对话的block组件。

生成对话的方式:

  • 首先获取到submit事件的事件对象,然后通过e.detail.value.say获取到你输入的信息,(say属性需要在input框的name属性定义);
  • 然后以{key: ‘你注册的图灵API得到的key’, info: 你输入的信息}的方式向http://www.tuling123.com/openapi/api发送POST请求,然后将拿到的数据和你输入的数据作为一个对象利用setData利用push方法添加到对话数组里,然后小程序会根据新数据重新渲染页面。

wxml部分

<view class="title">{{title}}view>

<view class="que" >
  <scroll-view class="con" scroll-y>
    <block wx:for="{{says}}" wx:for-item="item" wx:key="index">
      
      <view class="iSay">
        <view class="right-img">
          <image src="{{headRight}}">image>
        view>
        <view class="right-text">
          <view>{{item.iSay}}view>
        view>
      view>


      
      <view class="robotSay">
        <view class="left-img">
          <image src="{{headLeft}}">image>
        view>
        <view class="left-text">
          <view>{{item.rSay}}view>
        view>
      view>
    block>
  scroll-view>

view>


<view class="bottom">
  <view class="send">
    <form bindsubmit="converSation">
      <input type="text" placeholder="说点什么吧~" name="say" value="{{tempISay}}">input>
      <button class="btn-d" bindtap="deleteChat">清空button>
      <button class="submit" id="btn" form-type="submit">发送button>
    form>
  view>
view>

wxss部分

/* 标题部分 */
.title {
  position: fixed;
  height: 6vh;
  width: 100%;
  top: 0px;
  line-height: 80rpx;
  text-align: center;
  font-size: 40rpx;
  font-weight: bold;
  background-color: #fff;
  color: black;
  box-shadow: 16rpx -4rpx 30rpx rgb(209, 204, 204);
}

/* 对话框部分 */
.que {
  width: 98%;
  margin-top: 7vh;
  height: 83vh;
  margin-left: 1%;
  box-sizing: border-box;
  overflow: hidden;
}

.con {
  width: 95%;
  height: 100%;
  margin-left: 20rpx;
}

.iSay {
  width: 70%;
  margin-left: 30%;
  margin-top: 60rpx;
}

.iSay .right-img {
  position: relative;
  height: 60rpx;
  width: 60rpx;
  left: 88%;
  margin-top: 5rpx;
}

.iSay .right-text {
  width: 80%;
  margin-top: -65rpx;
}

.iSay .right-text view {
  padding: 10rpx 30rpx;
  text-overflow: ellipsis;
  word-wrap: break-word;
  border-radius: 30rpx;
  background-color: #eee;
  color: green;
  font-size: 25rpx;
}

.robotSay {
  width: 70%;
  margin-top: 30rpx;
}

.robotSay .left-img {
  width: 75rpx;
  height: 80rpx;
}

.robotSay .left-text {
  width: 80%;
  margin-left: 20%;
  margin-top: -60rpx;
}

.robotSay .left-text view {
  padding: 10rpx 30rpx;
  text-overflow: ellipsis;
  word-wrap: break-word;
  border-radius: 30rpx;
  background-color: #eee;
  color: green;
  font-size: 25rpx;
}

/* 按钮部分 */
.bottom {
  position: fixed;
  width: 100%;
  height: 10vh;
  bottom: 0rpx;
  background-color: gray;
}

.send {
  position: fixed;
  width: 100%;
  bottom: 20rpx;
  height: 66rpx;
}

input {
  width: 55%;
  height: 66rpx;
  padding-left: 40rpx;
  box-sizing: border-box;
  border-radius: 30rpx;
  border: 1rpx solid black;
  background-color: #fff;
}

.send button {
  float: right;
  width: 20%;
  height: 66rpx;
  bottom: 66rpx;
  margin-right: 10rpx;
  border-radius: 30rpx;
  line-height: 66rpx;
  color: #fff;

}

.send button.submit {
  background-color: rgb(62, 214, 143);
}

.send button.btn-d {
  background-color: black;
}

.robotSay .left-img image,
.iSay .right-img image {
  height: 100%;
  width: 100%;
  border-radius: 50%;
}

js部分

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: 'Chat Robot',
    headLeft: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFwAAABuCAYAAACqXXT+AAAgAElEQVR4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IXzZrWT67jd+amx+J5LPyWZbpyXwHeU723IXQj7H4IcrAPuei/wbPZp3x1lKa8G7le9sg64AsQywXsiGghYBlZY51ov7odP0fz5P/m5j68iGYccrPGjP2VoC3U8Dd//OeeZbcyr3g3XW/40fre4+7N8GuB9cPk2x2QqjOaies7UbuJoruB34/c4BPNcJ5lMYOxnUXKez8wPfgWr7LTa9C3g7U/9/pT1/6tjjQOoAAAAASUVORK5CYII=',
    headRight: '',
    says: [
      {
        rSay: '你好,来和我聊聊天吧~~~',
        iSay: '你好',
      },
    ],
    tempISay: '',
  },
  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function () {
    let That = this;
    //获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
    wx.getSetting({
      //返回成功时
      success: function (res) {
        //授权用户信息
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function (res) {
              That.setData({
                headRight: res.userInfo.avatarUrl,
              })
            }
          });
        }
      }
    })
  },


  // 绑定input框的提交事件
  converSation(e) {
    let That = this;
    let says = this.data.says;
    let key = '9bfb8444a1324d82bfc15ba46e6ca3c2';
    let newSays = {};
    newSays.iSay = e.detail.value.say;

    // 发送请求
    newSays.iSay && wx.request({
      url: 'http://www.tuling123.com/openapi/api',
      data: {
        key: key,
        info: newSays.iSay
      },
      header: {
        'content-type': 'application/json'
      },
      success(res) {
        newSays.rSay = res.data.text;
        says.push(newSays);


        // tempISay用于做中间变量,数据提交后将输入框里的内容清空
        That.setData({
          says: says,
          tempISay: '',
        })
      },
    })
  },


  // 清空对话内容
  deleteChat() {
    this.setData({
      says: [],
    })
  }
})

你可能感兴趣的:(前端练手项目,小程序,聊天机器人)