-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·146 lines (130 loc) · 4.41 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const Botmaster = require('botmaster');
const R = require('ramda');
const {fulfillOutgoingWare} = require('botmaster-fulfill');
const standardActions = require('botmaster-fulfill-actions');
const watsonConversationStorageMiddleware = require('./watson_conversation_storage_middleware');
const watson = require('watson-developer-cloud');
const cfenv = require('cfenv');
const request = require('request-promise');
const myActions = {
weather: {
controller: function(params) {
return getWeather(params)
.then(function(result) {
console.log(result);
return 'I thought you might like a weather forecast for that location.<pause />' + result;
})
.catch(function(err) {
console.log(err);
return 'Sorry, not weather forecast available at the moment.';
});
}
},
buttons: {
controller: (params) => {
const buttonTitles = params.content.split(',');
params.update.message.quick_replies = [];
for (const buttonTitle of buttonTitles) {
params.update.message.quick_replies.push({
content_type: 'text',
title: buttonTitle,
payload: buttonTitle,
});
}
return '';
},
},
locButton: {
controller: (params) => {
params.update.message.quick_replies = [];
params.update.message.quick_replies.push({
content_type: 'location',
});
return '';
},
},
};
const actions = R.merge(standardActions, myActions);
const appEnv = cfenv.getAppEnv();
const watsonConversation = watson.conversation({
username: process.env.WATSON_CONVERSATION_USERNAME,
password: process.env.WATSON_CONVERSATION_PASSWORD,
version: 'v1',
version_date: '2016-05-19',
});
const messengerSettings = {
credentials: {
verifyToken: process.env.FACEBOOK_VERIFY_TOKEN,
pageToken: process.env.FACEBOOK_PAGE_TOKEN,
fbAppSecret: process.env.FACEBOOK_APP_SECRET,
},
// !! see Readme if you have any issues with understanding webhooks
webhookEndpoint: '/webhook/',
};
const botsSettings = [{ messenger: messengerSettings }];
const botmasterSettings = {
botsSettings,
port: appEnv.isLocal ? 3000 : appEnv.port,
};
const botmaster = new Botmaster(botmasterSettings);
botmaster.use('incoming', watsonConversationStorageMiddleware.retrieveSession);
botmaster.use('outgoing', fulfillOutgoingWare({
actions
}));
botmaster.on('update', (bot, update) => {
console.log(update);
const messageForWatson = {
context: update.session.context,
workspace_id: process.env.WATSON_WORKSPACE_ID,
input: {
text: update.message.text,
},
};
watsonConversation.message(messageForWatson, (err, watsonUpdate) => {
watsonConversationStorageMiddleware.updateSession(update.sender.id, watsonUpdate);
const text = watsonUpdate.output.text[0];
if (watsonUpdate.output.action === 'weather') {
const requestOptions = {
url: 'https://twcservice.mybluemix.net/api/weather/v1/geocode/52.379189/4.899431/forecast/daily/3day.json?language=en-US&units=e',
auth: {
user: '68e5c50b-4f4f-4ea4-acff-bdc55a244a83',
pass: '72cQFTI93X',
sendImmediately: true,
},
json: true,
}
request(requestOptions)
.then((body) => {
const someText = body.forecasts[0].narrative;
return bot.reply(update, someText)
})
.catch((err) => {
console.log(err);
})
} else {
const watsontext = watsonUpdate.output.text;
bot.sendTextCascadeTo(watsontext,update.sender.id)
}
});
});
botmaster.on('server running', (message) => {
console.log(message);
});
botmaster.on('error', (bot, err) => {
console.log(err.stack);
});
function getWeather(params) {
const lat = params.content.split(',')[0];
const long = params.content.split(',')[1];
const requestOptions = {
url: 'https://twcservice.mybluemix.net/api/weather/v1/geocode/' + lat + '/' + long + '/forecast/daily/3day.json?language=en-US&units=e',
auth: {
user: 'WEATHER_USER_ID',
pass: 'WEATHER_PASSWORD',
sendImmediately: true,
},
json: true,
};
return request(requestOptions)
.then((body) => body.forecasts[0].narrative);
}