Я создал бот Slack со скриптом Google Apps, но по некоторым причинам использование кэша и отправка сообщений занимает много времени. Есть ли способ ускорить его и заставить его работать быстрее? Ниже приведен мой код (время 0 равно 117 секундам, а время 2 равно 150 секундам). Обратите внимание, что я включил в свой код и несколько библиотек, но они не имеют ничего общего с опубликованным мною кодом:
//Get post message from slack
function doPost(e) {
try {
//If interactive button is pressed return a message, if payload exists go in if statement
if(e.parameter.payload) {
return ContentService.createTextOutput(JSON.stringify(replyToButtonPressed(e))).setMimeType(ContentService.MimeType.JSON);
}
//Get JSON from user
var params = JSON.parse(e.postData.getDataAsString());
//Verify challenge check
if (params.type == "url_verification") {
return ContentService.createTextOutput(params.challenge);
}
//return nothing cos its a bot
if(params.event.bot_id) {
return;
}
var start0 = new Date().getTime();
//Use cache to check if duplicate requests are being sent and don't process them
var cache = CacheService.getPublicCache();
var cached = cache.get(params.event.event_ts);
if (cached != null || params.event.user == undefined) {
return false;
}
cache.put(params.event.event_ts, params.event.user, 600); //Don't do anything if same user automatically posted within 5 seconds
var end0 = new Date().getTime();
var time0 = end0 - start0;
console.log("T0: "+time0);
//Get user input
var userInput = params.event.text.toLowerCase();
//Create then post message to slack
var message = createMessage(params, userInput);
var start2 = new Date().getTime();
//Send message with colour attachments if -cw or -c commands are passed in
if ((userInput.match(/-cw/)
|| userInput.match(/-c/)
|| userInput.match(/-help/)
|| userInput.match(/助けて/)
|| userInput.match(/-ahelp/)
|| userInput.match(/-sechelp/)
|| userInput.match(/-user/)
|| userInput.match(/入札/) || userInput.match(/許可/)
|| userInput.match(/保険番号/) || userInput.match(/健康保険番号/) || userInput.match(/年金番号/) || userInput.match(/労働保険判暴/) || userInput.match(/厚生年金番号/)
)
&& !userInput.match(/-cadd/)
&& !userInput.match(/-crmv/)
&& !userInput.match(/-csv/)) {
sendMessageAsAttachment(message, params);
} else if(userInput.match(/-eno/)){
var tempArray = message.split(",");
//Send message to user
sendMessage(tempArray[0], params);
//Send pdf to user
sendFileMessageToSpecificUser(params.event.user, tempArray[1]);
} else {
sendMessage(message, params);
}
var end2 = new Date().getTime();
var time2 = end2 - start2;
console.log("T2: "+time2);
} catch(error) {
console.info(error);
}
//Error code if something is wrong with interactive button
var errorText = {
text:"Error code:379"
};
//Return error code
return ContentService.createTextOutput(JSON.stringify(errorText)).setMimeType(ContentService.MimeType.JSON);
}
//Create message based on the input
function createMessage(params, userInput) {
var message;
if (userInput.match(/hi/)) {
message = "hiiii";
}
return message;
}
function sendMessage(message, params) {
var payload = {
"token": token.SLACK_ACCESS_TOKEN_BOT,
"channel": params.event.channel,
"text": message
};
var options = {
"method" : "post",
"contentType": "application/x-www-form-urlencoded",
"payload" : payload
};
var response = UrlFetchApp.fetch(SLACK_CHAT_URL, options);
return response.getContentText();
}