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

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

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

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Q&A

解決済

1回答

695閲覧

LINE MESSAGING APIを用いてのリッチメニューの画像の実装で”Error: invalid data type for binary data”エラーが出る

Azel_T3T

総合スコア117

LINE Messaging API

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

0グッド

0クリップ

投稿2023/06/16 09:00

編集2023/06/16 09:22

実現したいこと

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"

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

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

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

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

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

guest

回答1

0

自己解決

リッチメニューをOfficial Accout Managerから作成することで解決しました。
リッチメニューのアクションをテキストにすることで、ユーザーからメッセージを送信させることができるようです。
それを踏まえた修正コードです。

Node.js

1async function handleEvent_scool(event) { 2 const userId = event.source.userId; 3 4 if (event.type === 'follow') { 5 const forumUrl = `my-app-url/forum?userId=${userId}`; 6 7 return client_school.replyMessage(event.replyToken, { 8 "type": "template", 9 "altText": "スマートフォンでのみ確認可能なメッセージです。", 10 "template": { 11 "type": "buttons", 12 "text": "友達追加ありがとうございます!\n\n下の「開く」ボタンを押してフォーラムを開き、必要事項をご入力ください。", 13 "actions": [ 14 { 15 "type": "uri", 16 "label": "開く", 17 "uri": forumUrl 18 } 19 ] 20 } 21 }); 22 } else if (event.type === 'message' && event.message.type === 'text' && event.message.text === '練習ログ登録をする。') { 23 const forumActiveUrl = `my-app-url/forum2?userId=${userId}`; 24 25 return client_school.replyMessage(event.replyToken, { 26 "type": "template", 27 "altText": "スマートフォンでのみ確認可能なメッセージです。", 28 "template": { 29 "type": "buttons", 30 "text": "以下のリンクから活動報告フォームを開いてください", 31 "actions": [ 32 { 33 "type": "uri", 34 "label": "開く", 35 "uri": forumActiveUrl 36 } 37 ] 38 } 39 }); 40 } 41 42 return Promise.resolve(null); 43} 44

投稿2023/06/20 07:10

Azel_T3T

総合スコア117

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.31%

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

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

質問する

関連した質問