-
Notifications
You must be signed in to change notification settings - Fork 0
/
POLL-JSON-Handler-Script.gs
204 lines (164 loc) · 5.83 KB
/
POLL-JSON-Handler-Script.gs
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
/************************************************************
* TELEGRAM POLL ANSWER JSON HANDLER WITH GOOGLE APPS SCRIPT *
*************************************************************/
/***************************
* JSON POLL ANSWER SAMPLES *
****************************/
const jsonPollUserbased = {
"update_id": 603402545,
"poll_answer": {
"poll_id": "6301080145537007634",
"user": {
"id": 1821266651,
"is_bot": false,
"first_name": "RSUD CIBABAT",
"last_name": "CIMAHI",
"username": "rscibabat",
"language_code": "en"
},
"option_ids": [0,2]
}
};
const jsonPollAnonymous = {
"update_id": 603402549,
"poll": {
"id": "6301080145537007639",
"question": "[1821266651] Your favorite color",
"options": [
{
"text": "RED",
"voter_count": 0
},
{
"text": "GREEN",
"voter_count": 0
},
{
"text": "BLUE",
"voter_count": 1
}
],
"total_voter_count": 1,
"is_closed": false,
"is_anonymous": true,
"type": "regular",
"allows_multiple_answers": false
}
};
/****************************************************
* JSON POLL ANSWER KEYS AND VALUES SCANNER FUNCTION *
* Modified version of getDeepKeys(obj) function:
* https://stackoverflow.com/a/42674656/12682081
*****************************************************/
function scanJSONPoll( obj ) {
let data = [];
let keys = [];
let values = [];
for ( var key in obj ) {
/*********************************************
* HANDLING NON OBJECT PROPERTIES *
* https://stackoverflow.com/a/4775737/12682081
**********************************************/
if ( Object.prototype.toString.call( obj[key] ) !== "[object Object]" ) {
/****************************
* HANDLING ARRAY PROPERTIES *
* options and options_ids
*****************************/
if ( Array.isArray( obj[key] ) ) {
/*******************************************
* HANDLING NUMERIC PROPERTIES WITHIN ARRAY *
* "options_ids": [0,1,2,...]
********************************************/
if ( Object.prototype.toString.call( obj[key][0] ) === "[object Number]" ){
keys.push( key );
let userAnswer = "";
for ( let i=0; i < obj[key].length; i++ ) {
userAnswer += pollOptions[ obj[key][i] ];
userAnswer += i < obj[key].length - 1 ? "-" : "";
}
values.push( userAnswer );
/***********************************************************
* HANDLING OBJECT PROPERTIES WITHIN ARRAY *
* "options": [ {"text":"RED","voter_count": 0}, {...}, ... ]
************************************************************/
} else {
for ( let i=0; i < obj[key].length; i++ ) {
keys.push( Object.values( obj[key][i] )[0] );
values.push( String( Object.values( obj[key][i] )[1] ) );
}
}
/***********************************************
* HANDLING NON ARRAY AND NON OBJECT PROPERTIES *
* "poll_id": "6301080145537007638"
************************************************/
} else {
keys.push( key );
values.push( String( obj[key] ) );
}
/*************************************************
* HANDLING OBJECT PROPERTIES *
* "user": { "id":1821266651, "is_bot":false, ... }
**************************************************/
} else if ( Object.prototype.toString.call( obj[key] ) === "[object Object]" ) {
for ( let property in obj[key] ) {
keys.push( property );
values.push( String( obj[key][property] ) );
}
/*****************************
* HANDLING OTHERS PROPERTIES *
******************************/
} else {
let subkeys = scanJSONPoll( obj[key] );
keys = keys.concat(
subkeys.map(
function( subkey ) {
return subkey;
}
)
);
}
}
data.push( keys, values );
return data;
}
/****************
* Let's test it *
*****************/
function testScanJSONPoll(){
/***** USER-BASED POLL KEYS SET in case for sheets column names *****/
let pollUserKeys = scanJSONPoll( jsonPollUserbased.poll_answer )[0];
Logger.log( pollUserKeys );
for ( let i=0; i < pollUserKeys.length; i++ ) {
Logger.log( pollUserKeys[i] );
}
/***** USER-BASED POLL VALUES SET *****/
let pollUserValues = scanJSONPoll( jsonPollUserbased.poll_answer )[1];
Logger.log( pollUserValues );
for ( let i=0; i < pollUserValues.length; i++ ) {
Logger.log( pollUserValues[i] );
}
/***** ANONYMOUS POLL KEYS SET in case for sheets column names *****/
let pollAnonymousKeys = scanJSONPoll( jsonPollAnonymous.poll )[0];
Logger.log( pollAnonymousKeys );
for ( let i=0; i < pollAnonymousKeys.length; i++ ) {
Logger.log( pollAnonymousKeys[i] );
}
/***** ANONYMOUS POLL VALUES SET *****/
let pollAnonymousValues = scanJSONPoll( jsonPollAnonymous.poll )[1];
Logger.log( pollAnonymousValues );
for ( let i=0; i < pollAnonymousValues.length; i++ ) {
Logger.log( pollAnonymousValues[i] );
}
}
/******************
* EXPECTED OUTPUT *
*******************
[
[poll_id, id, is_bot, first_name, last_name, username, language_code, option_ids],
[6301080145537007634, 1821266651, false, RSUD CIBABAT, CIMAHI, rscibabat, en, RED-BLUE]
]
[
[id, question, RED, GREEN, BLUE, total_voter_count, is_closed, is_anonymous, type, allows_multiple_answers],
[6301080145537007639, [1821266651] Your favorite color, 0, 0, 1, 1, false, true, regular, false]
]
*******************/