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.
-
Create a bot in Dialogflow. Remember your
project-id
andlanguage
. No need to set up the Integrations for Line. -
Enable the Dialogflow API in Google Cloud console for your project.
-
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
-
Obtain the channel access token of your Line bot from Line Developer console.
-
Set up the config in
.env
with
LINE_CHANNEL_ACCESS_TOKEN=
DIALOGFLOW_PROJECT_ID=
DIALOGFLOW_LANGUAGE_CODE=
PORT=
- Deploy express with
cd line-dialogflow-adapter-express
npm install
npm start
- 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.
- Go to Dialogflow console. For
Default Welcome Intent
, addLINE_FOLLOW
event to greet your audience from Dialogflow!
- 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 returnMy 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
.