-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
88 lines (71 loc) · 2.06 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
console.log('Loading function')
const r = require('recastai').default
const request = require('request')
const recastClient = new r.build(process.env.REQUEST_TOKEN)
let sessionId = null
let endConv = false
let done = null
// Format reply for Alexa
let alexaReply = (text) => {
done.send({
response: {
outputSpeech: {
type: 'PlainText',
text: text
},
shouldEndSession: endConv
}
})
// Always set endConv back to false
endConv = false
}
let handleAlexaMessage = (text) => {
endIntents = [
'goodbye',
'thanks',
]
// Send input to Recast.AI
recastClient.dialog({ 'type': 'text', content: text}, { conversationId: sessionId })
.then((res) => {
console.log(res)
// Set end conversation flag to true when we match an end intent, so Alexa stop listening continously
if (res.action && endIntents.indexOf(res.action.slug) > -1) {
console.log('End of conversation')
endConv = true
}
// reply to Alexa
alexaReply(res.messages[0].content)
})
.catch((err) => {
console.error(err)
done.send(err)
})
}
exports.bot = (event, doneFct) => {
console.log('Received event:', JSON.stringify(event, null, 2))
done = doneFct
let helloReplies = [
'Hello! How can I help you today?',
'Welcome back. What do you need?'
]
if (event.request) {
console.log('Alexa message received')
// Alexa Message
if (event.request.type == 'LaunchRequest') {
console.log('Trigger word received')
// User says invovation word, reply hello
alexaReply(helloReplies[Math.floor(Math.random() * helloReplies.length)])
} else if (event.request.type == 'IntentRequest') {
// Conversation
const text = event.request.intent.slots.sentence.value
console.log('Received: ', text)
sessionId = (sessionId == null ? event.session.sessionId : sessionId)
if (text) {
handleAlexaMessage(text)
} else {
done.send(new Error('No text provided'))
}
}
}
console.log('Processing finished')
}