cherry, cherry!

Memento coding

Tuesday, 17 March 2020

How to develop a backend for your Action using Heroku

Actions fulfillment can be done not only via Cloud Functions but also with a webhook.

Heroku is a nice free service you can deploy your backend on: porting Cloud Functions or Dialogflow inline editor fulfillment is pretty easy to do!

After creating a node.js project in Heroku, just change package.json as the following, to include AoG package:
 {  
  "name": "node-js-getting-started",  
  "version": "0.3.0",  
  "description": "A sample Node.js app using Express 4",  
  "engines": {  
   "node": "8"  
  },  
  "main": "index.js",  
  "dependencies": {  
   "actions-on-google": "^2.9.1-preview.1",  
   "ejs": "^2.5.6",  
   "express": "^4.15.2"  
  },  
  "devDependencies": {  
   "request": "^2.81.0",  
   "tape": "^4.7.0"  
  },  
  "repository": {  
   "type": "git",  
   "url": "https://github.com/heroku/node-js-getting-started"  
  },  
  "keywords": [  
   "node",  
   "heroku",  
   "express"  
  ],  
  "license": "Apache-2.0"  
 }
Now it’s time to change index.js, implementing a small express webserver with the following boilerplate code:
 'use strict';  
 const express = require('express')  
 const bodyParser = require('body-parser')  
 const path = require('path')  
 const PORT = process.env.PORT || 5000  
 const {dialogflow, HtmlResponse} = require('actions-on-google');  
 const app = dialogflow({debug: true});  
 <insert your intents fullfilment here>  
 express().use(bodyParser.json(), app).listen(PORT);
Replace the placeholder with intents fulfillment from inline editor or cloud function js file:
 app.intent('Welcome', (conv) => {  
 console.log('welcome');  
        conv.ask('Welcome!');  
  }  
 });  
Push the change to the Heroku repository and if you didn’t do so, take note of Heroku project URL (you can find it in project settings).

Back to Dialogflow, update the fulfillment URL with the one of the Heroku project:


Be careful to match the port number, too.

You can now use Dialogflow emulator to test Action behaviour!🙌🙌🙌

No comments:

Post a Comment