-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapp.js
59 lines (58 loc) · 1.51 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
'use strict';
const Readline = require('readline');
const rl = Readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
const matcher = require('./matcher');
const weather = require('./weather');
const {currentWeather, forecastWeather} = require('./parser');
rl.setPrompt('> ');
rl.prompt();
rl.on('line', reply => {
matcher(reply, data => {
switch(data.intent) {
case 'Hello':
console.log(`${data.entities.greeting} to you too!`);
rl.prompt();
break;
case 'Exit':
console.log("Have a great day!");
process.exit(0);
break;
case 'CurrentWeather':
console.log("Let me check...");
// get weather data from an API
weather(data.entities.city, 'current')
.then(response => {
let parseResult = currentWeather(response);
console.log(parseResult);
rl.prompt();
})
.catch(error => {
console.log("There seems to be a problem connecting to the Weather service!");
rl.prompt();
});
break;
case 'WeatherForecast':
console.log("Let me check...");
// get weather data from an API
weather(data.entities.city)
.then(response => {
let parseResult = forecastWeather(response, data.entities);
console.log(parseResult);
rl.prompt();
})
.catch(error => {
console.log("There seems to be a problem connecting to the Weather service!");
rl.prompt();
});
break;
default: {
console.log("I don't know what you mean :(");
rl.prompt();
}
}
});
});