-
Notifications
You must be signed in to change notification settings - Fork 0
/
rec.js
43 lines (31 loc) · 1.15 KB
/
rec.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
var yelp = require('yelp-fusion');
module.exports = function(ctx, cb) {
var getSearch = (token) => {
var params = ctx.body.text.split(' in '),
client = yelp.client(token);
if(!params[1]){
return cb(null, { text: 'What place do you want the recommendations for? [Example: /rec food in Reno]' });
}
client.search({
term: params[0],
location: params[1],
sort_by: 'rating'
}).then(response => {
var rec = ':bulb: Here are some recomendations: \n\n',
limit = (response.jsonBody.businesses.length > 3)? 3 : response.jsonBody.businesses.length;
for(i = 0; i < limit; i++) {
rec += response.jsonBody.businesses[i].name + ' - ' + response.jsonBody.businesses[i].rating + ':star: \n';
}
cb(null, { text: rec });
}).catch(e => {
console.log(e);
cb(null, { text: 'Sorry, something went wrong.' });
});
};
const token = yelp.accessToken(ctx.secrets.Y_CLIENT_ID, ctx.secrets.Y_CLIENT_SECRET).then(response => {
getSearch(response.jsonBody.access_token);
}).catch(e => {
console.log(e);
cb(null, { text: 'Sorry, something went wrong.' });
});
};