-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
273 lines (257 loc) · 18.6 KB
/
server.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
'use strict';
const conf = require('./config')
const tmbd = require('./tmbd')
const express = require ('express') ;
const server = express() ;
const bodyparser = require('body-parser');
const matcher = require ('./vanilla/matcher') ; // to use the matcher module here
const weather = require ('./vanilla/weather'); // to use the weather module here
const forecast = require ("./vanilla/forecast"); // to use the forecast module
const PORT = process.env.PORT || 3000;
//server.get('/', (req, res ) => res.send ("hello!!")) ;
server.listen (PORT, () => console .log ('The bot server is running on port', PORT)) ;
const fBeamer = require('./fbeamer');
const { createPrivateKey } = require('crypto');
const { response } = require('express');
const f = new fBeamer(conf.FB)
server.get ('/', (req , res ) => f.registerHook (req , res )) ;
server.post ('/', bodyparser.json ({
verify : f.verifySignature.call(f)
})) ;
server.post ('/', (req , res , next ) =>{
// message will be received here if the signature is verified
// the message will be passed to FBeamer for parsing
return f.incoming(req, res,async data=>{
const {message,sender} = data;
//console.log (message.nlp)
data=f.messageHandler(data);
//console.log(data)
try {
if (data.content === 'the given text in facebook !'){
await f.txt(data.sender , 'hey there I finally recieved your message in my chatbot :)') ;
}
else if(data.type==='image') {
await f.img(data.sender , 'https://www.mindtools.com/blog/wp-content/uploads/2016/08/GI_184953226_Lisa_Blue.jpg') ;
}
else if(message.nlp.intents&&message.nlp.intents.length&&(message.nlp.intents[0].name=="movieinfo"||message.nlp.intents[0].name=="releaseYear")){
tmbd(message.nlp).then(async result=> {
var resp=`Movie Release Date: ${result[0]}\nMovie Original title: ${result[1]}\nMovie ID: ${result[2]}\nOverview: ${result[3]}`
await f.txt(data.sender , resp) ;
await f.img(data.sender , result[4]) ;
})
.catch(async error => {
//console.log(error)
await f.txt(data.sender , "No data found, search again please!") ;
});
}
else if(message.nlp.intents&&message.nlp.intents.length&&(message.nlp.intents[0].name=="movieinfo"||message.nlp.intents[0].name=="director")){
tmbd(message.nlp).then(async result=> {
await f.txt(data.sender , result) ;
})
.catch(async error => {
//console.log(error)
await f.txt(data.sender , "No data found, search again!") ;
});
}
else{
matcher (data.content , async cb => {
switch(cb.intent){
case 'Hello':
//console.log('BOT: Hello and welcome to chatbot!');
//console.log('Entities:',cb.entities.greeting)
await f.txt(data.sender , 'Welcome to chatbot! :)') ;
break;
case 'Exit':
//console.log('BOT: Bye Bye!')
await f.txt(data.sender , 'Bye Bye!') ;
process.exit(0)
case 'Current Weather':
weather(cb.entities.city).then(async result=> {
var weaDesc = result.weather[0].description
var city = result.name
var countries = require("i18n-iso-countries");
var country = result.sys.country
var con = countries.getName(country, "en", {select: "official"})
var temp= result.main.temp
console.log("BOT: It is",weaDesc.red,"in",city.green,",",con.cyan,"with",temp,"degrees Celsius")
var output= `It is ${weaDesc} in ${city}, ${con} with ${temp} degrees Celsius`
await f.txt(data.sender , output) ;
}
)
.catch(async error => {
console.log(error.response.data.message.red)
await f.txt(data.sender , error.response.data.message) ;
});
break;
case 'Weather Forecast':
//console.log('Entities:',cb.entities.weather,cb.entities.city,cb.entities.time);
forecast(cb.entities.weather,cb.entities.city,cb.entities.time).then(async result=> {
var city = result.city_name
var countries = require("i18n-iso-countries");
var country = result.country_code
var weatherCond=cb.entities.weather
var time=cb.entities.time
var con = countries.getName(country, "en", {select: "official"})
if(time.toLowerCase()==="today"){
var weaCon = result.data[0].weather.description
console.log(weaCon)
var temp = result.data[0].temp
var date = result.data[0].datetime
if(weatherCond==="cold" && temp<15.0){
console.log("BOT: Yes, it's cold!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it's cold! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if((weatherCond.includes("sun")&&weaCon.toLowerCase()==="clear sky") && temp>15.0){
console.log("BOT: Yes, it's sunny!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it's sunny! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond==="hot" && temp>30.0){
console.log("BOT: Yes, it's hot!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it's hot! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("rain") && weaCon.includes("rain")){
console.log("BOT: Yes, it's raining!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it's raining! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("cloud") && weaCon.includes("cloud")){
console.log("BOT: Yes, it's cloudy!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it's cloudy! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else{
console.log("BOT: It's",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `No, it's ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
}
else if(time.toLowerCase()==="tomorrow"){
var weaCon = result.data[1].weather.description
var temp = result.data[1].temp
var date = result.data[1].datetime
if(weatherCond==="cold" && temp<15.0){
console.log("BOT: Yes, it'll be cold!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be cold! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if((weatherCond.includes("sun")&&weaCon.toLowerCase()==="clear sky") && temp>15.0){
console.log("BOT: Yes, it'll be sunny!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be sunny! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond==="hot" && temp>30.0){
console.log("BOT: Yes, it'll be hot!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be hot! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("rain") && weaCon.includes("rain")){
console.log("BOT: Yes, it'll be raining!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be raining! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("cloud") && weaCon.includes("cloud")){
console.log("BOT: Yes, it'll be cloudy!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be cloudy! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else{
console.log("BOT: It'll be",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `No, it'll be ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
}
else{
var weaCon = result.data[2].weather.description
var temp = result.data[2].temp
var date = result.data[2].datetime
if(weatherCond==="cold" && temp<15.0){
console.log("BOT: Yes, it'll be cold!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be cold! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if((weatherCond.includes("sun")&&weaCon.toLowerCase()==="clear sky") && temp>15.0){
console.log("BOT: Yes, it'll be sunny!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be sunny! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond==="hot" && temp>30.0){
console.log("BOT: Yes, it'll be hot!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be hot! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("rain") && weaCon.includes("rain")){
console.log("BOT: Yes, it'll be raining!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be raining! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(weatherCond.includes("cloud") && weaCon.includes("cloud")){
console.log("BOT: Yes, it'll be cloudy!",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `Yes, it'll be cloudy! ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else{
console.log("BOT: It'll be",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `No, it'll be ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
}
})
.catch(async error => {
console.log("BOT: ","Invalid input, couldn't get data...")
await f.txt(data.sender , "Invalid input, couldn't get data...") ;
});
break;
case 'get weather':
//console.log('The weather in Paris is 4 degrees...')
//console.log('Entities:',cb.entities.city,',',cb.entities.time)
forecast("0",cb.entities.city,cb.entities.time).then(async result=> {
var city = result.city_name
var countries = require("i18n-iso-countries");
var country = result.country_code
var time=cb.entities.time
var con = countries.getName(country, "en", {select: "official"})
if(time.toLowerCase()==="today"){
var weaCon = result.data[0].weather.description
var temp = result.data[0].temp
var date = result.data[0].datetime
console.log("BOT: It's",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `It's ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else if(time.toLowerCase()==="tomorrow"){
var weaCon = result.data[1].weather.description
var temp = result.data[1].temp
var date = result.data[1].datetime
console.log("BOT: It'll be",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `It'll be ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
else{
var weaCon = result.data[2].weather.description
var temp = result.data[2].temp
var date = result.data[2].datetime
console.log("BOT: It'll be",weaCon.red,"in",city.green,"(",con.yellow,")", time,"(",date.cyan,")","with temperature",temp,"°C")
var output= `It'll be ${weaCon} in ${city}, (${con}), ${time}, (${date}) with ${temp} °C`
await f.txt(data.sender , output) ;
}
})
.catch(async error => {
console.log("BOT: ","Invalid input, couldn't get data...".red)
await f.txt(data.sender , "Invalid input, couldn't get data...") ;
});
break;
default:
console.log('No intent identified');
await f.txt(data.sender , "Sorry! I couldn't understand... :(") ;
}
}) ;
}
}
catch(e){
console.log(e) ;
}
});
}) ;