前提・実現したいこと
GASを利用したLine botの投票機能。現段階ではifを用いて取得したメッセージに応じて返信を変更する。Line bot、JavaScriptともに初めて触りました。
発生している問題・エラーメッセージ
実行できない
該当のソースコード
javaScript
1var CHANNEL_ACCESS_TOKEN = "; 2 3function doPost(e) { 4 var contents = e.postData.contents; 5 var obj = JSON.parse(contents); 6 var events = obj["events"]; 7 for (var i = 0; i < events.length; i++) { 8 if (events[i].type == "投票") { 9 reply_message(events[i]); 10 } else if (events[i].type == "No.1ねこさんに投票しました") { 11 reply_message2(events[i]); 12 } 13 } 14} 15 16function reply_message(e) { 17 var postData = { 18 "replyToken": e.replyToken, 19 "messages": [ 20 { 21 "type": "flex", 22 "altText": "投票用flex message です", 23 "contents": { 24 "type": "carousel", 25 "contents": [ 26 {"type": "bubble", 27 "hero": { "type": "image", "size": "full", "aspectRatio": "1:1","aspectMode": "cover","url": "https://No.1.JPG" }, 28 "body": {"type": "box","layout": "vertical","spacing": "sm", 29 "contents": [ 30 {"type": "text","wrap": true,"weight": "bold","size": "xl","text": "ねこ"}, 31 {"type": "box","layout": "baseline","contents": [ 32 {"type": "text","text": "No.1","wrap": true,"weight": "regular","size": "xl","flex": 0}] 33 } ] }, 34 "footer": {"type": "box","layout": "vertical","spacing": "sm","contents": [ 35 {"type": "button","style": "primary","action": 36 {"type": "postback","data": "No.1","label": "投票", "text": "No.1ねこさんに投票しました"} 37 } ] } 38 }, {"type": "bubble","hero": 39 {"type": "image","size": "full","aspectMode": "cover","url": "https://No.2.JPG","aspectRatio": "10:10"}, 40 "body": {"type": "box","layout": "vertical","spacing": "none", 41 "contents": [ 42 { "type": "text","text": "いぬ","wrap": true,"weight": "bold","size": "xl"}, 43 {"type": "box","layout": "baseline","flex": 1,"contents": 44 [{"type": "text","wrap": true,"weight": "regular","size": "xl","flex": 0,"text": "No.2"}] 45 } ] }, 46 "footer": {"type": "box","layout": "vertical","spacing": "sm", 47 "contents": [{"type": "button","flex": 2,"style": "primary", 48 "action": {"type": "postback","data": "No.1","label": "投票", "text": "No.2いぬさんに投票しました"} 49 }] } }, 50 { "type": "bubble","hero": 51 {"type": "image","size": "full","aspectMode": "cover","url": "https://.JPG","aspectRatio": "10:10"}, 52 "body": {"type": "box","layout": "vertical","spacing": "none", 53 "contents": [ 54 { "type": "text","text": "くま","wrap": true,"weight": "bold","size": "xl"}, 55 {"type": "box","layout": "baseline","flex": 1,"contents": 56 [{"type": "text","wrap": true,"weight": "regular","size": "xl","flex": 0,"text": "No.3"}] 57 } ] 58 }, 59 "footer": {"type": "box","layout": "vertical","spacing": "sm", 60 "contents": [{"type": "button","flex": 2,"style": "primary", 61 "action": {"type": "postback","data": "No.1","label": "投票", "text": "No.3くまさんに投票しました"} 62 }] 63 } 64 }, { "type": "bubble","hero": 65 {"type": "image","size": "full","aspectMode": "cover","url": "https://No.4.JPG","aspectRatio": "10:10"}, 66 "body": {"type": "box","layout": "vertical","spacing": "none", 67 "contents": [ 68 { "type": "text","text": "さる","wrap": true,"weight": "bold","size": "xl"}, 69 {"type": "box","layout": "baseline","flex": 1,"contents": 70 [{"type": "text","wrap": true,"weight": "regular","size": "xl","flex": 0,"text": "No.4"}] 71 } ] 72 }, 73 "footer": {"type": "box","layout": "vertical","spacing": "sm", 74 "contents": [{"type": "button","flex": 2,"style": "primary", 75 "action": {"type": "postback","data": "No.1","label": "投票", "text": "No.4さるさんに投票しました"} 76 }] } }, 77 {"type": "bubble","hero": 78 {"type": "image","size": "full","aspectMode": "cover","url": "https://No.5.JPG","aspectRatio": "10:10"}, 79 "body": {"type": "box","layout": "vertical","spacing": "none", 80 "contents": [ 81 { "type": "text","text": "うさぎ","wrap": true,"weight": "bold","size": "xl"}, 82 {"type": "box","layout": "baseline","flex": 1,"contents": 83 [{"type": "text","wrap": true,"weight": "regular","size": "xl","flex": 0,"text": "No.5"}] 84 } ] 85 }, 86 "footer": {"type": "box","layout": "vertical","spacing": "sm", 87 "contents": [{"type": "button","flex": 2,"style": "primary", 88 "action": {"type": "postback","data": "No.1","label": "投票", "text": "No.5うさぎさんに投票しました"} 89 }] } 90 }, { "type": "bubble","hero": 91 {"type": "image","size": "full","aspectMode": "cover","url": "https://No.6.JPG","aspectRatio": "10:10"}, 92 "body": {"type": "box","layout": "vertical","spacing": "none", 93 "contents": [ 94 { "type": "text","text": "かめ","wrap": true,"weight": "bold","size": "xl"}, 95 {"type": "box","layout": "baseline","flex": 1,"contents": 96 [{"type": "text","wrap": true,"weight": "regular","size": "xl","flex": 0,"text": "No.6"}] 97 } ] 98 }, 99 "footer": {"type": "box","layout": "vertical","spacing": "sm", 100 "contents": [{"type": "button","flex": 2,"style": "primary", 101 "action": {"type": "postback","data": "No.1","label": "投票", "text": "No.6かめさんに投票しました"} 102 }] } } ] } }] }; 103 var options = { 104 "method": "post", 105 "headers": { 106 "Content-Type": "application/json", 107 "Authorization": "Bearer " + CHANNEL_ACCESS_TOKEN 108 }, 109 "payload": JSON.stringify(postData) 110 }; 111 UrlFetchApp.fetch("https://api.line.me/v2/bot/message/reply", options); 112} 113 114 115function reply_message2(e) { 116 var postData = { 117 "replyToken": e.replyToken, 118 "messages": [ 119 { 120 "type": "flex", 121 "altText": "投票用flex message です", 122 "contents": { 123 "type": "flex", 124 "altText": "投票用flex message です", 125 "contents": { 126 "type": "text", 127 "text": "投票が完了しました。", 128 "weight": "regular" 129 } 130 }}] 131 132}; 133 var options = { 134 "method": "post", 135 "headers": { 136 "Content-Type": "application/json", 137 "Authorization": "Bearer " + CHANNEL_ACCESS_TOKEN 138 }, 139 "payload": JSON.stringify(postData) 140 }; 141 UrlFetchApp.fetch("https://api.line.me/v2/bot/message/reply", options); 142} 143 144
あなたの回答
tips
プレビュー