There are a lot of chatbot and text tools such as WhatsApp, WeChat, Slack, Facebook, and Messenger. In Japan, LINE is currently one of the more popular chat applications. This tutorial explains how you can integrate Watson™ Assistant into your LINE chatbot.

Application Image

Get Watson Assistant

To begin building your app:

  1. Select Watson Assistant from the IBM Cloud catalog.

    Application Image

  2. Enter a Service name, and select a Region (US South is recommended), Org, and Space. Select the Lite plan, and click Create.

    Application Image

Note the credential information when the Watson Assistant API is created. If the credential is not created automatically, you can generate a new credential from the service Credential menu. Keep track of the API Endpoint URL, user, and password.

Note: You do not need to perform this operation if Node-RED will be on IBM Cloud and bound with the Watson Assistant API.

Create a Watson Assistant workspace

The Watson Assistant Workspace is a workspace to train definitions and flows of chats for chatbot applications. It has a unique ID that an application uses to call the API.

  1. Select the Watson Assistant API that you created.

    Watson Assistant dashboard

  2. Launch the Watson Assistant Tool by clicking Launch tool.

    Watson Assistant dashboard launch tool

  3. Click Create new to open a new flow window and select the Flow import function. Click the arrow icon next to the Create button.

    Application Image

  4. Download the flow definition file, then select Choose JSON file to select the file.

    Add dialog skill page

Complete the steps to create the workspace. This tutorial only covers the basic installation. To customize it, you can continue to modify the workspace or create a new one.

Create Node-RED application

Next, you’ll create a Node-RED application on IBM Cloud.

  1. Click Starter Kit in the left menu, then select Node-RED Starter to create your Node-RED application.

    IBM Cloud catalog

  2. Bind the Watson Assistant API you created to your Node-RED application by selecting your Node-RED app.

    Selecting Node-REd app

  3. Click Connection in the left menu, then click Create connection to create a connection.

    Connection menu

  4. Select the default authentication and click Connect.

    Connect existing compatible service

After the restaging process completes, you can access your Node-RED Flow Editor. It’s simple to make a Node-RED application for this application. The following image shows the flow view.

Flow view

In the process, a server-side application on Node-RED is called from the LINE webhook. It calls the Watson Assistant API. If you want to see the complete flow definition, you can download it here. The first node “http in” is for the configuration URL to access the Node-RED application. This is already set as “/line_hook” in the flow you just downloaded.

Node-RED is just a Node.js application, so the application you create on it is also a Node.js application. IBM Cloud allocated a unique URL on your application, and this application has the following URL:

https://<Your Node-RED App Name>.mybluemix.net/<Path you set on "http in node">/

e.g.)https://fillgapapp01-nodered01.mybluemix.net/line_hook/

The first Function node getText is for keeping the LINE API reply token:

//set reply token on flow
flow.set(“replyToken”,msg.payload.events[0].replyToken);

return msg;

The first Change node is for replacing the speaking sentence from LINE to be accepted by the Watson Assistant API.

Edit change node window

For the Watson Assistant node, enter your Watson Assistant workspace ID. You have already bound Assistant to Node-RED, so you don’t need to configure the credentials.

Watson Assistant node

The second Change node is for replacing the parameter from the Watson Assistant response to msg.payload.optext.

Change node

The second Function node createReplyMessage creates the speaking sentence for replying to the LINE app:

var post_request = {
    "headers": {
        "content-type": "application/json; charset=UTF-8",
        "Authorization": " Bearer " + "{ACCESS TOKEN FROM LINE}"
    },
    "payload": {
        "replyToken": flow.get("replyToken"),
        "messages": [
            {
                "type": "text",
                "text": msg.payload.optext + "ฅ^•ﻌ•^ฅ"
            }
        ]
    }
};

return post_request;

The parameters that are already set for replying to talking sentences as POST requests on the Node-RED flow that you downloaded are:

  • Access token generated by LINE (described below)
  • Reply token on flow
  • Sentence text on text (with an ASCII art cat character)

Finally, set a reply URL for the LINE app on the HTTP request node:

https://api.line.me/v2/bot/message/reply

HTTP request node window

Changes for the LINE app

Create a LINE Developer account

You can log in as a LINE developer with your LINE account, which you can create on your phone’s LINE app.

Log in to LINE Developers.

Line developers login page

Create a provider

  1. Set your provider name.

    Setting the provider name

  2. Confirm and create.

    Confirming and creating screen

  3. Click Create Channel on the Messaging API tile.

    Messaging API tile

Create new channel

  1. Configure the information needed for the channel:

     - App Icon Image: less than 3 MB, JPEG/PNG/GIF/BMP
     - App Name: 20 characters or less
     - App Description: 500 characters or less
     - Plan: Developer Trial
     - Industries: Any
     - Email Address: Any
    
  2. Agree to the terms of service, then click the Agreement button. Note: The terms of service are only in Japanese.

    Terms of service

  3. Select the two boxes for the agreements and click Create.

    Agreements

Basic configuration for the channel

Click the tile for the channel you created.

Channel window

There should only be two options you need to work with:

  • Generate Channel access token (long-lived)
  • Set Webhook URL

For the Generate Channel access token:

  1. After the operation, you put this token in your Node-RED Function node.
  2. Click the issue button twice.

    Messaging settings

    Issue new channel token

Configure the following settings:

  • Use Webhooks: Enabled
  • Webhook URL: Your Node-RED app URL
  • Auto-reply messages: Disabled

Congratulations! You have completed the tutorial.

Check the LINE chat application

You can add this chatbot to your LINE friends with a QR code. You can find QR code at the bottom of the LINE Developers basic information page.

Mobile LINE App: Add Friend -> QR Code

QR code

Chatbot image

Conclusion

This tutorial explained how to integrate the Watson Assistant service to your LINE app to create a chatbot that’s easy to understand and easy to use.