-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (75 loc) · 3.11 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const request = require('request');
var ConversationV1 = require('watson-developer-cloud/conversation/v1');
var prompt = require('prompt-sync')();
var conversation = new ConversationV1({
username: '06d3c964-0845-4df9-a3fd-1e0bfa7491fd', // replace with username from service key
password: 'SEmZHBdVfAZC', // replace with password from service key
path: { workspace_id: 'd1f0d0b6-a67b-4516-8235-a7f4ca826fa1' }, // replace with workspace ID
version_date: '2017-11-15'
});
let apiKey = v;
let city = 'Portland';
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}`
// Start conversation with empty message.
conversation.message({}, processResponse);
function processResponse(err, response) {
if (err) {
console.error(err); // something went wrong
return;
}
// Display the output from dialog, if any.
if (response.output.text.length != 0) {
console.log(response.output.text[0]);
}
if (response.intents.length > 0) {
if(response.intents[0].intent == 'Weather'){
if(response.entities.length > 0){
console.log(response.entities);
let city = response.entities[0].value;
let url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&units=imperial&appid=${apiKey}`
request(url, function (err, response, body) {
if(err){
console.log('error:', error);
}
else {
let weather = JSON.parse(body);
//console.log(weather);
let message = `It's ${weather.main.temp} degrees in ${weather.name}!`;
console.log(message);
var newMessageFromUser = prompt('>> ');
text = newMessageFromUser;
conversation.message({
input: { text: newMessageFromUser },
context : response.context,
}, processResponse);
}
});
}
else{
//no city inputted
var newMessageFromUser = prompt('>> ');
text = newMessageFromUser;
conversation.message({
input: { text: newMessageFromUser },
context : response.context,
}, processResponse);
}
}
else{
var newMessageFromUser = prompt('>> ');
text = newMessageFromUser;
conversation.message({
input: { text: newMessageFromUser },
context : response.context,
}, processResponse);
}
}
else{
var newMessageFromUser = prompt('>> ');
text = newMessageFromUser;
conversation.message({
input: { text: newMessageFromUser },
context : response.context,
}, processResponse);
}
}