1.2 馬上來開發機器人
在開始動手之前,要先瞭解:在 Microsoft Bot Framework 中的對談機器人,Bot Connector 收到訊息後,會以 HTTP Post 的方式把資料傳到你的機器人中,所以你必須以一個 web app (或 web api app) 的形式來開發 bot,來接收由 Bot Connector 來的資訊,處理完後再把訊息送給 Bot Connector 來處理。
因為本書以 Node.js 作為開發的程式語言,所以你可以用像是 express 或 restify 等這些框架來開發這個 web app 的皮。
第一個專案的範例就以常見的 Echo 機器人來認識 Microsoft Bot Framework,它的功能就是你說什麼話,這個機器人就回什麼話。
建立專案
就像一般的 Node.js 專案一樣,可以使用 npm init
指令開始建立專案,或是直接編寫 package.json 檔案來建立專案。有了 package.json 檔案之後,可以執行:
$ npm install botbuilder restify --save
這個指令把會用到的函式庫安裝起來,botbuilder
就是 Bot Builder SDK,而 restify
是為了要用它來建立一個 Web API app。指令執行完畢後就會寫進 package.json 檔案裡的 dependencies 段落。
以下是 package.json 檔案的參考。
{
"name": "myechobot",
"version": "1.0.0",
"description": "My First Echo Bot",
"dependencies": {
"botbuilder": "^3.8.4",
"restify": "^5.0.1"
}
}
若是直接修改 package.json 的內容,別忘了執行
npm install
的指令把函式庫及其相依的內容安裝完成。
寫機器人的程式
打開主要程式的 app.js 檔案,先用 restify 把 Web API app 的皮做出來。
const restify = require('restify');
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url);
});
這樣我們就開了一個會聽在連接埠 3978 的 Web API server。再來就是要設定一個位址讓 Bot Connector 可以把資訊送過來處理,這個位址是可以自行定義然後再到 Bot Connector 上設定的。Microsoft Bot Framework 官方的文件或範例都是使用 /api/messages
這個 URI 路徑。
為了處理由 Bot Connector 傳來的資訊,在 Bot Builder SDK 中我們可以使用 ChatConnector
來接收:
const builder = require('botbuilder');
// ...
let connector = new builder.ChatConnector();
server.post('/api/messages', connector.listen());
在新加入的程式碼中,我們建立了一個 ChatConnector
的物件 connector
,然後利用 restify 露出一個 /api/messages
的路徑(只接受 HTTP Post 呼叫),再把它交給 connector.listen()
來處理,如此一來,才能把 Bot Connector 傳來的資訊整理完畢後,進入對談機器人的本體來處理。
最後,就是產生對談機器人,然後把它接上 Chat Connector:
// ...
let bot = new builder.UniversalBot(connector, (session) => {
session.send(`您說「${session.message.text}」`);
});
在這裡我們建立了一個 UniversalBot
的物件,第一個參數帶入的就是 ChatConnector 的物件,而第二個參數的 function 則是描述這個 bot 在接收到對話時做的動作,裡面只做一件事,就是送回接收到的對話文字內容。
所以整個 app.js 的內容就是:
const restify = require('restify');
const builder = require('botbuilder');
let server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log('%s listening to %s', server.name, server.url);
});
let connector = new builder.ChatConnector();
server.post('/api/messages', connector.listen());
let bot = new builder.UniversalBot(connector, (session) => {
session.send(`您說「${session.message.text}」`);
});
如果在命令列執行 node app.js
(或是其它將這個程式跑起來的方式) 後沒有錯誤訊息,並且出現像是
restify listening to http://[::]:3978
的訊息,那就表示已經順利把這個 bot 跑起來,準備來測試或操作了。
測試機器人
由於這個 bot 是使用 Bot Builder SDK 開發的,而所有的訊息傳遞都要按照 Bot Connector 的通訊協定來做,在將這個 bot 部署到線上環境前,可以使用 Microsoft Bot Framework 所提供的 BotFramework Emulator 來進行測試,你可以在這裡下載各個平台的版本來使用。
將 bot 跑起來後,在 Bot Emulator 的上方位址列輸入 http://localhost:3978/api/messages
,因為這個位址列表示 Bot Connector 要 HTTP Post 資訊的端點,所以模擬器也會照著這個位址來送訊息,輸入位址後如果右下角的 Log 視窗沒有錯誤訊息的話,就表示已經接上 bot,然後你就能使用左側的聊天畫面開始跟 bot 進行對談。
在部署 bot 上線之前,一定要先使用這個模擬器先測試過,才能確保能正確與 Bot Connector 進行溝通。
如果要自己寫自動化測試,可以針對 Bot Connector 的通訊協定透過 HTTP Post 來測試 bot 的行為。