這是一個 JavaScript 的 RiveScript 解譯器函式庫。 RiveScript 是聊天機器人的腳本語言,可以輕鬆編寫觸發器/響應對來建立機器人的智慧。
該程式庫既可以在 Web 瀏覽器中使用,也可以作為 Node 模組使用。有關範例,請參閱eg/
資料夾。
RiveScript v2.0.0 對程式碼庫進行了大規模重構,以實現現代的 Async/Await 功能。重構現在允許「直接在 Redis 中儲存使用者變數」或「在條件中使用非同步宏」等功能
但它必然會破壞一些向後相容性——稍微! -- 將先前的同步函數(如reply()
轉變為非同步函數,並像replyAsync()
一樣傳回Promise。
有關更改以及如何修復新版本程式碼的信息,請參閱 Upgrading-v2 文件。
對於nodejs和其他類似的JavaScript引擎,您可以透過npm在專案中安裝此模組:
$ npm install rivescript
對於網絡,您可以使用 unpkg:
< script src =" https://unpkg.com/rivescript@latest/dist/rivescript.min.js " > </ script >
此專案的 git 儲存庫包含 ES2015+ 原始碼。對於針對舊版瀏覽器和 Node 版本的 ES5 構建,請檢查「發布」標籤。編譯後的發行版包括一個lib/
目錄,其中包含可與節點 <= 6 一起使用的 ES5 原始碼,以及一個包含可在網頁上使用的「瀏覽器化」腳本的dist/
目錄。
要在網路上使用,只需像平常一樣使用<script>
標籤載入dist/rivescript.min.js
。
var bot = new RiveScript ( ) ;
// Load a directory full of RiveScript documents (.rive files). This is for
// Node.JS only: it doesn't work on the web!
bot . loadDirectory ( "brain" ) . then ( loading_done ) . catch ( loading_error ) ;
// Load an individual file.
bot . loadFile ( "brain/testsuite.rive" ) . then ( loading_done ) . catch ( loading_error ) ;
// Load a list of files all at once (the best alternative to loadDirectory
// for the web!)
bot . loadFile ( [
"brain/begin.rive" ,
"brain/admin.rive" ,
"brain/clients.rive"
] ) . then ( loading_done ) . catch ( loading_error ) ;
// All file loading operations are asynchronous, so you need handlers
// to catch when they've finished. If you use loadDirectory (or loadFile
// with multiple file names), the success function is called only when ALL
// the files have finished loading.
function loading_done ( ) {
console . log ( "Bot has finished loading!" ) ;
// Now the replies must be sorted!
bot . sortReplies ( ) ;
// And now we're free to get a reply from the brain!
// RiveScript remembers user data by their username and can tell
// multiple users apart.
let username = "local-user" ;
// NOTE: the API has changed in v2.0.0 and returns a Promise now.
bot . reply ( username , "Hello, bot!" ) . then ( function ( reply ) {
console . log ( "The bot says: " + reply ) ;
} ) ;
}
// It's good to catch errors too!
function loading_error ( error , filename , lineno ) {
console . log ( "Error when loading files: " + error ) ;
}
RiveScript.js 的發行版包含一個名為riveshell的互動式命令列 shell,用於測試 RiveScript 機器人。它將“大腦”的路徑(相對或絕對)作為參數 - 包含 RiveScript 文件( .rive文件)的資料夾。
npm install rivescript
),則可以在專案資料夾中使用npx啟動 shell。例子: $ npx riveshell /path/to/brain
npm install -g rivescript
)全域安裝了 RiveScript,則可以從任何地方啟動 shell。例子: $ riveshell /path/to/brain
shell.js
啟動 shell。使用eg/資料夾中的預設大腦的範例: $ node shell.js /eg/brain
進入 shell 後,您可以使用該目錄中的 RiveScript 檔案與機器人聊天。為了進行簡單的偵錯,您可以鍵入/eval
來執行單行 JavaScript 程式碼。請參閱/help
以了解更多資訊。
shell 接受一些命令列參數:
--debug
:啟用詳細偵錯日誌記錄。--watch
:監視回覆資料夾中的更改,並在檔案修改時自動重新載入機器人。--utf8
:啟用 UTF-8 模式。--case
:啟用區分大小寫的使用者訊息。 docs 資料夾中產生了模組的 Markdown 和 HTML 文件。主要模組位於 rivescript。
也請查看RiveScript 社群 Wiki ,以了解 RiveScript 的常見設計模式以及提示和技巧。
GitHub 上該專案的 eg/ 目錄中提供了一個範例,展示瞭如何以多種方式(例如透過 Web 瀏覽器或 telnet 伺服器)與 RiveScript 機器人交互,以及其他程式碼片段和有用的技巧。
若要測試和分享使用 JavaScript 實作的 RiveScript 片段,請查看 RiveScript Playground。
它是一個 JSFiddle 風格的 Web 應用程序,用於在 Web 瀏覽器中使用 RiveScript 並與其他人共享程式碼。
https://play.rivescript.com/
1.0.5 版在 RiveScript 文件中新增了 UTF-8 的實驗性支援。預設情況下它是禁用的。透過在建構函式中傳遞utf8
選項的true
值來啟用它。
預設情況下(未啟用 UTF-8 模式),觸發器只能包含基本 ASCII 字元(無外來字元),且使用者的訊息將移除除字母、數字和空格之外的所有字元。這表示,例如,您無法在 RiveScript 回覆中擷取使用者的電子郵件地址,因為 @ 和 .人物。
啟用 UTF-8 模式後,這些限制將會解除。觸發器僅限於不包含某些元字符,例如反斜杠,並且用戶訊息僅去除反斜杠和 HTML 尖括號(如果您在 Web 應用程式中使用 RiveScript,則可以防止明顯的 XSS)。此外,常見的標點符號被刪除,預設為/[.,!?;:]/g
。這可以透過提供新的RegExp
物件作為rs.unicodePunctuation
屬性來覆寫。例子:
// Make a new bot with UTF-8 mode enabled.
var bot = new RiveScript ( { utf8 : true } ) ;
// Override the punctuation characters that get stripped from the
// user's message.
bot . unicodePunctuation = new RegExp ( / [.,!?;:] / g ) ;
RiveScript 中的<star>
標籤將擷取使用者的「原始」輸入,因此您可以編寫回覆來取得使用者的電子郵件地址或在其姓名中儲存外來字元。
到目前為止,僅在 Node 下運行時進行了測試。透過 Web 伺服器提供服務時,請特別注意您的伺服器使用 RiveScript 來源檔案傳送正確的內容編碼 ( Content-Type: text/plain; charset=utf-8
)。
在 UTF-8 模式下需要注意的一個警告是,標點符號不會從使用者的訊息中刪除,因此如果它們包含逗號或感嘆號,它可能會影響觸發器的匹配能力(您絕對不應該編寫明確的標點符號)即使啟用了 UTF-8 模式,觸發器也不應包含?
或 等符號,
雖然現在可能有效,但未來的更新可能會嚴格執行此操作。
我使用 npm run 腳本來處理各種建置任務。
npm run build
- 使用 Babel 從src/
編譯 ES2015+ 來源並將其輸出到lib/
npm run test
- 根據上述內容使用 Babel 建立原始程式碼,在test/
中建立 ES2015+ 測試腳本並將其輸出到test.babel/
中,然後在其上執行nodeunit
。npm run dist
- 產生完整的可分發版本。原始碼是使用 Babel 建構的,然後交給 webpack 和 uglify 進行瀏覽器建置。npm run webpack
- 直接從src/
中的 ES2015+ 來源建立dist/rivescript.js
(使用babel-loader
)。該命令獨立於npm run build
,並且可以在不留下任何 ES5 程式碼的情況下運行。npm run uglify
- 將dist/rivescript.js
縮小為dist/rivescript.min.js
npm run clean
- 清理所有建置檔。如果您的本機 Node 版本 >= 7(支援 Async/Await),您可以直接執行 ES2015+ 來源,無需執行任何 npm 腳本。為此,我有一個 Makefile。
make setup
- 設定開發環境,安裝依賴項等。make run
- 運行指向範例大腦的shell.js
。該腳本在 ES2015+ 來源上本地運行,無需建置步驟。make test
- 直接在 ES2015+ 測試來源上執行nodeunit
,而無需像npm run test
那樣建置它們。我在 Fedora 37/node 18.7.0 上遇到了這個問題; webpack/webpack#14532 的答案是:
export NODE_OPTIONS=--openssl-legacy-provider
此模組的 npm 維護者的步驟:
package.json
和src/rivescript.js
中的版本號Changes.md
npm run dist
來建立 ES5 原始碼並執行單元測試。npm install ../rivescript-js
)npm login
,然後npm publish
將模組發佈到 NPM。rm -rf .git node_modules
從新資料夾中刪除 cruft。zip -r rivescript-js-VERSION.zip rivescript-js
tar -czvf rivescript-js-VERSION.tar.gz rivescript-js
The MIT License (MIT)
Copyright (c) 2020 Noah Petherbridge
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
RiveScript 官方網站,http://www.rivescript.com/