This repository has been archived by the owner on Jan 21, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathalchemy.js
194 lines (165 loc) · 7.26 KB
/
alchemy.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
var AlchemyAPI = require('alchemy-api'),
config = require('./config'),
alchemy = new AlchemyAPI(config.alchemyApi.key);
/**
* Se verifica que el servicio este habilitado en cofig.js
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
*/
function validarEstado(botSend, from, callback) {
if (config.hangouts.services.alchemy) {
if(callback && typeof callback === "function"){
callback();
} else {
console.log("[ERROR][Alchemy] El Callback no esta presente o no es una función.");
}
} else {
botSend(from, "Alchemy Api ha sido desactivada por el jefe");
}
}
/**
* Función que envía al solicitante el estado del API (errores, estado real)
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
*/
exports.estado = function(botSend, from) {
validarEstado(botSend, from, function(){
alchemy.apiKeyInfo({}, function(err, response) {
if (err) {
botSend(from, "Tenemos un error: \n" + err);
} else {
botSend(from, '- Estado: ' + response.status + '\n- Consumido: ' + response.consumedDailyTransactions + '\n- Limite: ' + response.dailyTransactionLimit);
}
});
});
};
/**
* Función que envía al solicitante el analisis de una fotografía (errores, famosos, datos biométricos)
* Se cubren todos los posibles casos: fotos sin personas, personas famosas, personas no famosas, multiples personas...
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
* @param {string} message - Representa el mensaje enviado por el usuario en la petición
*/
exports.reconocimientoFacial = function(botSend, from, message) {
validarEstado(botSend, from, function(){
alchemy.imageFaces(message, {}, function(err, response) {
if (err) {
console.log(err);
botSend(from, err);
} else {
var imageFaces = response.imageFaces;
if (response.status === "OK") {
if (imageFaces.length !== 0) {
// Información mínima disponible
var respuesta = "";
for (var i = 0; i < imageFaces.length; i++) {
if (imageFaces[i].identity) {
// Es un famoso
respuesta += "- " + imageFaces[i].identity.name + " (" + imageFaces[i].identity.score + "%) " + imageFaces[i].identity.disambiguated.subType[1];
respuesta += "\n";
} else {
// No es un famoso
respuesta += "- Anónimo. Estimo una edad entorno a " + imageFaces[i].age.ageRange + " y es " + (imageFaces[i].gender.gender === 'MALE' ? 'Hombre' : 'Mujer');
respuesta += "\n";
}
}
botSend(from, "Yo veo... \n" + respuesta);
} else {
// Sin información mínima disponible
botSend(from, "¿De qué personas me hablas?");
}
} else {
botSend(from, "Mala suerte!\nAlchemy me ha mandado datos incorrectos.");
}
}
});
});
};
/**
* Función para detectar el idioma
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
* @param {string} message - Representa el mensaje enviado por el usuario en la petición
*/
exports.idioma = function(botSend, from, message) {
validarEstado(botSend, from, function(){
alchemy.language(message, {}, function(err, response) {
if (err) {
// Error de Conexión
console.log(err);
botSend(from, err);
} else {
if (response.language === "unknown") {
botSend(from, "Que idioma es ese?!");
} else {
botSend(from, "Eso es " + response.language);
if (response["iso-639-2"] && response["native-speakers"]) {
botSend(from, "- ISO 639: " + response["iso-639-2"] + "\n- Hablantes: " + response["native-speakers"]);
}
if (response.wikipedia) {
botSend(from, response.wikipedia);
}
}
}
});
});
};
/**
* Función para detectar el sentimeinto
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
* @param {string} message - Representa el mensaje enviado por el usuario en la petición
*/
exports.sentimiento = function(botSend, from, message) {
validarEstado(botSend, from, function(){
alchemy.sentiment(message, {}, function(err, response) {
if (err) {
console.log(err);
botSend(from, err);
} else {
var sentiment = "";
if (response.status === "OK") {
sentiment += "- Idioma: " + response.language;
if (response.docSentiment) {
sentiment += "\n- Tipo: " + response.docSentiment.type;
if (response.docSentiment.score) {
sentiment += " (" + response.docSentiment.score + ")";
}
}
botSend(from, sentiment);
} else {
botSend(from, "Alchemy me ha mandado datos incorrectos.\n"+response.statusInfo);
}
}
});
});
};
/**
* Función para detectar las emociones
* @param {function} botSend - Función que maneja los envios
* @param {string} from - Representa al usuario que lanzo la petición
* @param {string} message - Representa el mensaje enviado por el usuario en la petición
*/
exports.emociones = function(botSend, from, message) {
validarEstado(botSend, from, function(){
alchemy.emotions(message, {}, function(err, response) {
if (err) {
console.log(err);
botSend(from, err);
} else {
if (response.status === "OK") {
var emotionsData = "Mi análisis:\n";
emotionsData += "- Idioma: " + response.language + "\n";
emotionsData += "- Enfado (" + response.docEmotions.anger + ")\n";
emotionsData += "- Repugnancia (" + response.docEmotions.disgust + ")\n";
emotionsData += "- Miedo (" + response.docEmotions.fear + ")\n";
emotionsData += "- Alegría (" + response.docEmotions.joy + ")\n";
emotionsData += "- Tristeza (" + response.docEmotions.sadness + ")\n";
botSend(from, emotionsData);
} else {
botSend(from, "Alchemy me ha mandado datos incorrectos.\n"+response.statusInfo);
}
}
});
});
};