This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleOpParser.groovy
319 lines (274 loc) · 12 KB
/
SimpleOpParser.groovy
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Name: SimpleOpParser
// Version: 1.1.1
// Description: Helper class for Teneo Web Chat for easy message type creation in Teneo Studio.
// Homepage: https://developers.teneo.ai/resource/channels/easy-message-type-creation-in-studio
public class SimpleOpParser {
// QUICKREPLIES
public static Map createQuickReplyItems(def quickReplies = '') {
def quick_reply_items = []
def quick_reply_map = [:]
for (item in quickReplies.split("\\|")) {
quick_reply_items << ['title':item, 'postback':item]
}
quick_reply_map.type = 'quickreply'
quick_reply_map.quick_replies = quick_reply_items
return quick_reply_map
}
// BUTTONS
public static Map createButtonItems(def buttons = '') {
def button_items = []
def button_map = [:]
for (item in buttons.split("\\|").toList()) {
button_items << ['title':item, 'postback':item]
}
button_map.type = 'buttons'
button_map.button_items = button_items
return button_map
}
// LINKBUTTONS
public static Map createLinkButtonItems(def buttons = '') {
def linkbutton_items = []
def linkbutton_map = [:]
for (item in buttons.split("\\|").toList()) {
linkbutton_items << parseLinkButtonDetails(item)
}
linkbutton_map.type = 'linkbuttons'
linkbutton_map.linkbutton_items = linkbutton_items
return linkbutton_map
}
// CLICKABLE LSITS
public static Map createClickablelistItems(def clickablelist = '') {
def clickablelist_items = []
def clickablelist_map = [:]
for (item in clickablelist.split("\\|").toList()) {
clickablelist_items << ['title':item, 'postback':item]
}
clickablelist_map.type = 'clickablelist'
clickablelist_map.list_items = clickablelist_items
return clickablelist_map
}
// MEDIA ATTACHMENTS
public static Map createImageItem(def image = '') {
def image_map = [:]
if (image.contains('|')) {
def url = image.split("\\|")[0]
def alt = image.split("\\|")[1]
image_map = ['type':'image', 'image_url':url, 'alt':alt]
} else {
image_map = ['type':'image', 'image_url':image]
}
return image_map
}
// AUDIO
public static Map createAudioItem(def audioUrl = '') {
def audio = ['type':'audio', 'audio_url':audioUrl]
return audio
}
// VIDEO
public static Map createVideoItem(def videoUrl = '') {
def videoType = videoUrl.toLowerCase().contains('vimeo') ? 'vimeovideo' : (videoUrl.toLowerCase().contains('youtube')) || (videoUrl.toLowerCase().contains('youtu.be')) ? 'youtubevideo' : 'filevideo'
def video = ['type':videoType, 'video_url':videoUrl]
return video
}
// SYSTEM MESSAGE
public static Map createSystemMessageItem(def msg = '') {
def system_msg = ['type':'system', 'text':msg]
return system_msg
}
// TEXT BUBBLE
public static Map createTextBubbleItem(def text = '') {
def text_bubble = ['type':'text', 'text':text]
return text_bubble
}
public static Map parseLinkButtonDetails(def buttonItem = '') {
def buttonDetails = [:]
if (buttonItem.split(',').length > 0) {
buttonDetails.title = buttonItem.split(',')[0].trim()
}
if (buttonItem.split(',').length > 1) {
buttonDetails.url = buttonItem.split(',')[1].trim()
}
if (buttonItem.split(',').length > 2) {
buttonDetails.target = buttonItem.split(',')[2].trim()
}
return buttonDetails
}
// POSTPROCESSING PARSE PARAMS
public static void parseParams(def _) {
// Simple OP
def lOpItemTypes = ['quickreply', 'buttons', 'linkbuttons', 'clickablelist', 'video', 'audio', 'image', 'system', 'text']
def lOpItems = []
def lComboOrder = []
def mModal = [:]
def mCard = [:]
def sClickableListTitle = ''
def sButtonsTitle = ''
def sLinkButtonsTitle = ''
// Search for simple OPs and convert to JSON
Iterator it = _.getOutputParameters().entrySet().iterator()
while (it.hasNext()) {
def entry = it.next()
if (entry.getKey() in lOpItemTypes) {
switch (entry.getKey()) {
case 'quickreply':
lOpItems << SimpleOpParser.createQuickReplyItems(entry.getValue())
break
case 'buttons':
lOpItems << SimpleOpParser.createButtonItems(entry.getValue())
break
case 'linkbuttons':
lOpItems << SimpleOpParser.createLinkButtonItems(entry.getValue())
break
case 'clickablelist':
lOpItems << SimpleOpParser.createClickablelistItems(entry.getValue())
break
case 'video':
lOpItems << SimpleOpParser.createVideoItem(entry.getValue())
break
case 'audio':
lOpItems << SimpleOpParser.createAudioItem(entry.getValue())
break
case 'image':
lOpItems << SimpleOpParser.createImageItem(entry.getValue())
break
case 'system':
lOpItems << SimpleOpParser.createSystemMessageItem(entry.getValue())
break
case 'text':
lOpItems << SimpleOpParser.createTextBubbleItem(entry.getValue())
break
}
it.remove()
} else if (entry.getKey().startsWith('modal')) {
if (!mModal.keySet().contains('type')) mModal.put('type', 'modal')
switch (entry.getKey()) {
case 'modal_title':
mModal.title = entry.getValue()
break
case 'modal_bodytext':
mModal.text = entry.getValue()
break
case 'modal_image':
def image_map = [:]
if (entry.getValue().contains('|')) {
def url = entry.getValue().split("\\|")[0]
def alt = entry.getValue().split("\\|")[1]
image_map = ['type':'image', 'image_url':url, 'alt':alt]
} else {
image_map = ['type':'image', 'image_url':entry.getValue()]
}
mModal.image = image_map
break
case 'modal_buttons':
def lModalButtons = []
for (item in entry.getValue().split("\\|")) {
lModalButtons << ['title':item, 'postback':item]
}
mModal.button_items = lModalButtons
break
}
it.remove()
} else if (entry.getKey().startsWith('card')) {
if (!mCard.keySet().contains('type')) mCard.put('type', 'card')
switch (entry.getKey()) {
case 'card_title':
mCard.title = entry.getValue()
break
case 'card_subtitle':
mCard.subtitle = entry.getValue()
break
case 'card_bodytext':
mCard.text = entry.getValue()
break
case 'card_image':
def image_map = [:]
if (entry.getValue().contains('|')) {
def url = entry.getValue().split("\\|")[0]
def alt = entry.getValue().split("\\|")[1]
image_map = ['type':'image', 'image_url':url, 'alt':alt]
} else {
image_map = ['type':'image', 'image_url':entry.getValue()]
}
mCard.image = image_map
break
case 'card_buttons':
def button_items = []
for (item in entry.getValue().split("\\|")) {
button_items << ['title':item, 'postback':item]
}
mCard.button_items = button_items
break
case 'card_clickablelist':
def clickablelist_items = []
for (item in entry.getValue().split("\\|")) {
clickablelist_items << ['title':item, 'postback':item]
}
mCard.list_items = clickablelist_items
break
case 'card_links':
def link_items = []
for (item in entry.getValue().split("\\|")) {
link_items << ['title':item.split(', ')[0].trim(), 'url':item.split(', ')[1].trim()]
}
mCard.link_items = link_items
break
case 'card_linkbuttons':
def linkbutton_items = []
for (item in entry.getValue().split("\\|")) {
linkbutton_items << parseLinkButtonDetails(item)
}
mCard.linkbutton_items = linkbutton_items
break
}
it.remove()
} else if (entry.getKey() == 'combo_order') {
lComboOrder = entry.getValue().split("\\|")
it.remove()
} else if (entry.getKey() == 'clickablelist_title') {
sClickableListTitle = entry.getValue()
it.remove()
} else if (entry.getKey() == 'buttons_title') {
sButtonsTitle = entry.getValue()
it.remove()
} else if (entry.getKey() == 'linkbuttons_title') {
sLinkButtonsTitle = entry.getValue()
it.remove()
}
}
// Add modal and card to lOpItems
if (!mModal.isEmpty()) lOpItems << mModal
if (!mCard.isEmpty()) lOpItems << mCard
// Attach button title and list title to buttons and list
if (sClickableListTitle || sButtonsTitle || sLinkButtonsTitle) {
for (item in lOpItems) {
if (item.type == 'clickablelist') {
if (sClickableListTitle) item.title = sClickableListTitle
} else if (item.type == 'buttons') {
if (sButtonsTitle) item.title = sButtonsTitle
} else if (item.type == 'linkbuttons') {
if (sLinkButtonsTitle) item.title = sLinkButtonsTitle
}
}
}
// Create JSON
if (lOpItems.size() == 1) {
def outputJson = new groovy.json.JsonBuilder(lOpItems[0]).toString()
_.putOutputParameter('teneowebclient', outputJson)
} else if (lOpItems.size() > 1) {
def lComboComponents = []
if (lComboOrder.size() > 0) {
for (type in lComboOrder) {
for (item in lOpItems) {
if (item.type == type || (type == 'video' && item.type.contains('type'))) {
lComboComponents << item
}
}
}
} else {
lComboComponents = lOpItems
}
def outputJson = new groovy.json.JsonBuilder(['type':'combo', 'components':lComboComponents]).toString()
_.putOutputParameter('teneowebclient', outputJson)
}
}
}