-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
39 lines (31 loc) · 933 Bytes
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
require('dotenv').config();
const AssistantV1 = require('watson-developer-cloud/assistant/v1');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.json());
app.use(express.static('./public'));
const port = 3000;
const assistant = new AssistantV1({
username: process.env.ASSISTANT_USERNAME,
password: process.env.ASSISTANT_PASSWORD,
url: 'https://gateway.watsonplatform.net/assistant/api/',
version: '2018-02-16',
});
app.post('/conversation/', (req, res) => {
const { text, context = {} } = req.body;
const params = {
input: { text },
workspace_id: process.env.WORKSPACE_ID,
context,
};
assistant.message(params, (err, response) => {
if (err) {
console.error(err);
res.status(500).json(err);
} else {
res.json(response);
}
});
});
app.listen(port, () => console.log(`Running on port ${port}`));