iT邦幫忙

2018 iT 邦幫忙鐵人賽
DAY 8
0
AI & Machine Learning

Microsoft Bot Framework 30天上手 + 自幹 Line Builder 串接 + Line Bot Demo系列 第 8

Dialog , Prompt

botframework有一個很好的地方,
就是你可以設定,
這串Dialog 預期收到的資料格式,
使用 Prompt來幫你做過瀘錯誤語的資料。

目前有支援六種資料格式

  • Prompts.text
    文字.
  • Prompts.confirm
    預期就是True Or false.
  • Prompts.number
    數字.
  • Prompts.time
    時間格式.
  • Prompts.choice
    只能回覆提供的選項之一
  • Prompts.attachment
    照片,影片,聲音,等檔案格式.
// Ask the user for their name and greet them by name.
bot.dialog('greetings', [
    function (session) {
        session.beginDialog('askName');
    },
    function (session, results) {
        session.endDialog(`Hello ${results.response}!`);
    }
]);
bot.dialog('askName', [
    function (session) {
        builder.Prompts.text(session, 'Hi! What is your name?');
    },
    function (session, results) {
        session.endDialogWithResult(results);
    }
]);

這樣就可以獲得你需要的資料格式了.

Prompts.text 除了 檔案以外,什麼都成立了

builder.Prompts.text(session, "What is your name?");
s.response //就是 回文

Prompts.confirm

builder.Prompts.confirm(session, "Are you sure you wish to cancel your order?");
//s.response 只有會 true / false

Prompts.number

builder.Prompts.number(session, "How many would you like to order?”);
//自動轉譯成數字 例如 nine 會變 9

Prompts.time

builder.Prompts.time(s, "What time would you like to set an alarm for?”);
21:00 at today
{ type: 'chrono.duration',
  entity: '21:00 at today',
  startIndex: 0,
  endIndex: 14,
  resolution:
   { resolution_type: 'chrono.duration',
     start: 2017-12-14T13:00:00.000Z,
     ref: 2017-12-14T15:00:28.000Z },
  score: 1 }

Prompts.choice:

builder.Prompts.choice(session, "Which color?", ["red","green","blue"]);
//s.response { score: 1, index: 1, entity: 'green’ }
//也可以全部自已訂義
var salesData = {
    "west": {
        units: 200,
        total: "$6,000"
    },
    "central": {
        units: 100,
        total: "$3,000"
    },
    "east": {
        units: 300,
        total: "$9,000"
    }
};


bot.dialog('getSalesData', [
    function (session) {
        builder.Prompts.choice(session, "Which region would you like sales for?", salesData); 
    },
    function (session, results) {
        if (results.response) {
            var region = salesData[results.response.entity];
            session.send(`We sold ${region.units} units for a total of ${region.total}.`); 
        } else {
            session.send("OK");
        }
    }
]);

Prompts.attachment:

builder.Prompts.attachment(session, "Upload a picture for me to transform.”);
//就是要傳檔案給它,line有傳地址的功能,也只能用這個實現

上一篇
Dialogs
下一篇
Manage conversation flow with dialogs
系列文
Microsoft Bot Framework 30天上手 + 自幹 Line Builder 串接 + Line Bot Demo30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言