17611538698
webmaster@21cto.com

JS聊天机器人框架BotUI 使用笔记

资讯 0 4441 2020-02-09 12:04:08
11898116.jpg



前言

一款自动回复文字、图片、视频的JS聊天机器人框架BotUI,可以自由设置多种选项、触发关键词、输入框等内容,聊天内容或范围也可以自由设置,回复内容可以是文字、图片(GIF亦可)、视频,我在博客中引用了此框架。

项目地址

相关工具

A JavaScript framework to create conversational UIS

Github: https://botui.org

使用笔记

head部分,需要引入CSS样式文件

<link rel="stylesheet" href="build/botui.min.css">
<link rel="stylesheet" href="build/botui-theme-default.css">




BODY部分,设置好聊天机器人的对话容器位置:
<div class="botui-app-container" id="botui-app">
    <bot-ui></bot-ui>
</div>



引入botui.js和vue.js文件,然后设置好预回复的内容。
<script src="https://cdn.jsdelivr.net/npm/v ... gt%3B
<script src="build/botui.js"></script>
<!-- 这边是我自己的回复配置 -->
<script language="javascript">
var botui = new BotUI("me");
 
botui.message.bot({
    delay: 200,
    content: "Hi, there[拍手icon]"
}).then(function() {
    return botui.message.bot({
        delay: 1000,
        content: "这里是 雾时之森"
    })
}).then(function() {
    return botui.message.bot({
        delay: 1000,
        content: "一个可爱的蓝孩子~"
    })
}).then(function() {
    return botui.action.button({
        delay: 1500,
        action: [{
            text: "然后呢? [笑脸icon]",
            value: "and"
        },
        {
            text: "少废话! [狗脸icon]",
            value: "gg"
        }]
    })
}).then(function(res) {
    if (res.value == "and") {
        other()
    }
    if (res.value == "gg") {
        return botui.message.bot({
            delay: 1500,
            content: "![告辞](https://ae01.alicdn.com/kf/Hd3 ... 1S.jpg)"
        })
    }
});
 
var other = function() {
    botui.message.bot({
        delay: 1500,
        content: "[拍手icon]"
    }).then(function() {
        return botui.message.bot({
            delay: 1500,
            content: "主修的网络工程专业,是个即将毕业的大四狗"
        })
    }).then(function() {
        return botui.message.bot({
            delay: 1500,
            content: "略懂HTML/CSS/JavaScript/PHP,偶尔也折腾易语言、Python"
        })
    }).then(function() {
        return botui.message.bot({
            delay: 1500,
            content: "不过还是比较熟悉手机刷机、主机DIY与系统安装"
        })
    }).then(function() {
        return botui.message.bot({
            delay: 1500,
            content: "喜欢折腾,热爱折腾,目前正在计算机一道上探索中"
        })
    }).then(function() {
        return botui.action.button({
            delay: 1500,
            action: [{
                text: "为什么叫 雾时之森 呢? [疑问icon]",
                value: "next"
            }]
        })
    }).then(function(res) {
        return botui.message.bot({
            delay: 1500,
            content: "一次起昵称时偶然打出了这个名字,于是我就沿用了下来,嗯!"
        })
    }).then(function() {
        return botui.action.button({
            delay: 1500,
            action: [{
                text: "域名有什么含意吗?(ง •_•)ง",
                value: "next"
            }]
        })
    }).then(function(res) {
        return botui.message.bot({
            delay: 1500,
            content: "emmmmm,看备案信息你就知道了_(:з」∠)_"
        })
    }).then(function(res) {
        return botui.message.bot({
            delay: 1500,
            content: "那么,仔细看看我的博客吧? ^_^"
        })
    });
};</script>





这样一个聊天机器人已经完成,欢迎大家试用。

评论