【LINE bot】アンケートとターゲティングメッセージを送信するチュートリアル【Google Apps Script / LINE Messaging API】
const SPREAD_SHEET_ID = "スプレッドシートのID";
const SHEET_NAME = "シート1"
const ACCESS_TOKEN = "LINE Developersのチャンネルアクセストークン";
const sheet = SpreadsheetApp.openById(SPREAD_SHEET_ID).getSheetByName(SHEET_NAME);
const bot = new LineBotSdk.client(ACCESS_TOKEN);
function doPost(e) { bot.call(e, callback) };
function callback(e) {
if (e.type == "follow") {
question(e);
} else if (e.type == "postback") {
const postbackData = e.postback.data;
if (["犬", "猫", "兎"].includes(postbackData)) {
const cells = sheet.createTextFinder(e.source.userId).findAll();
if (cells.length == 0) {
sheet.appendRow([e.source.userId, postbackData]);
} else {
cells[0].offset(0, 1).setValue(postbackData);
}
bot.replyMessage(e, [bot.textMessage(`ご回答ありがとうございます!`)]);
}
}
};
function question(e) {
bot.replyMessage(e, [bot.flexMessage({
"type": "bubble",
"body": {
"type": "box",
"layout": "vertical",
"contents": [
{
"type": "text",
"wrap": true,
"color": "#666666",
"size": "sm",
"text": "好きな動物を教えてください"
}
]
},
"footer": {
"type": "box",
"layout": "vertical",
"spacing": "sm",
"contents": [
{
"type": "button",
"style": "link",
"height": "sm",
"action": {
"type": "postback",
"label": "いぬ",
"data": "犬"
}
},
{
"type": "button",
"style": "link",
"height": "sm",
"action": {
"type": "postback",
"label": "ねこ",
"data": "猫"
}
},
{
"type": "button",
"style": "link",
"height": "sm",
"action": {
"type": "postback",
"label": "うさぎ",
"data": "兎"
}
},
{
"type": "box",
"layout": "vertical",
"contents": [],
"margin": "sm"
}
],
"flex": 0
}
})]);
}
いただいたサポートは、自己投資と災害復興支援に使わせていただきます。