-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
211 lines (191 loc) · 7.73 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const express = require('express')
const app = express()
const port = process.env.PORT || 3000
app.use(express.json())
app.post('/', (req, res) => {
const text = req.body.message.text.toLowerCase()
const postback = req.body.message.postback
/* START OF ACTIONS */
const _actions_text = {
title: 'Option 1'
}
const _actions_url = {
title: 'Link Button',
url: 'http://google.com' // optional
}
const _actions_postback = {
title: 'Postback button',
payload: 'THIS_IS_PAYLOAD_KEY' // optional
}
const _actions = [
_actions_text,
_actions_url,
_actions_postback
]
/* END OF ACTIONS */
/* START OF IMAGE */
const _imageObject = {
url: "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png", // required
placeholder: "Image alternate text", // optional
}
/* END OF IMAGE */
let _responseText
if (postback)
_responseText = `Received postback: ${postback}`
else
_responseText = `Received text: ${text}`
/*
Every object can start with this response
const baseResponse = {
text: `Received text: ${text}`, //required
ssml: 'Here are <say-as interpret-as="characters">SSML</say-as> samples.', //optional
actions: _actions, // optional
shouldEndConversation: true // optional, default to false. If set to true, the assistant will terminate the mic
}
*/
// image response
// const imageResponse = {
// text: `Received text: ${text}`, // optional
// ssml: 'Here are <say-as interpret-as="characters">SSML</say-as> samples.', //optional
// image: _imageObject,
// imageDisplayOptions: 'CROPPED', // optional
// actions: _actions // optional
// }
/*
START OF OVERRIDE RESPONSE
*/
// case 1:
// We create a carousel response. For facebook, we just want to send plain text only.
// So we create add override.facebook
// for empty object, we send text only
// const overrideResponse = {
// ...singleCardResponse,
// override: {
// facebook: {}
// }
// }
const _ssml = ''
const textResponse = {
text: _responseText, //required
ssml: _ssml, //optional
actions: _actions // optional
}
switch (text) {
case 'text':
// text response
res.json(textResponse)
break
case 'ssml':
res.json({
text: _responseText,
ssml: '<speak>How do you spell Delight? It is <say-as interpret-as="verbatim">delight</say-as></speak>', //optional
})
break
case 'card':
// single card response
const singleCardResponse = {
text: _responseText,
ssml: _ssml, //optional
items: [{
title: "Tacos",
description: "Description",
image: _imageObject,
actions: _actions // optional
}],
}
res.json(singleCardResponse)
break
case 'carousel':
// carousel response
const carouselResponse = {
text: _responseText, // optional
ssml: _ssml, //optional
items: [{
title: "Tacos",
description: "Description",
image: _imageObject,
actions: _actions // optional
}, {
"title": "Choo Yilin",
"description": "Award-winning jewellery designer Choo Yilin is well known for her modern jade pieces as well as her contemporary take on traditional Chinese and Peranakan wedding jewellery.",
"image": {
'url': "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png",
'placeholder': "Image alternate text"
},
"actions": [{
'title': 'Show me the map',
'url': 'https://maps.google.com'
}]
}]
}
res.json(carouselResponse)
break
// case 'email':
// res.json({
// 'helperIntent': {
// 'name': 'get_email',
// 'success_callback': 'get_email_success'
// }
// })
// break
case 'raw':
/*
We override the google action response in the override field.
As a result, google action user will receive a different response than the rest.
*/
res.json({
text: 'Only google action will see override response.',
'override': {
'alexa': {
'raw': {"version": "1.0", "response": {"outputSpeech": {"type": "PlainText", "text": "This is a message from alexa raw mode"}, "shouldEndSession": false}}
},
'bixby': {
'raw': {'text': 'This is a message from bixby raw mode'}
},
'telegram':{
'raw': {'text': 'This is a message from telegram raw mode'}
},
'line': {
'raw': [{"quickReply": {"items": [{"action": {"label": "Option 1", "text": "Option 1", "type": "message"}, "type": "action"}, {"action": {"label": "Link Button", "text": "Link Button", "type": "message"}, "type": "action"}, {"action": {"label": "Postback button", "text": "Postback button", "type": "message"}, "type": "action"}]}, "text": "This is a message from line raw mode", "type": "text"}] // line can support sending multiple messages in the same request, so we need to wrap it in array
},
'wechat': {
'raw': {
'msgtype': 'text',
'text': {
'content': 'This is a message from wechat raw mode',
}
}
},
'googleaction': {
'raw': {
"expectUserResponse": true,
"expectedInputs": [
{
"possibleIntents": [
{
"intent": "actions.intent.TEXT"
}
],
"inputPrompt": {
"richInitialPrompt": {
"items": [
{
"simpleResponse": {
"textToSpeech": "Here's an example of override google action response. Which type of response would you like to see next?"
}
}
]
}
}
}
]
}
}
}
})
break
default:
res.json(textResponse)
}
})
app.listen(port, () => console.log(`delight-webhook-demo is listening on port ${port}!`))