-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
116 lines (105 loc) · 3.04 KB
/
bot.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
const botBuilder = require('claudia-bot-builder');
const axios = require('axios');
const _ = require('lodash');
const { skypeTemplate } = botBuilder;
const botOptions = {
platforms: ['skype'],
};
// Sample Spoonacular response for /mealplanner/generate
// eslint-disable-next-line no-unused-vars
const recipeResponse = {
data: {
meals: [
{
id: 1109176,
imageType: 'jpg',
title: 'Vegan Birthday Cake Cinnamon Rolls',
readyInMinutes: 45,
servings: 12,
sourceUrl:
'https://www.godairyfree.org/recipes/vegan-birthday-cake-cinnamon-rolls',
},
{
id: 358788,
imageType: 'jpeg',
title: 'Smoked Turkey Baked Chimichangas',
readyInMinutes: 27,
servings: 4,
sourceUrl:
'http://www.foodnetwork.com/recipes/rachael-ray/smoked-turkey-baked-chimichangas-recipe.html',
},
{
id: 458444,
imageType: 'jpg',
title: 'Italian Cassata',
readyInMinutes: 45,
servings: 8,
sourceUrl: 'http://allrecipes.com/Recipe/Italian-Cassata/',
},
],
nutrients: {
calories: 1825.55,
protein: 56.77,
fat: 68.42,
carbohydrates: 248.89,
},
},
};
const { SPOONACULAR_API_KEY } = process.env;
const toSCIENCE = (message) => {
return message
.split('')
.reduce((memo, c) => {
const uppered = c.toUpperCase();
const scienced = /^[a-z]$/i.test(uppered) ? `${uppered}.` : uppered;
return [...memo, scienced];
}, [])
.join('');
};
const getRecipe = () => {
return axios
.get('https://api.spoonacular.com/mealplanner/generate', {
params: {
apiKey: SPOONACULAR_API_KEY,
timeFrame: 'day',
},
})
.then((response) => {
// The response has 3 meals (breakfast, lunch and dinner) and this randomly
// picks lunch or dinner.
const recipe = response.data.meals[1 + _.random(1)];
const { id: recipeID, imageType, sourceUrl, title } = recipe;
const recipeImageUrl = `https://spoonacular.com/recipeImages/${recipeID}-312x150.${imageType}`;
return new skypeTemplate.Carousel('summary')
.addHero([recipeImageUrl])
.addTitle(title)
.addButton('View', sourceUrl, 'openUrl')
.get();
})
.catch((error) => error.message);
};
const help = () => {
const commands = ['/science'];
const response = 'Commands:\r\n';
return response + commands.join('\r\n');
};
const commandNotFound = () => {
return "Something I don't understand? Inconceivable!";
};
const requestHandler = (message) => {
// Strip bot name if mentioned in a group chat
const text = message.text.replace(/^(vizzini)? /, '');
const textParts = text.split(' ');
const commandArgs = textParts.slice(1).join(' ');
switch (textParts[0]) {
case '/recipe':
return getRecipe();
case '/science':
return toSCIENCE(commandArgs);
case '/help':
return help();
default:
return commandNotFound(message);
}
};
module.exports = botBuilder(requestHandler, botOptions);