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 Firebase Cloud Functions. If you want to deploy on Express 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.
- Initialize Firebase project
cd line-dialogflow-adapter-firebase
firebase init
and select your project-id
to match your Dialogflow project.
-
Obtain the channel access token of your Line bot from Line Developer console.
-
Set up the config with
firebase functions:config:set line.channel_access_token="your-channel-access-token" dialogflow.project_id="your-project-id" dialogflow.language_code="your-language"
- Deploy the function with
firebase deploy --only functions
You should get the URL of your function as https://us-central1-your-project-id.cloudfunctions.net/webhook
and the link will also show up at your Firebase Functions Dashboard.
-
Go to your Firebase console. Select the Blaze pricing plan. Firebase function would not allow external API call using free plan 😱.
-
Go to the Line Channel Setting of your bot.
- Enable webhook and add the Webhook URL to point to
https://us-central1-your-project-id.cloudfunctions.net/webhook
. - Disable Auto-reply messages and Greeting messages
- Enable webhook and add the Webhook URL to point to
-
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
.