-
Notifications
You must be signed in to change notification settings - Fork 235
/
Copy pathbot.js
25 lines (25 loc) · 1.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
/*global module*/
'use strict';
const builder = require('claudia-bot-builder'),
fbTemplate = builder.fbTemplate,
ws = require('./src/ws'),
eTitle = entity => ((entity.label || '') + ' ' + (entity.description || '')),
format = text => (text && text.substring(0, 80));
module.exports = builder((request, apiReq) => {
apiReq.lambdaContext.callbackWaitsForEmptyEventLoop = false;
return ws.findEntities(request.text).then(r => r.results).then(entities => {
if (!entities.length) {
return `Unfortunately, could not find anything about ${request.text} in Wikidata`;
} else if (entities.length === 1) {
let title = 'Facts about ' + eTitle(entities[0]);
return ws.entityClaims(entities[0].id).then(claims => title + ':\n' + claims.join('\n'));
} else {
const generic = new fbTemplate.generic();
entities.slice(0, 9).forEach(entity => {
generic.addBubble(format(entity.label), format(entity.description))
.addButton('View Facts', entity.id);
});
return generic.get();
}
});
});