-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
120 lines (107 loc) · 3.91 KB
/
index.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
const boxSDK = require('box-node-sdk'); // Box SDK
const clarifai = require('clarifai'); // Clarifai SDK
const config = require('./config.js'); // Keys and config
const util = require('util'); // Deep inspection of objects
let indexerCallback;
let indexerEvent;
/**
* Process an incoming event
*
* @return {callback} lambda callback
*/
const processEvent = () => {
// Capture file ID and tokens from Box event
const { queryStringParameters, body } = indexerEvent;
const { source, token, id } = JSON.parse(body);
const fileId = source.id;
let readToken = token.read.access_token;
let writeToken = token.write.access_token;
// Create new Box SDK instance
const sdk = new boxSDK({
clientID: config.boxClientId,
clientSecret: config.boxClientSecret
});
let client = sdk.getBasicClient(writeToken);
// Instantiate a new Clarifai app instance
const app = new clarifai.App({
apiKey: config.clarifaiKey
});
// Create shared link to the file with write token
const fileURL = `https://api.box.com/2.0/files/${fileId}/content?access_token=${readToken}`;
// predict the contents of an image by passing in a url
app.models.predict(clarifai.GENERAL_MODEL, fileURL).then(
function(response) {
// Capture all categories
let entries = [];
for (let category of response.outputs[0].data.concepts) {
if (category.value > 0.9) {
entries.push({ type: 'text', text: category.name });
}
}
// Set Box metadata template information
const metadataTemplate = 'boxSkillsCards';
const metadata = {
cards: [{
created_at: new Date().toISOString(),
type: 'skill_card',
skill_card_type: 'keyword',
skill_card_title: {
message: 'Categories'
},
skill: {
type: 'service',
id: 'jleblanc-clarifai-heroku'
},
invocation: {
type: 'skill_invocation',
id: fileId
},
entries: entries
}]};
// Update Box metadata
client.files.addMetadata(fileId, client.metadata.scopes.GLOBAL, metadataTemplate, metadata).then((err, metadata) => {
console.log("ADDING----------------------------------------------------------------");
}).catch(function (err) {
if (err.response && err.response.body && err.response.body.code === 'tuple_already_exists') {
console.log("CONFLICT----------------------------------------------------------------");
const jsonPatch = [{ op: 'replace', path: '/cards/0', value: metadata.cards[0] }];
client.files.updateMetadata(fileId, client.metadata.scopes.GLOBAL, metadataTemplate, jsonPatch).then((err, metadata) => {
console.log("UPDATED----------------------------------------------------------------");
}).catch(function (err) {
console.log(err.response.body);
});
} else {
console.log(err.response.body);
}
});
},
function(err) {
console.error(err);
}
);
};
/**
* This is the main function that the Lambda will call when invoked.
* @return {boolean} - true if valid event
*/
const isValidEvent = () => {
return (indexerEvent.body);
};
/**
* This is the main function that the Lamba will call when invoked.
*
* @param {webhooksEvent} event - data from the event, including the payload of the webhook, that triggered this function call
* @param {context} context - additional context information from the request (unused in this example)
* @param {callback} callback - the function to call back to once finished
* @return {callback} lambda callback
*/
exports.handler = (event, context, callback) => {
// Set indexer information
indexerCallback = callback;
indexerEvent = event;
if (isValidEvent()) {
processEvent();
} else {
callback(null, { statusCode: 200, body: 'Event received but invalid' });
}
};