This repository has been archived by the owner on Nov 28, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
139 lines (119 loc) · 2.98 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//#region init
import Vue from 'vue'
import BotUI from './botui/build/botui.min.js'
import {
ApiAiClient,
ApiAiConstants,
IStringMap,
IRequestOptions,
IApiClientOptions,
IServerResponse
} from "api-ai-javascript"
// import { IRequestOptions, IServerResponse, ApiAiConstants } from "api-ai-javascript/ApiAiClient"
// const lang = ApiAiConstants.AVAILABLE_LANGUAGES.FR;
const client = new ApiAiClient({
accessToken: 'f8c540d1fa9b4014b11b5075ee5222bc',
lang: ApiAiConstants.AVAILABLE_LANGUAGES.FR
})
const botui = BotUI('botui-app', {
vue: Vue // pass the dependency.
})
//#endregion
//#region Conversation
requestEventChatbot('WELCOME')
//#endregion
//#region functions
function keepAsking(promise) {
return promise.then(askInput).then(askChatbot)
}
function askInput() {
return botui.action.text({
action: {
placeholder: 'Type here'
}
}).then(res => {
return res.value
})
}
function addMessage(message) {
return botui.message.add({
content: message
})
}
function addSuggestions(suggestions) {
return botui.action.button({
action: suggestions
})
}
function addSuggestionsFromChatbot(suggestions) {
var formattedSugg = suggestions.map(sugg => {
return {
text: sugg.title,
value: sugg.title
}
})
return botui.action.button({
action: formattedSugg
})
}
function addMessagesFromChatbot(messages) {
var promise = new Promise((res, rej) => {
res(0)
});
messages.sort((m1, m2) => {
if (m1.type == 'suggestion_chips'){
return 1
}
return 0
})
messages.forEach(message => {
promise = promise.then(() => {
return addMessageFromChatbot(message)
})
});
return promise;
}
function addMessageFromChatbot(message) {
if (message.type == "simple_response") {
return addMessage(message.textToSpeech)
} else if (message.type == 0) {
return addMessage(message.speech)
}
// Pour le moment les suggestions sont dificiles à gérer
else if (message.type == "suggestion_chips") {
return addSuggestionsFromChatbot(message.suggestions)
}
else {
return new Promise((res, rej) => {
res(0)
})
}
}
function requestEventChatbot(req) {
return keepAsking(client.eventRequest(req)
.then(res => {
return clearChatbotResponse(res)
})
.then(addMessagesFromChatbot)
.catch(err => {
console.log(err);
}))
}
function askChatbot(req) {
return keepAsking(client.textRequest(req)
.then(res => {
return clearChatbotResponse(res)
})
.then(addMessagesFromChatbot)
.catch(err => {
console.log(err);
}))
}
function clearChatbotResponse(res) {
if (res.status.code == 200) {
return res.result.fulfillment.messages
} else {
throw res.status
}
}
//#endregion