Skip to content

Instantly share code, notes, and snippets.

@apivat60
Created November 22, 2020 06:51
Show Gist options
  • Save apivat60/8f233ecb167067e78545ee18d73511c5 to your computer and use it in GitHub Desktop.
Save apivat60/8f233ecb167067e78545ee18d73511c5 to your computer and use it in GitHub Desktop.
var LINE_ACCESS_TOKEN = "aaa";
var ss = SpreadsheetApp.openById("aaa");
var sh = ss.getSheetByName("aaa");
function doPost(e) {
if (typeof e === "undefined") {
return;
} else {
var json = JSON.parse(e.postData.contents);
replyFromSheet(json)
}
}
function replyFromSheet(data) {
var replyUrl = "https://api.line.me/v2/bot/message/reply";
var lastRow = sh.getLastRow();
var wordList = sh.getRange(1, 1, lastRow, 2).getValues();
var reply_token = data.events[0].replyToken;
var text = data.events[0].message.text;
var replyTextList = [];
for (var i = 1; i < wordList.length; i++) {
if (wordList[i][0] == text) {
replyTextList.push(wordList[i][1]);
}
}
if (replyTextList.length < 1) {
return;
} else if (replyTextList.length > 5) {
var messageLength = 5;
} else {
var messageLength = replyTextList.length;
}
var messageArray = [];
for (var j = 0; j < messageLength; j++) {
messageArray.push({
"type": "text",
"text": replyTextList[j]
});
}
var headers = {
"Content-Type": "application/json; charset=UTF-8",
"Authorization": "Bearer " + LINE_ACCESS_TOKEN,
};
var postData = {
"replyToken": reply_token,
"messages": messageArray
};
var options = {
"method": "post",
"headers": headers,
"payload": JSON.stringify(postData)
};
UrlFetchApp.fetch(replyUrl, options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment