-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
77 lines (66 loc) · 2.14 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
const recast = require('recastai')
const rairec = {
/*
* The class to instantiate and pass as a recognizer to the MS Bot Framework IntentDialog constructor
*
* args:
* botToken: string - the client token for the target Recast.ai bot
* lang: string - the language to use when determining intent and entities in Recast.ai - e.g. "en"
*/
RecastRecognizer: function(botToken, lang) {
this.client = new recast.Client(botToken, lang)
this.recognize = function(context, callback) {
/*
* The (single) IIntentRecognizer interfac function
*
* @see https://docs.botframework.com/en-us/node/builder/chat-reference/interfaces/_botbuilder_d_.iintentrecognizer
*/
rairec.RecastRecognizer.textRequest(context.message.text, this.client, function(err, res) {
if (err) {
callback(err);
} else {
/*
* create the IIntent array from Recast.ai intents
*/
const responseIntents = []
if (res.intents) {
res.intents.forEach(function(intent) {
responseIntents.push({intent: intent.slug, score: intent.confidence})
})
}
/*
* create the IEntity array from Recast.ai entities
*/
const responseEntities = []
if (res.entities) {
res.entities.forEach(function(entity) {
responseEntities.push({
type: entity.name, entity: entity.raw, score: entity.confidence, details: entity
})
})
}
/*
* Return a IIntentRecognizerResult object, adding the Recsat.ai result and the initial context
*/
callback(null, {
entities: responseEntities, intents: responseIntents,
intent: (responseIntents[0] ? responseIntents[0].intent : "none"),
score: (responseIntents[0] ? responseIntents[0].score : 0),
recastResult: res, context: context
})
}
})
}
}
}
/*
* Static method that does the heavy lifting of calling Recast.ai
*/
rairec.RecastRecognizer.textRequest = function(utterance, client, callback) {
client.textRequest(utterance).then(function(res) {
callback(null, res)
}).catch(function(err) {
callback(err)
})
}
module.exports = rairec