Line-Dialogflow Adapter for Express

Line-Dialogflow Adapter helps pass events from Line Messaging API to Dialogflow. Normally, the events such as Follow event and Postback event are not supported out-of-the-box by Dialogflow yet. Hence, the bot can only send text query to get the response from Dialogflow. This code seeks to translate Line webhook events to Dialogflow custom events. Click here to see the bot in action.

*Note: This code will deploy the adapter on Express. If you want to deploy on Firebase Functions see here.

Quickstart

  1. Create a bot in Dialogflow. Remember your project-id and language. No need to set up the Integrations for Line.

  2. Enable the Dialogflow API in Google Cloud console for your project.

  3. Create new gcloud service account for your project and activate it to allow request to Dialogflow:

gcloud auth activate-service-account [ACCOUNT] --key-file=KEY_FILE
  1. Obtain the channel access token of your Line bot from Line Developer console.

  2. Set up the config in .env with

LINE_CHANNEL_ACCESS_TOKEN=
DIALOGFLOW_PROJECT_ID=
DIALOGFLOW_LANGUAGE_CODE=
PORT=
  1. Deploy express with
cd line-dialogflow-adapter-express
npm install
npm start
  1. Go to the Line Channel Setting of your bot.
    • Enable webhook and add the Webhook URL to point to your web server.
    • Disable Auto-reply messages and Greeting messages

*Tips: If you are developing locally. You can use ngrok to tunnel your request to local machine and use ngrok provided url as webhook.

  1. Go to Dialogflow console. For Default Welcome Intent, add LINE_FOLLOW event to greet your audience from Dialogflow!

Line webhook event to Dialogflow event

  • Message event is simply sent to Dialogflow as text.
  • Follow, Join, Beacon event are sent as custom Dialogflow events below:
Line Dialogflow
Follow LINE_FOLLOW
Join LINE_JOIN
Beacon LINE_BEACON
  • Postback event are sent as <EVENT_NAME> to Dialogflow with all the followed parameters. The parameters may be used in the Dialogflow responses as #your_event_name.param (ie. My store id is #your_event_name.storeid will return My store id is 1234 from the example below).
{  
   "type":"postback",
   "replyToken":"b60d432864f44d079f6d8efe86cf404b",
   "source":{  
      "userId":"U91eeaf62d...",
      "type":"user"
   },
   "timestamp":1513669370317,
   "postback":{  
      "data":"action=<EVENT NAME>&storeid=1234",
      "params":{  
         "datetime":"2017-12-25T01:00"
      }
   }
}

Feel free to customize the name of the predefined Dialogflow events and <EVENT_NAME> selector (default is action) in config.ts.

Documentation