Head First ChatBot

Speaker: Jie-Han Chen (jielite)

Introduction

今年,Bot 如雨後春筍般的迸出!
各大軟體公司都建立了自己聊天機器人的平台

  • Facebook
  • LINE
  • Telegram
  • Slack

目前知名的聊天機器人:Siri, Amazon Echo, Google Allo

多數人認為 Bot 的出現,在未來將取代 app。
下面是一段 Facebook Messenger Bot 的 Demo片段 (1m40s開始):

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

[Q] 為什麼各大公司都建立自己的 Bot 平台? Bot 真的有辦法取代 App 嗎? 你平常都怎麼使用 App ?

現在 bot 的平台的是利用手動輸入來詢問 bot,對於電腦的使用者或許還算習慣,但是目前的潮流已經往行動裝置移動了,未來還會有各種的穿戴式裝置。在這種情況下, bot 要怎麼取得優勢?

參考文章

Prerequisite 先備知識

如果你知道以下東西,在這次的 Talk 中會吸收的比較好,但是沒有也沒關係 :)

  • Basic Programming
  • 簡單的 Backend Knowledge
    • HTTP (post, get)
    • Database
    • URL routing
  • JSON format

如果你對 Backend Programming 不熟悉,這裡有一份很好的資源可以學習:
Udacity: Intro to Backend

Process 建構流程

  • 選擇一個 Bot 平台
  • Build A HTTP service, 跟你普通在寫後端一樣
  • 建立 HTTPS 憑證(可略)
  • 串接平台與你的 Server

如何建立一個 HTTPS Server?

使用的程式語言 Golang

Why Golang ?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

因為 gopher 很可愛 (圖片取自 Golang Taiwan)

看看 python ??

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

所以 Let it GO!!!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

但是平心而論,Python 在 自然語言的處理 / AI 方面有比較完善的資源。

使用平台 LINE

這次的 Talk, 我們選擇用 LINE 平台來搭建我們的聊天機器人。

Why LINE Platform?

簡單易上手,功能不複雜,可以很容易的讓你的朋友使用

Facebook 的 Messenger Bot 要建立粉絲團,在這之前的驗證會比較繁瑣,有機會我們可以之後再介紹。

LINE Chatbot

LINE ChatBot 架構原理:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

要建立 Line Chatbot, 我們是利用 LINE Messaging API 的功能

LINE Messaging API 可以讓我們接收來自使用者的訊息,也可以傳輸訊息給使用者,這中間就是由 LINE Server 處理。我們要建構的系統,就是圖上的右邊部分。(Messaging Servers + Database)

  • Messaging Server:
    處理使用者訊息,包含解讀與回應

  • Database:
    顧名思義用來儲存資料,例如將使用者需要的資料儲存在資料庫中,避免多次查訪需要的耗損的時間。

Get Started ! 讓我們正式開始吧!

首先到 LINE Messaging API - Getting Started 頁面,按照上面的步驟申請 LINE@ 帳號,然後開始使用 Messaging API

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

進入 LINE@Manager 後,要設定

  • 使用 Messging API
  • 允許 Webhook 傳訊息 (帳號設定 > Bot設定)
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

Go developers!

接下來進入開發者頁面 LINE Developers

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在這個頁面需要設定的資訊是:

  • Webhook URL
  • IP Whitelists

Webhook 在這裡簡單來說就是,當使用者的訊息傳送到 LINE Server 的時侯,會觸發額外要執行的事情。是一種 Event 的概念。Webhook URL, 也常稱做 Callback URL。(觸發我們的 Messaging Server 去 Handle 這些訊息)

在我們寫 backend programming 的時候,會為特定格式的 url 提供特定的 function。我們在這邊做 Bot 的時候,就是要實作這個 function。
Webhook 是什麼?

IP Whitelists 是用來設定,這個 Bot 允許哪些 IP來源 可以回應訊息。

我們再看看 LINE Developers 頁面還有什麼資訊?

  • Channel Secret
  • Channel Access Token
    這兩個 Channel 資訊,主要是讓 LINE Server 驗證我們發送的訊息是不是合法的。必須夾帶在 Request Header 中。同時,也用來驗證是不是真的是 LINE Server 發送的 HTTP request。

Coding

講了這麼多,要怎麼開發呢?

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

訊息的收發流程:

  1. LINE Server - message -> Our Server
    設定 callback url 等待 LINE Server 發送 HTTP request 到我們的 Server

  2. LINE Server <- message - Our Server
    我們的 Server 要發送 HTTP request 給 LINE Server,為了讓 LINE Server 知道我們是合法的人,所以必須在 Header 上。

實作方法

  • 土炮法!!利用 HTTP request package(library) + 自幹 HTTP request header
  • 使用 SDK (Software Development Kit)
    • 選好開發語言
    • RTFM: SDK document

LINE Echo bot

接著,我們來看看 LINE Messaging API 提供了哪些 Event 吧
Webhook event object

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

要怎麼知道我們製作的 Bot 是可以運行的呢?通常大家會先製作到讓他可以 echo.

怎麼 Echo?

  • Receive
    • 判別接收到的訊息類型(事件)
    • 訊息的內容是否能夠正確解讀(decode)
  • Reply
    • 發送到的人是不是正確的?

以下是 Echo bot 的 source code

語意分析

做一個聊天機器人,首要面對的問題就是 - 知道使用者的意圖到底是什麼 (辨別 Intent)?

這類的問題有個專有名詞:Nature Language Processing 自然語言處理(NLP)。

NLP 的處理手法:

  • Rule-Based System: 由一大堆 if-then 組成。在 LISP 中很常見。MIT AI Course

    • 建立 Rule 很傷腦筋
    • Rule 定義得不夠嚴謹會有邏輯漏洞
    • 要擴充的時候,看起來很雜亂
    • 不透過 web 傳輸,不用耗損頻寬,速度可能較快。

    掌蚊人 - 聊天機器人第一版:部分 Rule
    開放課程: Rule-Based System MIT AI Course

  • Config with Markup Language: AIML

    • 回答較單一,適合固定的回答
    • 回覆具有結構性,容易維護,外人也可以上手
    • 擴充性不佳
    • 具有 state 標籤,也許可以實作 state machine
  • AI engine (其實我也不太確定確切名詞)

    • Luis.ai Microsoft
    • API.ai 被 Google 收購
    • Wit.ai 被 Facebook 收購
    • 需要 training, training 必須要有嚴謹的方式,最好有專業背景。
    • 便於辨別 Intent (使用者意圖)
  • Python NLTK 也是不錯的工具

    • 斷詞
    • 建立語法樹

LUIS AI Engine

LUIS 的介面如下:

介面常用的部分是:

  • Intents
    辨別使用者意圖
  • Entities
    這裡我理解的是一句話包含的元素,像是時間,地點都可以當做是 entities。另外要注意的是 Luis.ai 一個 application 只允許 10 個 entities。

Demo LUIS Training

因為沒有錄影,只好拿 Microsoft 的影片XD

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Bot Demo

額外資源

自己动手做聊天机器人教程