forked from ricardomiron/puntos-de-conectividad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheets2chatfuel.js
60 lines (56 loc) · 1.28 KB
/
sheets2chatfuel.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
function doGet() {
var sheet = SpreadsheetApp.getActiveSheet();
var data = sheet.getDataRange().getValues();
elements = create_elements(data)
if (elements.length != 0) {
return buildImageGallery(elements);
} else {
return notFound()
}
}
function create_elements(data) {
var elements = [];
for (var i = 1; i < data.length; i++) {
var object = {
title : data[i][0],
image_url : data[i][2],
subtitle : data[i][1],
buttons : [
{
type: "web_url",
url: data[i][3],
title: "Ver en el mapa"
}
]
}
elements.push(object)
}
return elements
}
function buildImageGallery(elements) {
var output = JSON.stringify({
messages: [
{
attachment: {
type: "template",
payload: {
template_type:"generic",
image_aspect_ratio: "square",
elements: elements
}
}
}
]
});
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}
function notFound() {
var output = JSON.stringify({
messages: [
{
text: "There are no items in this category"
}
]
});
return ContentService.createTextOutput(output).setMimeType(ContentService.MimeType.JSON);
}