vue机器人聊天

vue机器人聊天

各位话不多说! 先上效果图:如下

官网地址: https://www.npmjs.com/package/vue-bot-ui

第一步先安装:npm install vue-bot-ui

然后导入组件:import { VueBotUI } from 'vue-bot-ui'

components:{ VueBotUI}

就可以使用了哦!

 <VueBotUI
        :messages = "messageData" //       是机器人 发送的消息可以进行自定义
        :options = "botOptions"  //       data 中定义自己需要的 可以查看官网 这个是定义机器人头像等
        :bot-typing="botTyping"  //       如果true,机器人输入指示器将显示
        :input-disable="inputDisable" //    如果true, 消息输入将被禁用
        :is-open="false"           //   如果true, 板子将从 init 打开
        @msg-send = "messageSendHandler" //  自己点击发送消息
        @init="botStart"           //  初始化机器人发送消息
    />

怎么进行呢 列子如下:

import  { VueBotUI } from 'vue-bot-ui'
import { messageService } from './helpers/message' //需要你新建js文件 
export default {
  name: "Contactus",
  components:{
    VueBotUI
  },
  data() {
    return {
      botTyping:true,
      messageData: [],
      inputDisable:false,
      botOptions : {
        botTitle: '小康客服', //显示是什么
        botAvatarImg:'机器人头像 你可以自定义',
        botAvatarSize:'32', //这个是头像的大小
        animation:true
        // 请参阅下面的选项列表
      },
    };
  },
  methods: {
    botStart () {   //初始化 会发送什么
      this.botTyping = true
      setTimeout(() => {
        this.botTyping = false
        this.messageData.push({
          agent : 'bot' ,
          type : 'button' ,
          text : '欢迎您!使用小康智能客服,请问有什么需要吗??' ,
        })
      }, 1000)
    },
    messageSendHandler(value){  //点击发送传一个值 
      this.messageData.push({
        agent: 'user',
        type: 'text',
        text: value.text
      })
      this.getResponse()   //调用下面机器人自定义的消息
      console.log("点击发送了")
    },
    getResponse () {
      this.botTyping = true
      messageService.createMessage()
          .then((response) => {
            const replyMessage = {
              agent: 'bot',
              ...response
            }

            this.inputDisable = response.disableInput
            this.messageData.push(replyMessage)

            // finish
            this.botTyping = false
          })
    }
}

import { messageService } from './helpers/message' 文件如下:

import { fakeMessages } from './fake-messages' //这个也是一个js 是机器的自定义一些回答消息的需要新建文件

export const messageService = {
    createMessage
}

function createMessage () {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            let randomNumber = Math.floor(Math.random() * fakeMessages.length)
            resolve(fakeMessages[randomNumber])
        }, 1000)
    })
}

import { fakeMessages } from './fake-messages' 文件如下:

export const fakeMessages = [
    {
        'type': 'text',
        'text': '您说啥小康不会',
        'disableInput': false
    },
    {
        'type': 'button',
        'text': '您是否要访问或链接我们?',
        'options': [
            {
                'text': '打开博客',
                'value': 'search',
                'action': 'postback'
            },
            {
                'text': '联系人工客服',
                'value': 'submit_ticket',
                'action': 'postback'
            },
        ],
        'disableInput': true
    },
    {
        'type': 'text',
        'text': 'Please type your problem',
        'disableInput': false
    },
    {
        'type': 'button',
        'text': 'Here are the results from our knowledgebase.',
        'options': [
            {
                'text': '联系小杰',
                'value': 'https://google.com',
                'action': 'url'
            },
            {
                'text': '联系官方',
                'value': 'https://google.com',
                'action': 'url'
            },
            {
                'text': "取消",
                'value': 'result_not_match',
                'action': 'postback'
            }
        ],
        'disableInput': true
    },
    {
        'type': 'text',
        'text': 'Sorry to hear that. Please type your problem and we will create a ticket for you.',
        'disableInput': false
    },
    {
        'type': 'button',
        'text': 'All done! Your support ticket is created.',
        'options': [
            {
                'text': 'View ticket',
                'value': 'https://google.com',
                'action': 'url'
            }
        ],
        'disableInput': false
    }
]

如果还有不懂的可以翻阅官网文档!一些参数什么的

是不是超级简单啊!小伙伴们 这东西挺好玩哦 来试试哦~

  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杰哥力挽狂澜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值