Skip to content

Instantly share code, notes, and snippets.

@naranjja
Created June 25, 2018 21:50
Show Gist options
  • Save naranjja/6ab4d2b1510e6782f631e7b1e097d929 to your computer and use it in GitHub Desktop.
Save naranjja/6ab4d2b1510e6782f631e7b1e097d929 to your computer and use it in GitHub Desktop.
Sample Dialogflow API 2.0 Chatbot Fulfillment
require("dotenv").config()
const express = require("express")
const bodyParser = require("body-parser")
const { WebhookClient } = require("dialogflow-fulfillment")
const app = express()
app.use(bodyParser.urlencoded({ extended: true }))
app.use(bodyParser.json())
app.post("/", (request, response) => {
// https://github.com/dialogflow/dialogflow-fulfillment-nodejs/blob/master/docs/WebhookClient.md
const agent = new WebhookClient({ request, response })
const actionHandler = (agent) => {
switch (agent.action) {
case "test-action":
agent.add("you triggered test-action!")
break
default:
agent.add("you triggered an unhandled action")
break
}
}
agent.handleRequest(actionHandler)
})
const port = process.env.PORT || 8080
app.listen(port, () => console.log(`Webhook listening on port ${port}`))
{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"prod": "cross-env NODE_ENV=production PORT=8080 node index.js",
"dev": "nodemon index.js",
"start": "npm run prod",
"test": "mocha"
},
"keywords": [],
"author": "Jose Naranjo",
"license": "UNLICENSED",
"devDependencies": {
"mocha": "^5.2.0",
"nodemon": "^1.17.5"
},
"dependencies": {
"actions-on-google": "^2.2.0",
"body-parser": "^1.18.3",
"cross-env": "^5.2.0",
"dialogflow-fulfillment": "^0.4.1",
"dotenv": "^6.0.0",
"express": "^4.16.3",
"fb-messenger": "^1.2.0",
"moment": "^2.22.2",
"moment-timezone": "^0.5.21",
"request-promise": "^4.2.2",
"sleep-promise": "^8.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment