-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
166 lines (157 loc) · 4.96 KB
/
helpers.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
const ai_token='';
var app = require('apiai')(ai_token);
const request = require('request');
var DB=require('./db');
var FACEBOOK_ACCESS_TOKEN='';
var SendResponse=exports.SendResponse=function(senderId, text){
console.log("sending response");
request({
url: 'https://graph.facebook.com/v2.6/393368691140430/messages?access_token=',
messaging_type: "RESPONSE",
method: 'POST',
json: {
recipient: { id: senderId },
message: { text:text },
}
});
DB.conn("Messages","i",{id:senderId,text:text,fromto:"bot to user"},{},function(){});
console.log("done");
};
var GetWeatherByCityName=exports.GetWeatherByCityName = function(id,cityname,func) {
request('http://api.openweathermap.org/data/2.5/weather?q='+cityname+'&APPID=ba25db8f381ce66103009bf7b240c530', function (error, response, body) {
//console.log('error:', error);
//console.log('statusCode:', response && response.statusCode);
console.log("body : "+body);
var c=JSON.parse(body);
var temp=JSON.stringify(c.main.temp);
var desc=JSON.stringify(c.weather[0].description);
func(temp,desc,id);
});
}
var GetWeatherByCoord=exports.GetWeatherByCoord = function(id,lat,long) {
request('http://api.openweathermap.org/data/2.5/weather?lat='+lat+'&lon='+long+'&APPID=ba25db8f381ce66103009bf7b240c530', function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:',body);
var c=JSON.parse(body);
var temp=JSON.stringify(c.main.temp);
var desc=JSON.stringify(c.weather[0].description);
var last =desc+" with temperature "+temp+" kelvin";
console.log("last = "+last);
SendResponse(id,last);
});
}
exports.SendText=function(txt,id,result)
{
console.log("------------------------>sending text to api ai");
console.log("txt = "+txt);
var req=app.textRequest(txt,{
sessionId:'hamada'
})
req.on('response', function(response,obj=result) {
console.log(response);
var cn="";
var long=0;
var lat=0;
var res=response['result'];
var par=res['parameters'];
var count = Object.keys(par).length;
var mess=res.fulfillment.speech;
var met=res['metadata'];
var intent=met['intentName'];
if(obj['city']!='')
{
console.log("user adeem");
if(count==0)
{
console.log("message = "+mess);
SendResponse(id,mess);
}
else
{
cn=par['geo-city'];
lat=par['lat'];
long=par['long'];
if(intent=='city')
{
console.log("cityyy");
SendResponse(id,"what about "+cn+"?");
}
else if(cn!=''&&cn!=null&&cn!=undefined)
{
console.log("weather city",cn);
GetWeatherByCityName(id,cn,function(temp,desc,id){
var last =desc+" with temperature "+temp+" kelvin";
console.log("last = "+last);
SendResponse(id,last);
});
}
else if(lat!=''&&long!=''&&lat!=undefined&&long!=undefined)
{
console.log("weather geo");
console.log("lat = "+lat+" long = "+long);
GetWeatherByCoord(id,lat,long);
}
else if(intent=="weather")
{
console.log("btngan");
SendResponse(id,"I can see you are asking about the weather but i cannot find the city can you recheck the spelling ?");
}
else if(intent=="clothes")
{
console.log("clothes");
console.log("obj city = ",obj['city']);
GetWeatherByCityName(id,obj['city'],function(temp,desc,id){
var last="";
if(temp<=278)
{
last="it's freezing outside don't go out unless your are going to make snowmen :D, wear every thing you have in your closet";
}
else if(temp>=279&&temp<=288)
{
last="it's very cold oustide maybe you will need 2 or 3 jackets/sweaters ";
}
else if(temp>=289&&temp<=295)
{
last="it's a bit cold outside maybe a hoodie will do the job";
}
else if(temp>=296&&temp<=302)
{
last="it's a nice weather today please take me with you out ! :D you can wear a shirt or a t-shirt with no worries";
}
else if(temp>=303)
{
last="it's hot outside maybe you will need a short ";
}
console.log("last = "+last);
SendResponse(id,last);
});
}
}
}
else
{
if(intent=='city')
{
cn=par['geo-city'];
if(cn!='')
{
DB.conn("users",'u',{id:id},{$set:{id:id,city:cn}},function(){});
SendResponse(id,"Great now i can help you with the weather in any area in the world , i can also help you to choose what to wear for your outings , how can you do this ? just type your question like what should i wear or what is the weather in any city/Country in the world");
}
else
{
SendResponse(id,"I didn't get the city");
}
}
else
{
SendResponse(id,"Please tell me your city");
}
}
});
req.on('error', function(error) {
console.log(error);
});
req.end();
}