【人像特效】你的小跟班已上线【微信小程序】
756665228 发布于2020-11-20 浏览:1423 回复:4
2
收藏

本期活动专场介绍在 https://ai.baidu.com/forum/topic/show/966939?hmsr=qq=shequ&hmpl=face

小帅丶这次就给各位开发者分享下UNIT闲聊功能对接微信小程序的流程以及实现

先来看一个效果图

Step1:访问UNIT主网页

https://ai.baidu.com/unit/v2#/sceneliblist

Step2:创建闲聊

推荐选择增强版哦

Step3.创建机器人

选择 技能分发

Step4.机器人添加闲聊技能

选择自己创建的闲聊技能

Step5.保存技能ID以及机器人ID

 

-------------------------获取应用的AccessToken-------------------------

参考新手接入指南  https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjgn3

-------------------------微信小程序-------------------------

使用的ColorUI建议自己优先下载好哈。或者往后看小帅丶提供的源码也可

1.创建一个api.js

UNIT 接口文档说明地址 
https://ai.baidu.com/docs#/UNIT-v2-service-API/top
//接口token
const accessToken = '';
//闲聊接口-沙盒环境
const chat_url = 'https://aip.baidubce.com/rpc/2.0/unit/service/chat?access_token='+accessToken;
//闲聊技能ID
const SYS_REMEMBERED_SKILLS = '';
//机器人ID
const service_id = '';
/**
 * 聊天方法
 * @param log_id 日志ID
 * @param user_id 用户ID
 * @param text 文本内容
 */
let chatRequest = (log_id,user_id,text,callback) => {
  //发送接口请求
  wx.request({
    url: chat_url,
    method:'POST',
    data: '{"dialog_state":{"contexts":{"SYS_REMEMBERED_SKILLS":["'+SYS_REMEMBERED_SKILLS+'"]}},"log_id":"'+log_id+'","request":{"query":"'+text+'","user_id":"'+user_id+'"},"service_id":"'+service_id+'","session_id":"","version":"2.0"}',
    success: function (res) {
      callback.success(res.data)
    },
    fail: function (res) {
      if (callback.fail)
        callback.fail()
    }
  })
}
/** 暴露出去 */
module.exports={
  chatRequest: chatRequest
}

2.创建chatbot页面以及JS

由于html会被社区解析。帖子就不包含了哈。后面会提供下载地址哈
const app = getApp()
var api = require('../../utils/api.js');
var util = require('../../utils/util.js');
Page({
  data: {
    InputBottom: 0,
    text:null,
    title:'和小跟班对话',
    chatRecord: [{
      text:'嗨~快来跟我聊天吧~',
      type:'1',
      date:util.formatTime(new Date())
    }],
    inputValue:null,
    bstatus:false
  },
  InputFocus(e) {
    this.setData({
      InputBottom: e.detail.height
    })
  },
  InputBlur(e) {
    this.setData({
      InputBottom: 0
    })
  },
  undev(){
    wx.showToast({
      title: '敬请期待...',
      mask:true,
      icon:'none',
      duration:1000
    })
  },
  //进来加载
  onLoad: function () {
  },
  //发送按钮
  sendMsg(){
    var that = this;
    that.sendChatMsg();
  },
  //发送后台请求
  sendChatMsg(){
    var that = this;
    var text = that.data.inputValue;
    if(text==''||text==null){
      wx.showToast({
        title: '发送内容不可为空',
        mask:true,
        icon:'none',
        duration:1000
      })
    }else{
      that.setData({
        msg: {
          text:text,
          type:'2',
          date:util.formatTime(new Date())
        },
      })
      that.pageScrollToBottom();
      var length = that.data.chatRecord.push(that.data.msg);
      that.setData({
        inputValue:null,
        chatRecord:that.data.chatRecord,
        title:'小跟班正在输入...',
        bstatus:true
      })
      let log_id = util.formatTime(new Date());
      let user_id = util.formatTime(new Date());
      api.chatRequest(log_id,user_id,text,{
        success(result) {
            if(result.error_code==0){
              that.setData({
                msg: {
                  text:result.result.response_list[0].action_list[0].say,
                  type:'1'
                },
                bstatus:false
              })
              that.data.chatRecord.push(that.data.msg);
              that.setData({
                inputValue:null,
                title:'和小跟班对话',
                chatRecord:that.data.chatRecord
              })
              that.pageScrollToBottom();
            } else {
              that.pageScrollToBottom();
              that.setData({
                bstatus:false,
                title:'和小跟班对话',
              })
              if(result.code==87014){
                wx.showModal({
                  content: '内容违规,请更换',
                  showCancel: false,
                  confirmText: '好的',
                  success (res) {
                    if (res.confirm) {
                      that.data.chatRecord.splice(length-1,1)
                      that.setData({
                        chatRecord:that.data.chatRecord
                      })
                    }
                  }
                })
              }else{
                wx.showModal({
                  content: result.msg_zh,
                  showCancel: false,
                  confirmText: '好的'
                })
              }
            }
        }
      })
    }
  },
      /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    that.pageScrollToBottom();
},
  // 滚动到页面底部
  pageScrollToBottom: function() {
    let that = this;
    wx.createSelectorQuery().select('#chatBox').boundingClientRect(function(rect) {
      console.log(rect)
      wx.pageScrollTo({
        scrollTop: rect.height,
        duration: 100
      })
    }).exec()
  },
})

源码评论给出哈

收藏
点赞
2
个赞
共4条回复 最后由wangwei8638回复于2020-12-17
#3756665228回复于2020-11-20

如果任何疑问请加QQ:783021975

1
#2756665228回复于2020-11-20

链接: https://pan.baidu.com/s/1SROt7HswVsIK5igo98jzow 提取码: ti3f

 

1
#5wangwei8638回复于2020-12-17

多轮对话很赞

0
TOP
切换版块