標籤:訊息 失敗 link red 增加 數組 匯出 擷取 開發
關於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名稱
BotBuilder Nodejs樣本查看