|
var sheetId = "15x3asddawIXE"; |
|
var sheetName = "20adsasd-4"; |
|
var token = 'adasdasd'; |
|
var telegramToken = 'asdasdasd'; |
|
|
|
var book = SpreadsheetApp.openById(sheetId); |
|
var sheet = book.getSheetByName(sheetName); |
|
|
|
var sheetLog = book.getSheetByName("log"); |
|
|
|
function testDoPostFindAll() { |
|
var requestMock = { |
|
parameter: { |
|
token: "50a1asdasd7ac", |
|
operation: "findAll" |
|
}, |
|
postData: { |
|
contents: {query:"$.name == 'batou'"} |
|
} |
|
}; |
|
Logger.log(requestMock); |
|
var result = doPost(requestMock); |
|
Logger.log(result.getContent()); |
|
} |
|
|
|
function testDoPostNewMessage() { |
|
var requestMock = { |
|
parameter: { |
|
token: "50asdasdca7ac", |
|
operation: "newMessage" |
|
}, |
|
postData: { |
|
contents: {"update_id":661916498, |
|
"message":{"message_id":78,"from":{"id":297927684,"is_bot":false,"first_name":"Kirito","last_name":"ENL","username":"Kiritoxz"},"chat":{"id":-479640753,"title":"Test","type":"group","all_members_are_administrators":true},"date":1594569903,"text":"/animes today","entities":[{"offset":0,"length":7,"type":"bot_command"}]}} |
|
} |
|
}; |
|
Logger.log(requestMock); |
|
var result = doPost(requestMock); |
|
Logger.log(result.getContent()); |
|
} |
|
|
|
function doPost(e) { |
|
|
|
var receivedToken = e.parameter.token; |
|
var callback = e.parameter.callback; |
|
|
|
if (receivedToken !== token) { |
|
var response = { |
|
"status": 401, |
|
"message": "remote execution is not authorized" |
|
}; |
|
return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON); |
|
} |
|
|
|
var apiOperation = e.parameter.operation; |
|
|
|
var response; |
|
|
|
try { |
|
switch (apiOperation) { |
|
case "newMessage": |
|
response = processIncomingMessage(e.postData.contents); |
|
break; |
|
case "findAll": |
|
response = performQuery(null); |
|
break; |
|
case "query": |
|
//if operation is query |
|
var postData = JSON.parse(e.postData.contents); |
|
var query = postData.query; |
|
response = performQuery(query); |
|
break; |
|
default: |
|
response = operationNotImplementedYetError(); |
|
break; |
|
} |
|
|
|
if (callback) { |
|
return createJsonpResponse(callback, response); |
|
} else { |
|
addLogToSheet("before response"); |
|
return ContentService.createTextOutput(JSON.stringify(response)).setMimeType(ContentService.MimeType.JSON); |
|
//return ContentService.createTextOutput(); |
|
} |
|
|
|
|
|
|
|
} catch (err) { |
|
var responseErr = { |
|
"status": 500, |
|
"message": "Internal error:" + err |
|
}; |
|
addLogToSheet("Global:"+err) |
|
return ContentService.createTextOutput(JSON.stringify(responseErr)).setMimeType(ContentService.MimeType.JSON); |
|
} |
|
|
|
} |
|
|
|
function addLogToSheet(payload) { |
|
sheetLog.appendRow([payload]); |
|
} |
|
|
|
function processIncomingMessage(rawBody) { |
|
|
|
addLogToSheet("processIncomingMessage"); |
|
addLogToSheet((typeof rawBody) + rawBody); |
|
|
|
var newMessage = JSON.parse(rawBody); |
|
addLogToSheet((typeof newMessage) + newMessage); |
|
addLogToSheet("text:"+newMessage.message.text); |
|
|
|
// Make a POST request with a JSON payload. |
|
var data = { |
|
'chat_id': newMessage.message.chat.id, |
|
'text': "your command is:"+newMessage.message.text |
|
}; |
|
var options = { |
|
'method' : 'post', |
|
'contentType': 'application/json', |
|
'payload' : JSON.stringify(data) |
|
}; |
|
UrlFetchApp.fetch('https://api.telegram.org/bot'+telegramToken+'/sendMessage', options); |
|
|
|
return newMessage; |
|
} |
|
|
|
//TODO: implement filter at convertSheet2JsonText function in order to speed the elapsed time |
|
//TODO: detect query parameters and validate if exist |
|
function performQuery(query) { |
|
|
|
var data; |
|
try { |
|
data = convertSheet2JsonObject(sheet); |
|
} catch (err) { |
|
return internalError(err) |
|
} |
|
|
|
//if query is null, return entire data |
|
if (query == null) { |
|
var successResponse = { |
|
"status": 200, |
|
"message": "success", |
|
"content": data |
|
}; |
|
return successResponse; |
|
} |
|
|
|
try { |
|
var dataFiltered = []; |
|
data.filter(function($) { |
|
if (eval(query)) { |
|
dataFiltered.push($); |
|
} |
|
}); |
|
|
|
var successResponse = { |
|
"status": 200, |
|
"message": "success", |
|
"content": dataFiltered |
|
}; |
|
return successResponse; |
|
|
|
} catch (err) { |
|
return internalError(err) |
|
} |
|
} |
|
|
|
function createJsonpResponse(callback, data) { |
|
return ContentService.createTextOutput(callback + '(' + JSON.stringify(data) + ')').setMimeType(ContentService.MimeType.JAVASCRIPT); |
|
} |
|
|
|
function createJsonResponse(data) { |
|
return ContentService.createTextOutput(JSON.stringify(data)).setMimeType(ContentService.MimeType.JSON); |
|
} |
|
|
|
function internalError(err) { |
|
var response = { |
|
"status": 500, |
|
"message": "Internal error:" + err |
|
}; |
|
return response; |
|
} |
|
|
|
function operationNotImplementedYetError(operation) { |
|
var response = { |
|
"status": 400, |
|
"message": "operation not implemented yet: " + operation |
|
}; |
|
return response; |
|
} |
|
|
|
function convertSheet2JsonObject(sheet) { |
|
// first line(title) |
|
var colStartIndex = 1; |
|
var rowNum = 1; |
|
var firstRange = sheet.getRange(1, 1, 1, sheet.getLastColumn()); |
|
var firstRowValues = firstRange.getValues(); |
|
var titleColumns = firstRowValues[0]; |
|
|
|
// after the second line(data) |
|
var lastRow = sheet.getLastRow(); |
|
var rowValues = []; |
|
for (var rowIndex = 2; rowIndex <= lastRow; rowIndex++) { |
|
var colStartIndex = 1; |
|
var rowNum = 1; |
|
var range = sheet.getRange(rowIndex, colStartIndex, rowNum, sheet.getLastColumn()); |
|
var values = range.getValues(); |
|
rowValues.push(values[0]); |
|
} |
|
|
|
// create json |
|
var jsonArray = []; |
|
for (var i = 0; i < rowValues.length; i++) { |
|
var line = rowValues[i]; |
|
var json = new Object(); |
|
for (var j = 0; j < titleColumns.length; j++) { |
|
json[titleColumns[j]] = line[j]; |
|
} |
|
jsonArray.push(json); |
|
} |
|
return jsonArray; |
|
} |