実現したいこと
LINE MESSAGING APIを用いてのリッチメニューの実装
前提
Node.jsを用いてLINEBOTを作成しています。
リッチメニューをMessagingAPIから作成して、それをタップすると公式アカウントからuserId入りリンクが送信される機能を作成したいです。
該当コード部分には、何が原因になっているかはわからないため、念のためLINE Messaging APIに関連するコードをすべて記述します。
発生している問題・エラーメッセージ
setRichMenuImageでエラーが発生していると思われます。
2023-06-16T08:44:34.659043+00:00 app[web.1]: There was an error in creating the rich menu: Error: invalid data type for binary data 2023-06-16T08:44:34.659044+00:00 app[web.1]: at HTTPClient.toBuffer (/app/node_modules/@line/bot-sdk/dist/http.js:70:19) 2023-06-16T08:44:34.659045+00:00 app[web.1]: at HTTPClient.postBinary (/app/node_modules/@line/bot-sdk/dist/http.js:74:35) 2023-06-16T08:44:34.659045+00:00 app[web.1]: at Client.setRichMenuImage (/app/node_modules/@line/bot-sdk/dist/client.js:203:26) 2023-06-16T08:44:34.659046+00:00 app[web.1]: at /app/server.js:123:26 2023-06-16T08:44:34.659046+00:00 app[web.1]: at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
node.js
node.js
1//webhookの設定 2app.post('/webhook_scool', line.middleware(config_scool), (req, res) => { 3 console.log("After line.middleware for school: ", req.body); 4 Promise 5 .all(req.body.events.map(handleEvent_scool)) 6 .then((result) => res.json(result)) 7 .catch((err) => { 8 console.error(err); 9 res.status(500).end(); 10 }); 11}); 12 13//リッチメニューの設定 14let richMenuId; 15 16const richMenu = { 17 size: { 18 width: 2500, 19 height: 843 20 }, 21 selected: false, 22 name: 'Menu', 23 chatBarText: 'Menu', 24 areas: [ 25 { 26 bounds: { 27 x: 0, 28 y: 0, 29 width: 833, 30 height: 843 31 }, 32 action: { 33 type: 'postback', 34 data: 'action=goto_forum' 35 } 36 } 37 ] 38}; 39 40//リッチメニューを作成。画像もともに。 41 42client_school.createRichMenu(richMenu) 43 .then((id) => { 44 console.log('Rich menu was created with id:', id); 45 richMenuId = id; 46 return downloadImage('image-url.jpg'); 47 }) 48 .then((buffer) => { 49 return client_school.setRichMenuImage(richMenuId, 'image/jpeg', buffer); 50 }) 51 .then(() => { 52 console.log('Image has been set to the rich menu'); 53 }) 54 .catch((err) => { 55 console.error('There was an error in creating the rich menu:', err); 56 }); 57 58//画像ダウンロード処理 59//エラー該当コード部分 60 61 function downloadImage(url) { 62 return new Promise((resolve, reject) => { 63 https.get(url, (res) => { 64 const data = []; 65 res.on('data', (chunk) => data.push(chunk)); 66 res.on('end', () => { 67 const buffer = Buffer.concat(data); 68 console.log('Downloaded image data:', buffer); 69 resolve(buffer); 70 }); 71 }).on('error', reject); 72 }); 73 } 74 75//webhookに来たeventをもとに、メッセージ送信などを行う。 76//友達追加時にメッセージ送信、リッチメニューからのpostback通信の際にメッセージ送信を行っている。 77 78async function handleEvent_scool(event) { 79 if (!richMenuId) { 80 console.error('Rich menu ID has not been set'); 81 return Promise.resolve(null); 82 } 83 if (event.type === 'follow') { 84 const userId = event.source.userId; 85 const forumUrl = `my-app-link/forum?userId=${userId}`; 86 87 client_school.linkRichMenuToUser(userId, richMenuId) 88 .then(() => { 89 console.log(`Successfully linked rich menu to user ${userId}`); 90 }) 91 .catch((err) => { 92 console.error(err); 93 }); 94 95 return client_school.replyMessage(event.replyToken, { 96 "type": "template", 97 "altText": "スマートフォンでのみ確認可能なメッセージです。", 98 "template": { 99 "type": "buttons", 100 "text": "友達追加ありがとうございます!\n\n下の「開く」ボタンを押してフォーラムを開き、必要事項をご入力ください。", 101 "actions": [ 102 { 103 "type": "uri", 104 "label": "開く", 105 "uri": forumUrl 106 } 107 ] 108 } 109 }); 110 111 } else if (event.type === 'postback' && event.postback.data === 'action=goto_forum') { 112 const userId = event.source.userId; 113 const url = `my-app-link/forum_active?userId=${userId}`; 114 115 const message = { 116 type: 'text', 117 text: `フォーラムへ: ${url}` 118 }; 119 120 return client_school.replyMessage(event.replyToken, message); 121 } 122 123 return Promise.resolve(null); 124 } 125
試したこと
画像の変更。
画像の形式の変更(jpg→png)
ダウンロードされた画像データの出力。以下出力内容
2023-06-16T08:44:33.851477+00:00 app[web.1]: Rich menu was created with id: richmenu-id 2023-06-16T08:44:34.657013+00:00 app[web.1]: Downloaded image data: <Buffer ff d8 ff db 00 84 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 ... 36514 more bytes>
補足情報
以下package.jsonの内容
"@line/bot-sdk": "^7.5.2", "axios": "^1.4.0", "bcrypt": "^5.1.0", "body-parser": "^1.20.2", "cheerio": "^1.0.0-rc.12", "connect-flash": "^0.1.1", "ejs": "^3.1.5", "errorhandler": "^1.5.1", "express": "^4.17.2", "express-session": "^1.17.3", "koa": "^2.13.4", "mysql": "^2.18.1", "node-fetch": "^3.3.1", "nodemon": "^2.0.15"

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。