質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

Q&A

解決済

1回答

376閲覧

LINEbotを使ってアンケートをとり回答毎に別々のメッセージと画像を送る

Tsubanishi

総合スコア43

LINE Messaging API

LINE Messaging APIは、メッセージの送信・返信ができるAPIです。Web APIを経由しアプリケーションサーバとLINEのAPIでやり取りが可能。複数のメッセージタイプや分かりやすいAPIリファレンスを持ち、グループチャットにも対応しています。

0グッド

0クリップ

投稿2023/11/21 11:42

実現したいこと

  • LINE Messaging APIを使用して、初期登録時にアンケートが出るように

設定しました。アンケートの回答毎に違う文章が送られるようになったのですが、
flex messageを一緒に送ろうとコードを組んだら送れませんでした。

前提

できているコードにflex messageを追加し、デプロイしたのですが、
flexmesaageがうまく出ませんでした

発生している問題・エラーメッセージ

const flex_message = { //上記に電球マークでRemove Variable statement Lgnore this error message Disable checking for this fileと出ます

該当のソースコード

Google

1const SPREAD_SHEET_ID = "***************************"; 2const SHEET_NAME = "シート1" 3const ACCESS_TOKEN = "****************************"; 4 5const sheet = SpreadsheetApp.openById(SPREAD_SHEET_ID).getSheetByName(SHEET_NAME); 6const bot = new LineBotSdk.client(ACCESS_TOKEN); 7function doPost(e) { bot.call(e, callback) }; 8function callback(e) { 9 if (bot.bool(e, "message", "text")) { 10 // ユーザーがメッセージ送ってきた時、応答メッセージでアンケートを送る 11 question(e); 12 } else if (e.type == "follow") { 13 question(e); 14 } else if (e.type == "postback") { 15 const postbackData = e.postback.data; 16 if (["会員", "非会員"].includes(postbackData)) { 17 const cells = sheet.createTextFinder(e.source.userId).findAll(); 18 if (cells.length == 0) { 19 sheet.appendRow([e.source.userId, postbackData]); 20 } else { 21 cells[0].offset(0, 1).setValue(postbackData); 22 } 23 if (postbackData === "会員") { 24 bot.replyMessage(e, [bot.textMessage("ご回答ありがとうございます$$会員で登録しました$これから会員のお知らせを配信します$",[ 25 bot.emoji("5ac1bfd5040ab15980c9b435", "001"), 26 bot.emoji("5ac1bfd5040ab15980c9b435", "002"), 27 bot.emoji("5ac1bfd5040ab15980c9b435", "011"), 28 bot.emoji("5ac1bfd5040ab15980c9b435", "009"),])]); 29//↓ここから下のflex messageが出ない 30 const flex_message = { 31 "type": "bubble", 32 "body": { 33 "type": "box", 34 "layout": "vertical", 35 "contents": [ 36 { 37 "type": "image", 38 "url": "***********************", 39 "size": "full", 40 "aspectMode": "cover", 41 "aspectRatio": "1:1", 42 "gravity": "center" 43 }, 44 { 45 "type": "box", 46 "layout": "vertical", 47 "contents": [], 48 "position": "absolute", 49 "background": { 50 "type": "linearGradient", 51 "angle": "0deg", 52 "endColor": "#00000000", 53 "startColor": "#00000099" 54 }, 55 "width": "100%", 56 "height": "40%", 57 "offsetBottom": "0px", 58 "offsetStart": "0px", 59 "offsetEnd": "0px" 60 }, 61 { 62 "type": "box", 63 "layout": "horizontal", 64 "contents": [ 65 { 66 "type": "box", 67 "layout": "vertical", 68 "contents": [ 69 { 70 "type": "box", 71 "layout": "horizontal", 72 "contents": [] 73 }, 74 { 75 "type": "box", 76 "layout": "horizontal", 77 "contents": [] 78 } 79 ], 80 "spacing": "xs" 81 } 82 ], 83 "position": "absolute", 84 "offsetBottom": "0px", 85 "offsetStart": "0px", 86 "offsetEnd": "0px", 87 "paddingAll": "20px" 88 } 89 ], 90 "paddingAll": "0px" 91 } 92} 93 } 94 95 if (postbackData === "非会員") { 96 bot.replyMessage(e, [bot.textMessage(`ご回答ありがとうございます。$$非会員で登録しました$これから非会員のお知らせを配信します$`,[ 97 bot.emoji("5ac1bfd5040ab15980c9b435", "038"), 98 bot.emoji("5ac1bfd5040ab15980c9b435", "044"), 99 bot.emoji("5ac1bfd5040ab15980c9b435", "047"), 100 bot.emoji("5ac1bfd5040ab15980c9b435", "056"),])]); 101 } 102 } 103 } 104}; 105 106function question(e) { 107 bot.replyMessage(e, [bot.flexMessage({ 108 "type": "bubble", 109 "body": { 110 "type": "box", 111 "layout": "vertical", 112 "contents": [ 113 { 114 "type": "text", 115 "wrap": true, 116 "color": "#666666", 117 "size": "sm", 118 "text": "欲しい情報の会員種別を教えてください", 119 "weight": "bold", 120 "style": "italic", 121 "align": "center" 122 } 123 ] 124 }, 125 "footer": { 126 "type": "box", 127 "layout": "vertical", 128 "spacing": "sm", 129 "contents": [ 130 { 131 "type": "button", 132 "style": "primary", 133 "action": { 134 "type": "postback", 135 "label": "会員", 136 "data": "会員" 137 }, 138 "height": "sm", 139 "color": "#00008b" 140 }, 141 { 142 "type": "button", 143 "style": "secondary", 144 "height": "sm", 145 "action": { 146 "type": "postback", 147 "label": "非会員", 148 "data": "非会員" 149 }, 150 "color": "#ffff00" 151 }, 152 { 153 "type": "box", 154 "layout": "vertical", 155 "contents": [], 156 "margin": "sm" 157 } 158 ], 159 "flex": 0 160 } 161 })]); 162} 163

試したこと

const flex_message = {
上記を
function question(e) {
bot.replyMessage(e, [bot.flexMessage({
に変更してみましたが、エラーと表記されてしまいました。
どなたかわかる方いましたら教えていただきたいです。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

要件が良くわからないけど、そのコードだとflex_message を定義しているだけで、どこにも使われていません。
flex_message を定義してから、replyMassage関数の中に追加すればいいだけではないでしょうか。

(「エラーと表記されてしまいました。」ということですけど、エラーになったコード全体が書かれていないので何とも言えません)

js

1function callback(e) { 2 if (bot.bool(e, "message", "text")) { 3 // ユーザーがメッセージ送ってきた時、応答メッセージでアンケートを送る 4 question(e); 5 } else if (e.type == "follow") { 6 question(e); 7 } else if (e.type == "postback") { 8 const postbackData = e.postback.data; 9 if (["会員", "非会員"].includes(postbackData)) { 10 const cells = sheet.createTextFinder(e.source.userId).findAll(); 11 if (cells.length == 0) { 12 sheet.appendRow([e.source.userId, postbackData]); 13 } else { 14 cells[0].offset(0, 1).setValue(postbackData); 15 } 16 17 const flex_message = { 18 "type": "bubble", 19 "body": { 20 "type": "box", 21 "layout": "vertical", 22 "contents": [ 23 { 24 "type": "image", 25 "url": "***********************", 26 "size": "full", 27 "aspectMode": "cover", 28 "aspectRatio": "1:1", 29 "gravity": "center" 30 }, 31 { 32 "type": "box", 33 "layout": "vertical", 34 "contents": [], 35 "position": "absolute", 36 "background": { 37 "type": "linearGradient", 38 "angle": "0deg", 39 "endColor": "#00000000", 40 "startColor": "#00000099" 41 }, 42 "width": "100%", 43 "height": "40%", 44 "offsetBottom": "0px", 45 "offsetStart": "0px", 46 "offsetEnd": "0px" 47 }, 48 { 49 "type": "box", 50 "layout": "horizontal", 51 "contents": [ 52 { 53 "type": "box", 54 "layout": "vertical", 55 "contents": [ 56 { 57 "type": "box", 58 "layout": "horizontal", 59 "contents": [] 60 }, 61 { 62 "type": "box", 63 "layout": "horizontal", 64 "contents": [] 65 } 66 ], 67 "spacing": "xs" 68 } 69 ], 70 "position": "absolute", 71 "offsetBottom": "0px", 72 "offsetStart": "0px", 73 "offsetEnd": "0px", 74 "paddingAll": "20px" 75 } 76 ], 77 "paddingAll": "0px" 78 } 79 } 80 81 if (postbackData === "会員") { 82 bot.replyMessage(e,[ 83 bot.textMessage( 84 "ご回答ありがとうございます$$会員で登録しました$これから会員のお知らせを配信します$", 85 [ 86 bot.emoji("5ac1bfd5040ab15980c9b435", "001"), 87 bot.emoji("5ac1bfd5040ab15980c9b435", "002"), 88 bot.emoji("5ac1bfd5040ab15980c9b435", "011"), 89 bot.emoji("5ac1bfd5040ab15980c9b435", "009"), 90 ] 91 ), 92 bot.flexMessage(flex_message), 93 ]); 94 } 95 96 if (postbackData === "非会員") { 97 bot.replyMessage(e, [ 98 bot.textMessage( 99 `ご回答ありがとうございます。$$非会員で登録しました$これから非会員のお知らせを配信します$`, 100 [ 101 bot.emoji("5ac1bfd5040ab15980c9b435", "038"), 102 bot.emoji("5ac1bfd5040ab15980c9b435", "044"), 103 bot.emoji("5ac1bfd5040ab15980c9b435", "047"), 104 bot.emoji("5ac1bfd5040ab15980c9b435", "056"), 105 ], 106 ), 107 bot.flexMessage(flex_message), 108 ]); 109 } 110 } 111 } 112}; 113

投稿2023/11/21 12:41

編集2023/11/21 12:51
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Tsubanishi

2023/11/21 16:09

flex=messageの定義の場所がわかっていませんでした。的確なアドバイスありがとうございました。とても助かりました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問