Advertisement
Guest User

Untitled

a guest
Aug 28th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. const express = require('express');
  2. const router = express.Router();
  3.  
  4. router.post('/conversation/', (req, res) => {
  5. const { text, context = {} } = req.body;
  6.  
  7. const params = {
  8. input: { text },
  9. workspace_id:'Workspace-id',
  10. context,
  11. };
  12.  
  13. assistant.message(params, (err, response) => {
  14. if (err) res.status(500).json(err);
  15.  
  16. res.json(response);
  17. });
  18. });
  19.  
  20. module.exports = router;
  21.  
  22. /*eslint-env node*/
  23. 'use strict';
  24. // This application uses express as its web server
  25. // for more info, see: http://expressjs.com
  26. const express = require('express');
  27. const bodyParser = require('body-parser');
  28. const path = require('path');
  29.  
  30.  
  31. // cfenv provides access to your Cloud Foundry environment
  32. // for more info, see: https://www.npmjs.com/package/cfenv
  33. const cfenv = require('cfenv');
  34. // create a new express server
  35. const app = express();
  36.  
  37. const AssistantV1 = require('watson-developer-cloud/assistant/v1');
  38.  
  39. const assistant = new AssistantV1({
  40. username: 'username',
  41. password: 'pass',
  42. url: 'https://gateway.watsonplatform.net/assistant/api/',
  43. version: '2018-02-16',
  44. });
  45.  
  46. //Configuring the environment
  47. //Use body-parser to nodejs understand the user requests
  48. app.use(bodyParser.urlencoded({extended:false}));
  49. app.use(bodyParser.json());
  50. //establish the views directory
  51. app.set('views',path.join(__dirname,'views')); //__dirname get the main tab project
  52. //establish the views engine
  53. app.set('view engine','ejs');
  54.  
  55. // serve the files out of ./public as our main files
  56. app.use(express.static(__dirname + '/public'));
  57.  
  58. // get the app environment from Cloud Foundry
  59. var appEnv = cfenv.getAppEnv();
  60.  
  61. //Routes
  62. app.use('/', require('./routes/index'));
  63.  
  64.  
  65. // start server on the specified port and binding host
  66. app.listen(appEnv.port, appEnv.bind, () => {
  67. // print a message when the server starts listening
  68. console.log("server starting on " + appEnv.url);
  69.  
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement