首页 > 技术文章 > BotBuilder Nodejs示例查看

weschen 2017-08-29 10:24 原文

关于Bot Framework知识,可以参考《Nodejs Bot学习

本文是根据bot framework官方示例《https://github.com/Microsoft/BotBuilder》写出的个人学习资料


 

示例一:basics-waterfall(最基础的瀑布流)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-waterfall

https://github.com/ChenWes/bot-nodejs-sample/tree/master/1-basics-waterfall

多个方法时,上一个方法的结果,可以在下一个方法提取到,就是所说的瀑布流,运行完一个方法,接着运行下一个方法

将数据保存至userData,但第二次并未取出


 

示例二:basics-loops(最基础的循环)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-loops

https://github.com/ChenWes/bot-nodejs-sample/tree/master/2-basics-loops

预先定义好问题的数组,bot开始直接进入至dialog,在dialog中,提取数组中的问题,并发送给用户,获取回复

数组没有循环完成,则继续下一个问题(使用replaceDialog替换dialog),循环完成了,则返回至bot开始的方法中(endDialog()和endDialogWithResult()都可以返回)


 

示例三:basics-menus(基本的菜单选择)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-menus

https://github.com/ChenWes/bot-nodejs-sample/tree/master/3-basics-menus

bot开始进入至主菜单,选择不同的菜单进入不同的dialog,各个dialog完成时,使用endDialog()方法返回到主菜单dialog,此时使用replaceDialog()方法重新获取控制权

在主菜单中,使用endDialog()返回,主动权回到bot中,如果使用endConverstation()方法,则会结束整个对话

在endDialog()方法加入数组参数,则会以bot SDK的默认规则随机选择其中一个元素返回


 

示例四:basics-naturalLanguage(结合Luis开发“闹钟”应用)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-naturalLanguage

https://github.com/ChenWes/bot-nodejs-sample/tree/master/4-basics-naturalLanguage

结合Luis,在本机一个对象中,加入“闹钟”对象,并设置一个定时器,如果是对应的时间,就直接返回一条消息给用户

增加“闹钟”前获取用户的地址(该地址记录的是用户的地址)

返回消息前设置用户的地址,这样就可以返回至增加“闹钟”的用户了,其他用户并不会有提示,发送完提示后删除“闹钟”

实体识别可以知道返回的时间,并变为日期与时间组合

从Luis实体中找到参数,如果没有,则让用户选择需要删除的“闹钟”


 

示例五:basics-multiTurn(多轮瀑布流)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-multiTurn) 

https://github.com/ChenWes/bot-nodejs-sample/tree/master/5-basics-multiTurn

运行例子是失败的,从代码的逻辑看,应该是通过查询不同的问题,不同的问题都是返回到同一个dialog回应(但传入dialog的参数是不同的,回复dialog会根据参数不同从而有不同的回应)


 

示例六:basics-firstRun(中间件)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-firstRun

https://github.com/ChenWes/bot-nodejs-sample/tree/master/6-basics-firstRun

中间件的作用就是可以设定是否重复运行对话

每发一条消息,都会进入onFindAction方法中,onFindAction即中间件

中间件,首次,userData.version为空,则callback时,score为1.1,则会运行firstRun dialog,userData.version修改为1

以后,userData.version为1,则callback时,score为0,则会跳过firstRun dialog

运行效果:首次(打开模拟器的第一次,关闭后重新计算)会进入至firstRun dialog,无论你是否输入名称,都会保存至userData,然后,以后每次都会运行bot的第二行和第三行代码

在以后的输入中,你如果输入了help,就会一直卡住在help中


 

示例七:basics-logging(日志中间件)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-logging

https://github.com/ChenWes/bot-nodejs-sample/tree/master/7-basics-logging

日志中间件的方法名称得是botbuilder

在中间件中,如没有运行next()方法,则bot本身的消息不会被发送出去


 

示例八:basics-localization(多语言支持)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-localization

https://github.com/ChenWes/bot-nodejs-sample/tree/master/8-basics-localization

用户可选择语言,预计在项目中加入JSON文件,并可以让用户进行选择语言,用户选择语言后,可以读取JSON文件对应节点的文本

session.preferredLocale可以选择不同的语言


 

示例九:basics-customPrompt(自定义对话框)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-customPrompt

https://github.com/ChenWes/bot-nodejs-sample/tree/master/9-basics-customPrompt

主程序等待回复,当子dialog运行endDialogWithResult(),即会重新激活

第一次进入至onBegin(),一定会经过matches()方法,

如果匹配到了,就会结束,没有匹配会运行onDefault(),

也会查看文本是否等于42,等于42则结束,不等于42会发出提示文本,但并不会退出


 

示例十:basics-libraries(dialog封装成Lib)(https://github.com/Microsoft/BotBuilder/tree/master/Node/examples/basics-libraries) 

https://github.com/ChenWes/bot-nodejs-sample/tree/master/10-basics-libraries

定义一个Library,然后导出

lib的dialog定义事件,调用时,直接使用beginDialog()//参加使用Lib加dialog名称

 

推荐阅读